gpt4 book ai didi

ios - UITableViewCell dequeueReusableCellWithIdentifier :forIndexPath: issue

转载 作者:行者123 更新时间:2023-11-29 03:25:33 24 4
gpt4 key购买 nike

我想知道是否有人遇到过我最近试验过的类似问题。

让我进一步描述一下我的问题。我有一个 UITableViewController,我在 IB 中设计了一组自定义 UITableViewCell。每个 UITableViewCell 都有不同的元素,如 UIButton、UITextField 和 UILabel 等。显然,每个 UITableViewCell 都有不同的标识符。

一旦定义了我所有的 UITableViewCells,下一步就是实例化 tableView:cellForRowAtIndexPath: 上的单元格。我在此方法中所做的取决于部分和行,我通过 dequeueReusableCellWithIdentifier:forIndexPath: 实例化不同的 UITableViewCells。

在桌面上创建的一切似乎都完美且正确地工作,但是当我向下滚动时问题就来了。在最顶部,我有一个带有 UIButton 的 UITableViewCell,我已经指定了单击它时要执行的具体操作。向下滚动,有几个具有相同格式的 UITableViewCells(一个 UIButton 里面指定了不同的操作)。

问题是,当单击底部的第一个按钮时,此按钮会执行我在最顶部的 UIButton 上定义的第一个操作及其自己的操作。

似乎当 uitableviewdelegate 创建新单元格或重用它们时,它嵌套了来自其他 indexPath 的功能,而不是指定的 indexPath....

希望我已经正确解释了自己。

提前谢谢你。

[编辑]

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] init];

NSLog(@"indexpath value is: %@", indexPath);

if (indexPath.section == 0)
cell = [self buildSection_0:tableView getIndexPath:indexPath];
else if (indexPath.section == 1)
cell = [self buildSection_1:tableView getIndexPath:indexPath];
else if (indexPath.section == 2)
cell = [self buildSection_2:tableView getIndexPath:indexPath];
else if (indexPath.section == 3)
cell = [self buildSection_3:tableView getIndexPath:indexPath];
else if (indexPath.section == 4)
cell = [self buildSection_4:tableView getIndexPath:indexPath];
return cell;
}

-(UITableViewCell *)buildSection_0:(UITableView *)tableView getIndexPath:(NSIndexPath *)indexPath{
NSString *identifier;
UITableViewCell *cell;

if (indexPath.row == 0){
identifier = @"headerCell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
}
else if (indexPath.row == 1){
identifier = @"buttonCell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
UIButton *button = (UIButton *)[cell viewWithTag:3001];
[button setTitle:@"Scan" forState:UIControlStateNormal];
[button addTarget:self action:@selector(scan) forControlEvents:UIControlEventTouchUpInside];
}

return cell;
}

-(UITableViewCell *)buildSection_3:(UITableView *)tableView getIndexPath:(NSIndexPath *)indexPath{
NSString *identifier;
UITableViewCell *cell;

if (indexPath.row == [[job jobLocations] count]) { // Adding button insert more Locations
identifier = @"buttonCell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
UIButton *button = (UIButton *)[cell viewWithTag:3001];
[button setTitle:@"Add Location" forState:UIControlStateNormal];
[button addTarget:self action:@selector(newLocation) forControlEvents:UIControlEventTouchUpInside];
}
else{ // Showing different Locations
identifier = @"locationCell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:3007];
UITextField *textField = (UITextField *)[cell viewWithTag:3008];

[label setText:[NSString stringWithFormat:@"Location #%ld", (long)indexPath.row + 1]];
[textField setText:[[[job jobLocations] objectAtIndex:indexPath.row] locationType]];
}

return cell;
}

最佳答案

dequeueReusableCellWithIdentifier:forIndexPath 将重用不再可见的单元格,您可能需要在您的单元格上实现 prepareForReuse,或者在您将其出队后重置它。

[编辑]

看到你添加了你的代码,你应该在添加新的之前从按钮中删除以前的目标/操作,因为如果单元格被重复使用,旧的仍然会存在:

[button removeTarget:self action:NULL forControlEvents:UIControlEventTouchUpInside];
[button addTarget:...

关于ios - UITableViewCell dequeueReusableCellWithIdentifier :forIndexPath: issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20500795/

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