删除tableview中的行后更新标记

use*_*043 0 uitableview ios

好的,所以我的问题是我在点击按钮时删除了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;
}
Run Code Online (Sandbox Code Playgroud)

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

UIButton *tempButton = sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:tempButton.tag inSection:0];
[_questionsArray removeObjectAtIndex:tempButton.tag];
[_dataTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[_dataTable reloadData];
Run Code Online (Sandbox Code Playgroud)

我也尝试过beginUpdates和endUpdates而不是reloadData,但没有任何效果......

所以,当我测试它时,我有两行...最初他们的标签设置正确 - 0和1 ...但是当我然后删除第0行,并检查新第一行的标签时,它仍然有标签= 1,当它现在应该是0 ...这几乎就像删除后没有更新按钮的标签...

Dhr*_*oel 6

解决方案1:使用:

[_dataTable reloadData];
Run Code Online (Sandbox Code Playgroud)

重置标记,因为再次调用cellForRowAtIndexPath

解决方案2:使用:

[_dataTable beginUpdates];
[_dataTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[_dataTable endUpdates];
Run Code Online (Sandbox Code Playgroud)

它还将调用cellForRowAtIndexPath并更新标记