Reputation: 2495
I am trying to access a label property on a custom UiTableViewCell (subclassed UITableViewCell) from a UitableViewController class. For instance I have the heightForRowAtIndexPath method and I need to get access to the label.
Here is my code:
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// I need access to the custom UITableView Cell property here
}
Any tips? Also is it even possible to declare the outlet in the uiviewcontroller and link it to the label on the custom uitableviewcell?
Thanks
Upvotes: 0
Views: 736
Reputation: 3896
For accessing cell from heightForRowAtIndexPath:
, check following:
If you need to access cell's label after it is initialized then you need to add tag
to the label in the custom UITableViewCell class and you can access the label as following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"cell"];
UILabel *cellLabel = (UILabel *)[cell viewWithTag: 1];
}
Upvotes: 1
Reputation: 318794
The height calculation should be done with data from your data model, not the view. Remember, the cell was created with data from the data model.
You can't call [tv cellForRowAtIndexPath:indexPath]
because this can cause infinite recursion between the tableView:cellForRowAtIndexPath:
and this tableView:heightForRowAtIndexPath:
method.
You can try directly calling your tableView:cellForRowAtIndexPath:
method to get a cell but that may have undesired side effects.
Upvotes: 0