开发者

Calling an instance of a class by its tag

开发者 https://www.devze.com 2023-01-06 15:11 出处:网络
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 w

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 开发者_StackOverflow社区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.


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号