2013-11-22 152 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; 
    } 
} 
相關問題