如何获取uitableview ios中viewwithtag的UIButton的索引

我有一个问题:我想在tableview中得到UIButton的索引。 我创build的uitableview每行有2个uibutton,但是当我点击uibutton时,它会得到索引不正确。 代码创buildUITableview:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSLog(@"AllItemsArrayarray: %@", temp); return [temp count]; } 

cellForRowAtIndexPath函数

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = (UITableViewCell *)[_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom]; [market setFrame:CGRectMake(200, 6, 30, 30)]; [market setTag:4000]; [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown]; [cell.contentView addSubview:market]; } UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000]; if([sellingArray count]>0) { NSLog(@"sellingArray %@",sellingArray); if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing { [marketButton setSelected:NO]; [marketButton setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal]; marketButton.enabled = YES; } else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]) // marketplace { [marketButton setSelected:YES]; [marketButton setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal]; marketButton.enabled = YES; } } return cell; } 

marketPressedActionfunction

 - (void)marketPressedAction:(id)sender { buttonPressed = (UIButton *)sender; buttontag = buttonPressed.tag ; NSLog(@"Market button click at row %d",buttontag); } 

当表格有5行时,我点击button,显示错误:

 -[__NSArrayM objectAtIndex:]: index 4000 beyond bounds [0 .. 4]' 

你使用indexpath.row设置标签

  UIButton *market ; if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } market = [UIButton buttonWithType:UIButtonTypeCustom]; [market setFrame:CGRectMake(200, 6, 30, 30)]; [market setTag:indexPath.row]; [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown]; if([sellingArray count]>0) { NSLog(@"sellingArray %@",sellingArray); if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing { [market setSelected:NO]; [market setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal]; market.enabled = YES; } else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]) // marketplace { [market setSelected:YES]; [market setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal]; market.enabled = YES; } [cell.contentView addSubview:market]; 

基于你在问题下面的评论,我想你试图访问你的数组中的项目,基于哪个button在表视图中单击。 首先,你将button的标签设置为4000,然后当你试图访问你的数组中的项目时,点击button标签的索引处,显然它会超出范围,因为你正试图访问该对象在索引4000的数组中,而数组只有5个项目。

要实现这一点,你的button应该有标签等于特定单元格所代表的对象的索引。 所以,而不是

 [market setTag:4000]; 

你应该这样做:

 [market setTag:indexPath.row]; 

然后当你访问你的button的select器方法中的标签时,它将与单元格表示的对象的索引相同。