gpt4 book ai didi

ios - dequeReusableCellWIthIdentifier : loads cell that was previously deleted by the user

转载 作者:行者123 更新时间:2023-11-28 22:00:22 24 4
gpt4 key购买 nike

我正在编写一个小提醒应用程序。当用户尝试设置新提醒时,将显示一个 UITableView,其中不同的部分代表用户编辑的提醒的不同方面。其中一个部分我想要动态行,以便用户可以单击第一个单元格中的加号,另一个单元格将出现在它下面。一旦发生这种情况,红色的 x 符号将替换第一个单元格中的加号,因此用户也可以删除该单元格。我遇到的问题是,当删除一个单元格时,我正在调用 -deleteAtIndexPaths:withRowAnimation: 但似乎该单元格只是被放入可重用队列中,实际上并没有被释放。我知道这一点是因为当一个单元格被删除但用户再次单击加号时,该单元格中的旧信息(包括红色 x 符号)位于来自 -dequeReusableCellWIthIdentifier: 的新单元格中.当我想让它解除分配时,我想不出一种方法来阻止它排队。这是我的 tableView:cellForRowAtIndexPath: 与我正在谈论的单元格有关。

static NSString *identifier2 = @"addTime";
ReminderAddTimeTableViewCell *cell2 = [self.tableView dequeueReusableCellWithIdentifier:identifier2];
if (cell2 == nil) {
NSArray *cellnib = [[NSBundle mainBundle] loadNibNamed:@"ReminderAddTimeTableViewCell" owner:self options:nil];
cell2 = (ReminderAddTimeTableViewCell *)[cellnib objectAtIndex:0];

[cell2.extra addTarget:self action:@selector(addTimeCell:) forControlEvents:UIControlEventTouchUpInside];
cell2.extra.tag = indexPath.row;
cell2.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell2;

这里是通过 IBAction 方法对应的添加和删除单元格:

-(IBAction)addTimeCell:(UIButton*)sender{
timeCellCount ++;
NSIndexPath *path = [NSIndexPath indexPathForRow:sender.tag+1 inSection:1];
NSArray *array = [NSArray arrayWithObjects:path,nil];
[self.tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationRight];
[sender setImage:[UIImage imageNamed:@"red-x.png"] forState:UIControlStateNormal];
[sender removeTarget:self action:@selector(addTimeCell:) forControlEvents:UIControlEventTouchUpInside];
[sender addTarget:self action:@selector(removeTimeCell:) forControlEvents:UIControlEventTouchUpInside];}

-(IBAction)removeTimeCell:(UIButton*)sender{
for(int i = sender.tag+1; i <= timeCellCount; i++){
((ReminderAddTimeTableViewCell*)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]]).extra.tag --;
}
timeCellCount --;
[sender setImage:[UIImage imageNamed:@"green-plus.png"] forState:UIControlStateNormal];
[sender removeTarget:self action:@selector(removeTimeCell:) forControlEvents:UIControlEventTouchUpInside];
[sender addTarget:self action:@selector(addTimeCell:) forControlEvents:UIControlEventTouchUpInside];
NSIndexPath *path = [NSIndexPath indexPathForRow:sender.tag inSection:1];
NSArray *array = [NSArray arrayWithObjects:path,nil];
[self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationRight];
}

我已经标记了按钮,所以我知道我正在处理的是哪个单元格,并设置了一个名为 timeCellCount 的全局变量来保存单元格总数。在此先感谢您的帮助!

最佳答案

您需要重置单元格,而不是删除它。通过覆盖 prepareForReuse 方法更新它的 View 。

来自official documentation :

Prepares a reusable cell for reuse by the table view's delegate.

- (void)prepareForReuse

If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:. For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.

Availability Available in iOS 2.0 and later.

关于ios - dequeReusableCellWIthIdentifier : loads cell that was previously deleted by the user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25312674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com