【发布时间】:2012-11-28 19:31:32
【问题描述】:
我在这里遇到了一个奇怪的问题。我有一个 TableView 并将 indexPath.row 分配给 UIButton 的标签,这样我就可以将该标签作为参数传递给 segue 并将对象提供给下一个视图控制器。一切正常,直到我通过 TableView 中的 6 个单元格,当按下按钮时,从数组中传递了错误的对象。我决定放入一些 NSLogs 来看看发生了什么,我发现结果很奇怪。赋值buttonForApplyingTag.tag = indexPath.row; 发生在NSLogs 之后,是return cell; 之前的最后一条语句。由于 indexPath.row 的实际值是正确的,因此数组中的对象正在正确加载。这只是导致问题的按钮。这是我的输出:
2012-11-28 19:01:57.596 IndexPath.row: 0
2012-11-28 19:01:57.596 IndexPath.Row from Tag: 0
2012-11-28 19:01:57.597 SearchResults Count: 100
2012-11-28 19:01:57.598 IndexPath.row: 1
2012-11-28 19:01:57.598 IndexPath.Row from Tag: 1
2012-11-28 19:01:57.598 SearchResults Count: 100
2012-11-28 19:01:57.599 IndexPath.row: 2
2012-11-28 19:01:57.600 IndexPath.Row from Tag: 2
2012-11-28 19:01:57.600 SearchResults Count: 100
2012-11-28 19:01:57.601 IndexPath.row: 3
2012-11-28 19:01:57.601 IndexPath.Row from Tag: 3
2012-11-28 19:01:57.602 SearchResults Count: 100
2012-11-28 19:01:57.602 IndexPath.row: 4
2012-11-28 19:01:57.603 IndexPath.Row from Tag: 4
2012-11-28 19:01:57.603 SearchResults Count: 100
2012-11-28 19:01:57.604 IndexPath.row: 5
2012-11-28 19:01:57.605 IndexPath.Row from Tag: 5
2012-11-28 19:01:57.605 SearchResults Count: 100
2012-11-28 19:02:02.004 IndexPath.row: 6
2012-11-28 19:02:02.004 IndexPath.Row from Tag: 6
2012-11-28 19:02:02.005 SearchResults Count: 100
2012-11-28 19:02:03.993 IndexPath.row: 7
2012-11-28 19:02:03.993 IndexPath.Row from Tag: 0
2012-11-28 19:02:03.993 SearchResults Count: 100
2012-11-28 19:02:17.846 IndexPath.row: 8
2012-11-28 19:02:17.846 IndexPath.Row from Tag: 0
2012-11-28 19:02:17.846 SearchResults Count: 100
2012-11-28 19:02:18.482 IndexPath.row: 9
2012-11-28 19:02:18.482 IndexPath.Row from Tag: 0
2012-11-28 19:02:18.482 SearchResults Count: 100
很抱歉行数过多,但您明白了。这种趋势一直持续到 100,这意味着按钮没有收到正确的标签。更奇怪的是,当在带有错误 indexPath.row 标记的单元格上单击按钮时,显然又组成了另一个标记,并且我得到了一个低值标记。此外,当单元格 6 部分出现在 TableView 的底部时,即使很明显单元格已实际加载,NSLogs 还没有发布到调试器,因此按钮失败并返回标签 5。所以这种做事方式实际上存在 3 个问题。
真的,我想问的是为什么会发生这种奇怪的行为,我该如何解决?其次,由于这似乎是一场灾难,是否有更好的方法来传递 indexPath.row 值,以便在单击按钮时从数组中获取对象?
感谢您的帮助。
问候,
迈克
附:我是一个完全的业余爱好者,所以如果你能用莱曼的话解释一下,我将不胜感激。
编辑:这是我根据要求将标签实际分配给按钮的代码。
UIButton *buttonForApplyingTag = (UIButton *)[cell viewWithTag:1004];
NSLog(@"IndexPath.row: %d", indexPath.row);
NSLog(@"IndexPath.Row from Tag: %d", buttonForApplyingTag.tag);
NSLog(@"SearchResults Count: %d", [searchResults count]);
buttonForApplyingTag.tag = indexPath.row;
return cell;
编辑 2:这是整个 cellForRowAtIndexPath 代码:
if (isLoading) {
return [tableView dequeueReusableCellWithIdentifier:LoadingCellIdentifier];
} else if ([searchResults count] == 0) {
return [tableView dequeueReusableCellWithIdentifier:NothingFoundCellIdentifier];
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SearchResultCellIdentifier];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[tableView setSeparatorColor:[UIColor grayColor]];
/* Then we do a load of assignments for labels in the cell, then we reach the tagging code. */
SearchResultCellIdentifier来自这个:static NSString *const SearchResultCellIdentifier = @"SearchResultCellProto";
【问题讨论】:
-
你能把按钮添加到单元格的代码贴出来吗?
-
是的。我将进行编辑。
-
您是否在代码的
if (cell == nil)部分设置标签。如果是,请将其移出。 -
好的。现在看到你的更新了。在这种情况下,请在设置标签时检查
buttonForApplyingTag是否为 nil。 -
也许张贴整个 cellForRowAtIndexPath:有证据表明细胞重用存在问题。
标签: iphone objective-c ios