在tableview中删除行后更新标签

好吧,所以我的问题是,我正在删除行中的一个button上点击一个tableview,我通过获取发件人的标记,并从我的数据源数组中删除它,删除行表…

在cellForRowAtIndexPath(相关部分是deleteBtn): *下面是固定和工作代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"questionCell"; HTQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; if(cell == nil) { cell = [[HTQuestionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } NSDictionary *dict = [NSDictionary dictionary]; dict = _questionsArray[indexPath.row]; NSMutableAttributedString *atbString; NSMutableAttributedString *atbQuestionString; atbString = [[NSMutableAttributedString alloc] initWithString:[dict objectForKey:@"person"] attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:34]}]; atbQuestionString = [[NSMutableAttributedString alloc] initWithString:[dict objectForKey:@"question"] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:34]}]; [atbString appendAttributedString:atbQuestionString]; [cell.questionTxtView setAttributedText:atbString]; [cell.deleteBtn setTag:indexPath.row]; [cell.showBtn setTag:indexPath.row]; NSLog(@"Delete Button : %@", cell.deleteBtn); [cell.deleteBtn addTarget:self action:@selector(deleteMsg:) forControlEvents:UIControlEventTouchUpInside]; [cell.showBtn addTarget:self action:@selector(showMsg:) forControlEvents:UIControlEventTouchUpInside]; return cell; } 

然后在删除方法中,我这样做:

 UIButton *tempButton = sender; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:tempButton.tag inSection:0]; [_questionsArray removeObjectAtIndex:tempButton.tag]; [_dataTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [_dataTable reloadData]; 

我也试过beginUpdates和endUpdates而不是reloadData,但没有任何影响…

所以,当我testing它时,我有两行…最初他们的标签被设置正确 – 0和1 …但是,当我然后删除行0,并检查标签为新的第一行,它仍然有标签= 1,现在应该是0 …这几乎就是它不会更新删除后button的标签…

解决scheme1:使用:

 [_dataTable reloadData]; 

重新设置标签,因为cellForRowAtIndexPath被再次调用

解决scheme2:使用:

 [_dataTable beginUpdates]; [_dataTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [_dataTable endUpdates]; 

这也将调用cellForRowAtIndexPath并更新标签