views:

21

answers:

2

I have several instances of a UIControl class Foo being instantiated, one instance corresponding to each cell in a UITableView. Each foo toggles the image on the left side of the cell via a selector when the image is touched (checkmark, no checkmark). I've also assigned each instance a tag:

foo.tag = indexPath.row;

The center of the cell has the name of a person so that if the image on the row is touched, that person is "chosen". The right side of the cell has a detail disclosure button to get details on the person.

All of the above works fine.

The center portion of the cell doesn't do anything now, but I would like it to do the same as if the image on the row were touched. It turns out that it's not always obvious that the image has to be touched to chose a person.

My didSelectRowAtIndexPath is now empty. My thinking was in didSelectRowAtIndexPath to call the selector corresponding to the image on the row (the instance of the image toggle class) by its tag.

Does this make sense to do and if so how would I do it?

Thanks.

A: 

The DataSource for the table view should hold all the actual instances - why not just get the instance using the index and the data source?

psychotik
The datasource just has the names of the people. In the cellForRowAtIndexPath, I instantiate one foo for each row, assigning it a tag and a selector, so the instances are not really stored as part of the datasource
Matt Winters
+1  A: 

Set the tag to be the same in each cell.

foo.tag = kFooTag;

In didSelectRow, get the cell and then get foo from the cell.

-(void) tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)path {
  UITableViewCell *cell = [table cellForRowAtIndexPath:path];
  UIView *foo = [cell viewWithTag:kFooTag];
  // do something with foo
}

If you added foo to cell.contentView then call viewWithTag on that.

drawnonward
Very nice! Works perfectly. Thanks. I just call the toggle method: [foo toggleImage]; and that's that. I get a warning (which is understandable): warning: 'UIView' may not respond to '-toggleImage:', but do you know of any way to eliminate it? Thanks.
Matt Winters
viewWithTag returns a `UIView *` but you can typecast it to whatever type foo has, or just use an `id` type.
drawnonward
yes, my Class is called ToggleImageControl, so:[(ToggleImageControl *)foo toggleImage]; eliminated the warning. thanks again.
Matt Winters