删除tableview中的行后更新标记

时间:2013-02-04 03:22:30

标签: ios uitableview

好的,我的问题是我在点击按钮时删除了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,但没有任何效果......

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

1 个答案:

答案 0 :(得分:6)

解决方案1:使用:

[_dataTable reloadData];

重置标签,再次调用cellForRowAtIndexPath

解决方案2:使用:

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

也将调用cellForRowAtIndexPath并更新标记