How to detect a swipe-to-delete gesture in a customized UITableviewCell?

后端 未结 2 1050
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 11:07

I have customized a UITableViewCell and I want to implement \"swipe to delete\". But I don\'t want the default delete button. Instead, I want to do something different. What

相关标签:
2条回答
  • 2020-12-13 11:39

    Here are two methods that can be used to avoid the Delete Button:

    - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
    

    and

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    
    0 讨论(0)
  • 2020-12-13 11:42

    If you want to do something completely different, add a UISwipeGestureRecognizer to each tableview cell.

    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    
        // Configure the cell.
    
    
        UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)];
        [sgr setDirection:UISwipeGestureRecognizerDirectionRight];
        [cell addGestureRecognizer:sgr];
        [sgr release];
    
        cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];
        // ...
        return cell;
    }
    
    - (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer {
        if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
            UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
            NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
            //..
        }
    }
    
    0 讨论(0)
提交回复
热议问题