UITableview indexPath.row在iPhone中返回错误的值?

时间:2014-03-15 05:11:18

标签: ios iphone

我想从uitableview获取一个选定的索引路径。当我选择0,1,2时它返回正确的值但是当我选择第3行时它将行索引返回为0(第3行仅在我滚动时显示请问我这个问题的解决方案? 当用户点击uitableviewcell

中的按钮时,我得到表格视图ID
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    UIButton *btnReadmor=(UIButton *)[cell viewWithTag:1];

    [btnReadmor addTarget:self
                   action:@selector(funReadmore:)
         forControlEvents:UIControlEventTouchUpInside];
    [btnReadmor setTag:[indexPath row]];

    return cell;
}

- (IBAction)funReadmore:(id)sender
{


       UIButton *readMoreButton = (UIButton *)sender;
        NSLog(@"Select %d",[readMoreButton tag]);

}

以下是输出:

2014-03-15 10:58:43.765 BGrowB2B_28.1.14[3151:70b] Select 0
2014-03-15 10:58:46.701 BGrowB2B_28.1.14[3151:70b] Select 1
2014-03-15 10:58:48.693 BGrowB2B_28.1.14[3151:70b] Select 1
2014-03-15 10:58:52.013 BGrowB2B_28.1.14[3151:70b] Select 3
2014-03-15 10:58:53.668 BGrowB2B_28.1.14[3151:70b] Select 1
2014-03-15 10:58:55.476 BGrowB2B_28.1.14[3151:70b] Select 4

1 个答案:

答案 0 :(得分:0)

让我们检查您是否在表格中使用多个部分。如果您使用两个部分。您还必须添加上一节的行数。

示例:

if (indexPath.section == 1)
{
    NSLog (@"Row :%d", indexPath.row + section0.count);

    [btnReadmor setTag:indexPath.row + section0.count];
}

谢谢!