【问题标题】:Get the right number of the current row (iOS) [duplicate]获取当前行的正确编号(iOS)[重复]
【发布时间】:2014-10-02 20:34:02
【问题描述】:

我正在尝试在我的项目中创建一个“喜欢”按钮。要检测 UITableViewCell 中的当前行 (单击按钮所在的位置),我使用以下代码:

-(void)likeBtnClick:(id)sender {
    UIButton *senderButton = (UIButton *)sender;
    NSLog(@"current Row=%d",senderButton.tag);
}

所以,对于第一个单元格 NSLog 显示

“当前行=203”,第二个-“当前行=200”,第三个-“当前行=197”。 但是第 4 行是“当前行 = 203”再次(第 5 -“当前行 = 200”,第 6 -“当前行 = 197”等)!

同样的错误每 3 行重复一次。

我的问题是 - 如何在 UITableView 中获取当前行的正确编号

更新:

对于 Lyndsey Scott – cellForRowAtIndexPath 方法代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsCell"];
    }
    [self.tableView setScrollsToTop:YES];
    [HUD hide:YES];
    Location *location = [_locations objectAtIndex:indexPath.row];
    UIButton *lkbut = (UIButton*) [cell viewWithTag:300];
    [lkbut setImage:[UIImage imageNamed:@"likehq2.png"] forState:UIControlStateNormal];

    NSInteger value = [location.information intValue];

    NSLog(@"val %ld", (long)value);

    lkbut.tag = value;
    NSLog(@"ur %@", location.user_like);
    [lkbut addTarget:self action:@selector(likeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //...
    return cell;
}

解决方案:

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    NSLog(@"indexPath.row = %ld", (long)indexPath.row);

    NSLog(@"%lu", (unsigned long)[_locations count] - (long)indexPath.row);

"(unsigned long)[_locations count]" - 是您的行数。 //这里每个人都有自己的代码

感谢 Anna、Lyndsey Scott 和 JOM!

【问题讨论】:

  • 查看stackoverflow.com/a/1802875/467105 以获得比标签更好的方法。
  • 我看过,但是像“0x00000016”这样的数字不是我需要的
  • 您如何看待该文本?您如何记录生成的 indexPath?你试过NSLog(@"indexPath.row = %d", indexPath.row);吗?
  • 您能否从您设置标签的 cellForRowAtIndexPath 方法中发布代码?
  • "-(void)likeBtnClick:(id)sender" 不在 "-(UITableViewCell *)tableView" 中,所以 indexPath 没有在那里声明...

标签: ios objective-c xcode uitableview


【解决方案1】:

我建议这样做:

首先:

[lkbut addTarget:self action:@selector(likeBtnClick:) forControlEvents:UIControlEventTouchUpInside];

应该是:

[lkbut addTarget:self action:@selector(likeBtnClick:andEvent:) forControlEvents:UIControlEventTouchUpInside];

您的button 操作方法应该是:

-(void)likeBtnClick:(id)sender andEvent:(UIEvent*)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];

    NSLog(@"current Row=%d",indexPath.row);
}

基本上,不需要遍历所有标签。

【讨论】:

  • “UITableView”没有可见的@interface 声明选择器“indexPathForItemAtPoint:”...
  • @dvijok:哎呀,对不起。 -indexPathForItemAtPoint: 用于collectionView。对于tableView,它应该是-indexPathForRowAtPoint: ...更新答案
【解决方案2】:

您再次获得相同数字的原因是 tableview 单元格正在被回收。在您的情况下,一次屏幕上似乎最多有 3 行。

无论如何,这是我之前对类似问题的回答:iPhone - How to determine in which cell a button was pressed in a custom UITableViewCell

更新:Anna 有一个很好的建议,应该也可以。请检查一下,看看效果是否更好。

第三种方法可能是您在创建/回收单元格时使用 indexPath.row 重置单元格标记。做任何事情总是有不止一种方法;)

【讨论】:

  • 需要小心这个superview方法,因为视图层次结构因iOS版本而异。
  • 是的,我不认同这个方法,但评价是对的……
  • 当我使用此代码时,我从 0 中获取值。但我有大约 200 个单元格(或更多)。那么我怎样才能得到从最高到最低的值呢?
  • @dvijok 不要使用此代码。使用安娜的方法。请注意,您的标签不起作用的原因是:“您再次获得相同数字的原因是 tableview 单元格正在被回收。在您的情况下,一次屏幕上似乎最多有 3 行。”
  • 好吧,我已经明白如何使用安娜的方法了... :) 但我仍然有同样的问题:“当我使用这段代码时,我从 0 中获取值。但我有大约 200 个单元格(或更多)。那么我怎样才能得到从最高到最低的值呢?”
猜你喜欢
  • 2011-03-31
  • 2014-11-08
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
相关资源
最近更新 更多