Reputation: 243
I have an array of 37 objects and this objects has to be listed in a tableview cell. And for each cell I have created custom buttons. So 37 buttons. For each button I have given a image just as check box. If a button is selected the image changes. Now i want to know which button in which cell is clicked.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell..
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(34, 4, 300, 30)];
label.text=[categoryarray objectAtIndex:[indexPath row]];
[cell.contentView addSubview:label];
UIButton *cellbutton=[[UIButton alloc]initWithFrame:CGRectMake(0, 10, 20, 20)];
cellbutton.tag=[indexPath row];
[cellbutton setBackgroundImage:[UIImage imageNamed:@"bfrtick.png"] forState:UIControlStateNormal];
[cellbutton addTarget:self action:@selector(button1Tapped:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cellbutton];
return cell;
}
Upvotes: 1
Views: 1425
Reputation:
In cellforrowatindexpath,
button.tag = indexpath.row
button.addTarget(self, action: Selector("FindTag:"), forControlEvents: UIControlEvents.TouchUpOutside)
And in the targetmethod
@IBAction func FindTag(sender: UIButton) {
let buttontag = sender.tag // it is the row where button is
//Now you know which row contains the button.
}
Upvotes: 2
Reputation: 42588
From the button, find the cell that contains the button. From the cell, you can get the index path. From the index path you can get the array index. This will work without having to worry about maintaining tags.
- (IBAction)button1Tapped:(UIButton *)button
{
UIView *view = button;
while (view && ![view isKindOfClass:[UITableViewCell class]]) {
view = view.superview;
}
if (!view) {
return; // The button was not in a cell
}
UITableViewCell *cell = (UITableViewCell *)view;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (!indexPath) {
return; // The cell was not in the table view
}
NSInteger arrayIndex = indexPath.row;
…
}
By the way, your code has an issue. When you dequeue a reused cell, it will already have a label and a button on it.
You're code will just keep layering labels and buttons on top of the existing buttons and labels. This will cause issues.
Upvotes: 1
Reputation: 8154
Since you already have tagged your button, in the target, you can use the sender to identify the button again, e.g.
-(void) onButtonPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(@"%d", [button tag]);
}
Upvotes: 1
Reputation: 8745
You can set a tag for your button
and in touchUpInside handler you can get this tag and get all the data from your data source by this tag considering that tag value is your index value.
For example: In your tableviewCellForRowAtIndexPath method:
button.tag = [indexpath row]; //assign current index value to your button tag
then on your touchUpInside handler
- (IBAction)buttonTuouchedUpInside:(id)sender {
UIButton *button = (UIButton*)sender; // convert sender to UIButton
NSInteger index = button.tag; // get button tag which is equal to button's row index
NSObject *myDataEntry = [myDataArray objectAtIndex:index]
//do something with this data
}
Upvotes: 0
Reputation: 7948
Create an NSMutableDictionary or NSMutableArray which will contain indexPath row (cellbutton.tag) and after you can process that as you wish. It would be the best if you have id for each cell object, but this will work as well.
Also, keep in mind that every cell is reusable and you have to check if specific button exists in array. Otherwise, you could have inconsistent image shown.
Upvotes: 0