中的行后更新标记

中的行后更新标记

本文介绍了删除 tableview 中的行后更新标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我的问题是我在单击按钮时删除 tableview 中的行,并且我通过获取发件人的标记并将其从我的数据源数组中删除并删除要删除的行来标识要删除的行表中的行...

Okay, so my issue is that I'm deleting rows in a tableview upon a click on a button, and I'm identifying the row to delete by getting the sender's tag and removing it from my data source array and deleting the row from the table...

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

In cellForRowAtIndexPath (relevant part is deleteBtn): *BELOW IS THE FIXED AND WORKING CODE

- (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;
}

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

And in the delete method I then do this:

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

I've also tried with beginUpdates and endUpdates instead of reloadData, but none have any effect...

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

So, when I'm testing it, I have two rows... Initially their tag's get set correctly - 0 and 1... But when I then delete row 0, and check the tag for the new first row, it still has the tag = 1, when it now should be 0... It's almost like it doesn't update the button's tag after the deletion...

推荐答案

解决方案 1:使用:

[_dataTable reloadData];

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

to reset the tags, as cellForRowAtIndexPath is called again

解决方案2:使用:

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

这也将调用 cellForRowAtIndexPath 并更新标签

which will also call cellForRowAtIndexPath and update the tags

这篇关于删除 tableview 中的行后更新标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:52