错误的indexpath.在重新载体之后



我正在尝试获取按钮的 indexPath.row在表格行中单击。

用户单击此按钮时,我会得到 index.row对应于该按钮,但是当我在源数组中添加更多对象以通过调用 reloadData来创建更多单元格时,每个单元格中的 rowButtonClicked不再给我正确indexPath.row例如,我按索引20,现在打印的indexPath.row为9。

cellForRowAtIndexPath中将事件添加到按钮:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  lBtnWithAction = [[UIButton alloc] initWithFrame:CGRectMake(liLight1Xcord + 23, 10, liLight1Width + 5, liLight1Height + 25)];
  lBtnWithAction.tag = ROW_BUTTON_ACTION;
  lBtnWithAction.titleLabel.font = luiFontCheckmark;
  lBtnWithAction.tintColor = [UIColor blackColor];
  lBtnWithAction.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
            [cell.contentView addSubview:lBtnWithAction];
   }
 else 
     { 
       lBtnWithAction = (UIButton *)[cell.contentView viewWithTag:ROW_BUTTON_ACTION];
     }
//Set the tag
lBtnWithAction.tag = indexPath.row;
//Add the click event to the button inside a row
[lBtnWithAction addTarget:self action:@selector(rowButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}

用单击索引做点什么:

-(void)rowButtonClicked:(UIButton*)sender
{
    //Get the index of the clicked button
    NSLog(@"%li", (long)sender.tag);
    [self doSomething:(long)sender.tag];
}

常数

#define ROW_BUTTON_ACTION 9

当我更改表格的初始项目时,为什么它会给出错误的索引?有没有办法解决此问题?

看起来您正在弄乱按钮标签。设置标签

lBtnWithAction.tag = indexPath.row;

您将无法使用

正确获取按钮
lBtnWithAction = (UIButton *)[cell.contentView viewWithTag:ROW_BUTTON_ACTION];

(假设ROW_BUTTON_ACTION是常数)。lBtnWithAction将一直是nil,除非indexPath.row等于ROW_BUTTON_ACTION

我会建议在此子类UITableViewCell,在此处添加一个按钮范围,然后直接参考它而不是通过TAG搜索。在这种情况下,您将可以自由使用标签:) -

@interface UIMyTableViewCell : UITableViewCell
@property (nonatomic, strong, nonnull) UIButton *lBtnWithAction;
@end

,然后在cellForRowAtIndexPath中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    UIMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UIMyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell.lBtnWithAction addTarget:self action:@selector(rowButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    cell.lBtnWithAction.tag = indexPath.row;
    return cell;
}

您可以简单地更新行 -

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

to

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]

我对此的看法将在 UITableViewCell上划分并为其响应提供代表。

代码:

 //MyCoolTablewViewCell.h
 @protocol MyCoolProtocol<NSObject>
 -(void)youTouchedMyCoolCell:(MyCoolTableViewCell __nonnull *)myCoolCell;
 @end
 @interface MyCoolTableViewCell:UITableViewCell
 @property(nonatomic, weak,nullable) id<MyCoolProtocol>myCooldelegate; 
 @end
 //MyCoolTablewViewCell.m
 @interface MyCoolTableViewCell(){
    UIButton *mySuperCoolButton;
 }
 @end
 @implementation MyCoolTablewViewCell
 -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  if(!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])){
    return nil;
 }
 // init your stuff here
 // init button
 mySuperCoolButton = [UIBUtton buttonWithType:UIButtonTypeCustom]; 
 [mySuperCoolButton addTarget:self action:@selector(tappedMyCoolButton:) forControlEvents:UIControlEventTouchUpInside];
 return self;
 }
- (void)tappedMyCoolButton:(id)sender{
  if([_myCoolDelegate respondToSelector:@selector(youTouchedMyCoolCell:)]){
     [_myCoolDelegate youTouchedMyCoolCell:self];
  }
 }
 @end

然后在您的控制器中

// whatEverController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   // get the cell
    cell = [tableView dequeueReusableCellWithIdentifier:@"myCoolIdentifier"forIndexPath:indexPath];
cell.myCooldelegate = self;
}
-(void)youTouchedMyCoolCell:(MyCoolTableViewCell *)myCoolCell{
    NSIndexPath *myCoolIndexPath = [_tableView indexPathForCell:myCoolCell];
  // then do whatever you want with the index path
}

最新更新