2016-09-16 52 views
1

我已經研究過這個過程,但似乎沒有找到解決方案。我有一個自定義的UITableViewCell(具有各種子視圖,包括單選按鈕,標籤等)。當表格視圖設置爲編輯時,我希望+和 - 插入/刪除編輯控件出現在單元格的最左側部分。編輯自定義UITableViewCell時不會出現插入/刪除編輯控件

如果我使用標準的UITableViewCell,這可以很好地工作。但是,在使用自定義單元格時,控件不會顯示。任何人有任何想法如何解決同樣的問題嗎?

下面是我的表視圖代碼中的一些快照....

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (self.isEditing) { 
     if ([tableView isEqual:self.tableView]) { 
      if (editingStyle == UITableViewCellEditingStyleInsert) { 
       // ... 
      }     
      else if (editingStyle == UITableViewCellEditingStyleDelete) { 
       // ... 
      } 
     } 
    } 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([tableView isEqual:self.tableView]) { 
     if (indexPath.row == 0) { 
      return UITableViewCellEditingStyleInsert; 
     } 
     else { 
      return UITableViewCellEditingStyleDelete; 
     } 
    } 
    else { 
     return UITableViewCellEditingStyleNone; 
    } 
} 

而定製的表視圖單元代碼...

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
} 

- (void)setEditing:(BOOL)editing animated:(BOOL)animated 
{ 
    [self setNeedsLayout]; 
} 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    [self configureConstraints]; 
} 

- (void)configureConstraints 
{ 
    // This is where the cell subviews are laid out. 
} 

回答

3

您沒有在自定義單元格中正確實施setEditing:animated:方法。你忘了打電話super

- (void)setEditing:(BOOL)editing animated:(BOOL)animated 
{ 
    [super setEditing:editing animated:animated]; 

    [self setNeedsLayout]; 
} 

這是一個你不叫super罕見的覆蓋方法。

無關 - 在您的表格視圖代碼中,請勿使用isEqual:來比較兩個表格視圖,請使用==

if (tableView == self.tableView) { 

你確實想看看它們是否是相同的指針。

+1

謝謝@rmaddy ...這完美的作品!我在這個超級小姐的實施中的監督!再次感謝 - 我會在最短的時間之後很快接受答案!也要感謝等比較器的提示! – vikram17000

0

來源:Custom edit view in UITableViewCell while swipe left. Objective-C or Swift

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Clona" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ 
     //insert your editAction here 
    }]; 
    editAction.backgroundColor = [UIColor blueColor]; 

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ 
     //insert your deleteAction here 
    }]; 
    deleteAction.backgroundColor = [UIColor redColor]; 
    return @[deleteAction,editAction]; 
} 
+0

Thanks @ user6837640 ...這確實會顯示編輯操作,但僅在左側滑動?我希望+和 - 按鈕始終在表格設置爲編輯時出現。基本上就像iOS上的編輯聯繫人視圖...或者我錯過了什麼請... ...? – vikram17000

+0

這不是正確的解決方案。 – rmaddy

相關問題