I have a UITableViewController that has a UITableView whose cells have a button attached to them via the accessoryView. These buttons can be selected/deselected when the user clicks on them. The table itself is made up from an NSMutableArray of objects that contain multiple NSString parameters, with the “name” parameter being on display in the table.
What I am trying to do now is when the button is selected, to add the corresponding OBJECT that links to the respective "name" parameter to an NSMutableArray. The user is not selecting the cell, but rather, they are clicking on the button that is attached to the cell.
Here is my method that selects/deselects the button in each cell:
-(void)buttonTouched:(id)sender
{
UIButton *btn = (UIButton *)sender;
if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"CheckBox1.png"]])
{
[btn setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal];
// statement to add object to NSMutableArray
}
else
{
[btn setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateNormal];
// statement to remove object from NSMutableArray
}
}
//Here is my code that shows how I initialize my button and cell in my UITableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
testButton = [UIButton buttonWithType:UIButtonTypeCustom];
[testButton setFrame:CGRectMake(280, 57, 25, 25)];
[testButton setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateSelected];
[testButton setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal];
[testButton setUserInteractionEnabled:YES];
[testButton addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[testButton setTag:cell.tag];
[cell setAccessoryView:testButton];
//[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
// Configure the cell...
TestObject *tObject = [[DataModel sharedInstance].testList objectAtIndex:indexPath.row];
cell.textLabel.text = tObject.testTitle;
return cell;
}
My issue is not so much the actual adding of an object to the array, but deriving the respective cell from the button, and then deriving the correct object from that cell.
You need a way to link your cell to your button when you do it like this, you have a few options
1- Set tags on the button to be the corresponding index of the element in your array, that way you can get a hold of the cells through the tag property, this can have issues if you are deleting cells as you have to update your button tags.
2- Use a dictionary to map your buttons to your cells
So if you use the tag to mark the index you can use cellForRowAtIndexPath of UITableViewCell to get a hold of the cell , the data object should be in your datasourcce array with that index too..
Daniel