UIButton标记没有被分配6个单元格后的indexPath.row

我有一个奇怪的问题在这里。 我有一个TableView,我将indexPath.row分配给一个UIButton的标签,所以我可以将该标签作为parameter passing给一个segue,并将对象给予下一个视图控制器。 一切工作hunky dory,直到我超过了6个单元格在TableView中,当按下button时,从数组中通过错误的对象。 我决定join一些NSLog来看看发生了什么,我发现结果很奇怪。 赋值buttonForApplyingTag.tag = indexPath.row; 发生在NSLogs之后,并且是return cell;之前的最后一个语句return cell; 。 由于indexPath.row的实际值是正确的,因此数组中的对象正在被正确加载。 这只是造成问题的button。 这是我的输出:

 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,这意味着button没有收到正确的标签。 即使是更奇怪的事实是,当button单击一个错误的indexPath.row标签单元格时,另一个标签显然是组成,我得到一个低价值的标签。 此外,当单元格6部分出现在TableView的底部时,即使已经清楚单元格实际加载了,NSLogs还没有被发布到debugging器,因此button失败并返回一个5的标签。实际上这样做有3个问题。

真的,我想问的是为什么这种奇怪的行为发生,我该如何解决? 其次,由于这只是一个灾难,有没有更好的方式来传递indexPath.row值,所以当button被点击时,我可以从我的数组中获得一个对象?

谢谢你的帮助。
问候,
麦克风

PS我是一个完整的业余爱好者,所以如果你能用莱曼的话来解释,我会很感激的。

编辑:这里的代码,我真的把标签分配给button,按要求。

  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";

它看起来像你想要做的是在单元格中有button,然后知道button操作发生时按下了哪个单元的button。 使用标签是一个合理的想法,但很难实现,因为标签需要改变,当细胞被重用(并在不同的索引path结束)。

这里有一个更好的方法:使用常量标签(或没有标签)添加button,然后在点击操作执行此操作…

 - (IBAction)pressedButtonInMyCell:(id)sender { UIButton *button = (UIButton *)sender; // find the cell that contains the button, this might be one or two levels // up depending on how you created the button (one level in code, two in IB, probably) UIView *view = button.superview; while (view && ![view isKindOfClass:[UITableViewCell self]]) view = view.superview; UITableViewCell *cell = (UITableViewCell *)view; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; NSLog(@"cell is in section %d, row %d", indexPath.section, indexPath.row); } 

这一行从单元格获取button:

 UIButton *buttonForApplyingTag = (UIButton *)[cell viewWithTag:1004]; 

但是,在重复使用的单元格中,标签不是1004.实际上是之前使用单元格时设置的标签。 所以buttonForApplyingTag将可能在重用单元上为零,整个系统失败。

最好创build一个子类,并将UIButton附加到一个实例variables上。

子类:

创build一个新的UITableViewCell子类,我们称之为CustomCellView 。 确保你在cellForRowAtIndexPath而不是常规的UITableViewCell实例化这个类。 为它添加一个实例variables,名为theButton 。 然后创build并分配buttonForApplyingTag到这个实例variables。 最好在你的子类的init中这样做。 然后在cellForRowAtIndexPath ,你不必创build一个button,你只需使用cell.theButton

但是,更好的办法是将“标签”存储在子类的实例variables整数中。