开发者

Is it possible to programmatically show the red delete button on a UITableViewCell?

开发者 https://www.devze.com 2023-02-16 22:34 出处:网络
There are lots of similar questions on here, but none that I think specifically ask this question, which is, is there any way in code to force the red delete button to appear at the right-side of a UI

There are lots of similar questions on here, but none that I think specifically ask this question, which is, is there any way in code to force the red delete button to appear at the right-side of a UITableView row?

The reason I ask is that I'm trying to change the behaviour of a tableview using two UISwipeGestureRecognizers such that:

  • a single-finger swipe invokes a custom action (instead of causing the red delete button to show, which is how it behaves now), and

  • a double-finger swipe invokes the default single-finger swipe behaviour, i.e. causes the red delete button to show.

I have scoured through the SDK docs but I can't find any way of causing that red button to appear, which makes me think that the proposed UI scheme above 开发者_Go百科is impossible to implement without manually creating the red delete button from scratch and trying to make it emulate the built-in one's behaviour.

Any help is much appreciated.


In the doubleFingerSwipe gesture's selector method set one variable and assign as no of row swiped and reload the table . In the

- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == yourVariable) return UITableViewCellEditingStyleDelete;
    else return UITableViewCellEditingStyleNone;
}

I think thats work for ur problem.


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d",indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        if(TableMethod==1)
        {
            UIButton *delete_btn=[UIButton buttonWithType:UIButtonTypeCustom];
            [delete_btn setFrame:CGRectMake(10,15,25,25)];
            delete_btn.tag=indexPath.row;
            [delete_btn setImage:[UIImage imageNamed:@"remove_Icone.png"] forState:UIControlStateNormal];
            [delete_btn addTarget:self action:@selector(delete_btn:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:delete_btn];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    // Configure the cell.
    return cell;
}


- (IBAction)delete_btn:(id)sender
{
    UIButton *buttontag = (UIButton *)sender;
    //NSLog(@"%d",buttontag.tag);
    Delete_row = buttontag.tag;

    UIAlertView *Alert = [[UIAlertView alloc]initWithTitle:@"Delete Reservation"   message:@"Are you sure to want Delete Reservation ??" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel",nil];
    Alert.tag=1;
    [Alert show];
    [Alert release];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if(alertView.tag==1)
    {
        if(buttonIndex == 0)
        {
            // delete row
        }
    }
}


This is not an answer to your question, but a suggestion about the functionality (I can't leave comments yet, but I would still like to offer my two cents).

From a UI/UX perspective, overriding the default or "expected" functionality is not a great idea. Users have certain expectations about how various apps and features will work, and when they don't work in that manner, the users either get frustrated or confused. Hence the proliferation of the pull-to-refresh functionality in nearly every app that involves a table view. What would make more sense would be to have the two-finger swipe be the custom functionality. Not only would your app then conform to traditional UX guidelines (in fact, I think Apple might reject your app as it stands, and that is probably why you're having such a hard time finding an answer), but it would involve a lot less struggle/custom code.

So sorry that wasn't a direct answer to your question, but I hope it helps.


By calling

[self.tableView setEditing:YES animated:YES];

on a UITableViewController you can set the tableView to editing mode. you can actually add a gesture recognizer to the cell a cell view that makes that call using a delegate method.

According to the Documentation of UITableViewCell you can actually set the same way an individual cell. Apart from that you will have to manage the standard gesture recognizer. I guess subclassing would be your ally on that situation.

I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消