【问题标题】:UITableViewCell dequeueReusableCellWithIdentifier:forIndexPath: issueUITableViewCell dequeueReusableCellWithIdentifier:forIndexPath: 问题
【发布时间】:2013-12-10 17:09:09
【问题描述】:

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

让我进一步描述一下我的问题。我有一个 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;
}

【问题讨论】:

  • cellForRowAtIndexPath 的邮政编码

标签: ios uitableview


【解决方案1】:

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

[编辑]

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

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

【讨论】:

  • 您的建议非常适合。非常感谢你。但是,如果这种行为发生在任何其他单元格或元素上,我想这个想法是将值存储在某个地方,并且每次我想绘制不同的单元格或元素时,都会显示这些信息。我说的对吗?
【解决方案2】:

为与indexPath.row 对应的按钮添加标签。然后在您的按钮操作方法中,您可以通过查看(UIButton *)sender 的标签来确定单击了哪个按钮。

【讨论】:

    猜你喜欢
    • 2012-10-21
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多