使用tag可以识别出哪个按钮在哪个单元格中被点击了。
- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//init identifier
if (cell ==nil)
{
//load nib file
}
buttonName.tag = indexPath.row;
[buttonName addTarget:self action:@selector(showName:) forControlEvents:UIControlEventTouchUpInside];
buttonInfo.tag = indexPath.row;
[buttonInfo addTarget:self action:@selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];
}
}
-(void) showName:(UIButton*)button{
int row = button.tag; //you know that which row button is tapped
}
-(void)showInfo:(UIButton*)button{
int row = button.tag;//you know that which row button is tapped
}
---------------- 编辑 -------------
如果您需要根据row & section了解按下了哪个按钮,您可以在下面尝试。 (在cellForRowAtIndexPath:方法中)
int tag = (indexPath.row+1)+(indexPath.section*100);
buttonName.tag = tag;
当按钮在
行 = 5,部分 = 0,然后标签 = 6。
行 = 4,部分 = 3,然后标签 = 305。
行 = 0,部分 = 11,然后标签 =1101。
限制,行数不能超过 99。不要对其他视图使用正标记。如果您需要使用标签,请尝试否定。 (-1, -9, -100)。
所以从这里,您可以计算 indexPath 的后行和部分。使用这个:
-(NSIndexPath*)getIndexPathFromTag:(NSInteger)tag{
/* To get indexPath from textfeidl tag,
TextField tag set = (indexPath.row +1) + (indexPath.section*100) */
int row =0;
int section =0;
for (int i =100; i<tag; i=i+100) {
section++;
}
row = tag - (section*100);
row-=1;
return [NSIndexPath indexPathForRow:row inSection:section];
}
如何使用:
-(void)showInfo:(UIButton*)button{
NSIndexPath *indexPath = [self getIndexPathFromTag:button.tag];
int row = indexPath.row;
int section = indexPath.section;
}