0

好的,所以我的问题是我在单击按钮时删除表视图中的行,并且我通过获取发件人的标签并将其从我的数据源数组中删除并从桌子...

在 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,但没有任何效果......

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

4

1 回答 1

6

解决方案1:使用:

[_dataTable reloadData];

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

解决方案2:使用:

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

它还将调用 cellForRowAtIndexPath 并更新标签

于 2013-02-04T04:00:01.837 回答