I have made a table and contents some values, I have a button coding like
cell.textLabel.textColor = [UIColor whiteColor];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setBackgroundImage:[UIImage imageNamed:@"edit_button.png"] forState:UIControlStateNormal];
[btn setFr开发者_JAVA技巧ame:CGRectMake(290, 15, 15, 15)];
[btn addTarget:self action:@selector(editTable:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
NSString *cellValue = [myArrayNew objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
I want to click that button and take values from that particular index,
like
Ali (button here) --------index 0 Sajid (button here) --------index 1 Robert (button here) --------index 2 RaM (button here) --------index 3
Theser are cells of table, now how can I get index as I click that button?
if you are not getting my question, you can ask me again...
I found my answer,
Just write the following coding inside the button targeted method and use clickButtonPath as your index path. It is working perfectly, simply
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
btn.tag = indexPath.row;
Then in (void)editTable:(id)sender
:
int row = (UIButton *)btn.tag;
you would use something like this.
- (IBAction)editTable:(UIButton *)sender {
UIView *contentView = [sender superView];
UITableViewCell *cell = [contentView superView];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
But first you should make sure to properly reuse your cells. By changing your code like this:
cell = ... dequeue cell ...
if (cell == nil) {
cell = ... create new cell ...
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = 194;
[btn setBackgroundColor:[UIColor clearColor]];
[btn setBackgroundImage:[UIImage imageNamed:@"edit_button.png"] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(290, 15, 15, 15)];
[btn addTarget:self action:@selector(editTable:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
}
// if you need to change something for this button you can get it like this:
//UIButton *btn= [cell.contentView viewWithTag:194];
NSString *cellValue = [myArrayNew objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
精彩评论