2013-11-22 150 views
1

我可以把一个UITableView进入编辑模式并显示删除按钮。我如何在删除按钮旁边添加一个蓝色的“编辑”按钮?编辑和删除按钮UITableView

就像在ios6邮件中向左滑动一样,除了邮件应用显示“更多”,我想要一个“编辑”按钮。

+0

'UITableViewCell'不支持这样的功能。你需要推出自己的。见https://github.com/CEWendel/SWTableViewCell – rmaddy

+0

好的,谢谢你的链接。 – user2228755

回答

1

这不是Apple的标准功能UITableViewCell - 您需要使用自己的滑动识别器制作自己的子类UITableViewCell

This GitHub project是一个很好的开始 - 使用它,你应该能够使用这个代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"Cell"; 

    SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) { 
     NSMutableArray *rightUtilityButtons = [NSMutableArray new]; 

     [rightUtilityButtons sw_addUtilityButtonWithColor: 
        [UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0] 
        title:@"More"]; 
     [rightUtilityButtons sw_addUtilityButtonWithColor: 
        [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f] 
         title:@"Delete"]; 

     cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
        reuseIdentifier:cellIdentifier 
        containingTableView:_tableView // For row height and selection 
        leftUtilityButtons:nil 
        rightUtilityButtons:rightUtilityButtons]; 
     cell.delegate = self; 
    } 
... 

return cell; 

然后,您可以实现对电池的委托方法:

- (void)swippableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index { 
    switch (index) { 
     case 0: 
      NSLog(@"More button was pressed"); 
      break; 
     case 1: 
     { 
      // Delete button was pressed 
      NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell]; 

      [_testArray removeObjectAtIndex:cellIndexPath.row]; 
      [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath] 
        withRowAnimation:UITableViewRowAnimationAutomatic]; 
      break; 
     } 
     default: 
      break; 
    } 
} 
相关问题