Reputation: 7560
I got a hard job to be done. I try to attach additional information to a UITableViewCell. The tableViewCells got buttons inside that I subclassed to do that (I'm passing a NSString).
Now, when the user clicks on a tableRow, I want to fire an other method and want to pass the same string. I created a subclass of UITableViewCell and added the string to it.
But I can't get the value of the string in tableView:didSelectRowAtIndexPath:
.
That's because UITableView returns an UITableViewCell and not my custom subclass:
[tableView cellForRowAtIndexPath:indexPath].passString
→ /* Property 'passString not found on object of type UITableViewCell */
When I do NSLog(@"%@", [tableView cellForRowAtIndexPath:indexPath]);
I can see the property on the log.
Now my question: How can I get this subclass property in other methods?!
I don't want to subclass UITableView as well. I'd end up subclassing like every UIElement I'm using :'(
Thanks for help, greetings Julian
Upvotes: 2
Views: 279
Reputation: 7560
Thanks for answering! I got it working.
For the first 30 minutest I just didn't know, what you'v meant and wrote Source for trash… :))
Upvotes: 1
Reputation: 39988
Jullian just type cast UITableViewCell
to your custom class like
CustomCell *cell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
cell. passString; //it will work
or you can pass message instead of dot operator
[[tableView cellForRowAtIndexPath:indexPath] passString];
it will give you warning but will work if it is the object of you CustomCell. Prefer using first method as I don't like warnings in my code.
Upvotes: 3