iphone - UIButton 标记未在 6 个单元格后分配 indexPath.row

标签 iphone objective-c ios

我这里遇到了一个奇怪的问题。我有一个 TableView,我将 indexPath.row 分配给 UIButton 的标签,这样我就可以将该标签作为参数传递给 segue,并将对象提供给下一个 View Controller 。一切正常,直到我经过 TableView 中的 6 个单元格,当按下按钮时从数组中传递了错误的对象。我决定放入一些 NSLog 来查看发生了什么,我发现结果很奇怪。分配 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 值,以便在单击按钮时我可以从我的数组中获取一个对象?

感谢您的帮助。
问候,
迈克

附言我是一个完全的业余爱好者,所以如果你能用 Lehmann 的术语解释一下,我将不胜感激。

编辑:这是我实际根据要求将标签分配给按钮的代码。

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

最佳答案

看起来您想要做的是在单元格中放置按钮,然后知道在按钮操作发生时按下了哪个单元格的按钮。使用标签是一个合理的想法,但很难实现,因为当单元格被重用时标签需要更改(并最终位于不同的索引路径)。

这是一个更好的方法:使用常量标签(或不使用标签)添加按钮,然后在点击操作上执行此操作...

- (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);
}

关于iphone - UIButton 标记未在 6 个单元格后分配 indexPath.row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13612939/

相关文章:

iphone - 如何在iphone项目中包含FFMpeg库

iphone - iPhone上的CDMA单元数据中断。 NSError代码?

ios - 我如何检测第 3 次应用程序启动

ios - @"/dev/null"是设备上的有效路径吗?

ios - 如何摆脱表格单元格的选择样式,但仍然接收 setSelected/设置突出显示的单元格

iphone - 如何创建一个完整的 NSDate 对象来表示下一个指定的日期时间?

iphone - 如何处理包含属性的 Objective-C 协议(protocol)?

iphone - GKSession发送数据到所有Peers :withDataMode:error: is asynchronous - how to get news of when it's done?

objective-c - 以编程方式创建 cocoa 应用程序的初始窗口(OS X)

ios - MapBox iOS SDK 框架