gpt4 book ai didi

ios - 在 TableView 中删除行后更新标签

转载 作者:行者123 更新时间:2023-11-28 18:23:52 26 4
gpt4 key购买 nike

好的,所以我的问题是我在单击按钮时删除了 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 行并检查新第一行的标签时,它仍然有标签 = 1,而现在它应该是 0...这几乎就像它在删除后没有更新按钮的标签...

最佳答案

解决方案一:使用:

[_dataTable reloadData];

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

方案二:使用:

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

这也会调用 cellForRowAtIndexPath 并更新标签

关于ios - 在 TableView 中删除行后更新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14679930/

26 4 0