I am using a custom UITableViewCell class. My cell has multiple buttons (4 to be precise) on it and the button clicks are handled in the UIViewController which uses this cell class.
I was trying to use the button's tag to calculate the row number on which the button was clicked. But doing this causes an issue if a cell was not created and instead uses a free object. In that case the tag and the row number do not match.
Can someone please tell me how I can handle this case? If I give the same tag to all buttons in different rows, how can I identify the row on which the button was clicked?
T开发者_运维问答hanks a lot.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
MyTableCell *cell = (MyTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// whatever you have now
}
// Set up the cell...
cell.myListViewController = self;
int tag = indexPath.row;
cell.button1.tag = tag;
cell.button2.tag = tag;
cell.button3.tag = tag;
....
}
This code will have a unique tag for buttons in each row. You are setting the tag not in the new cell creation but for all cases, including reuse.
精彩评论