0

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

kratos
  • 2,465
  • 2
  • 27
  • 45

2 Answers2

1

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];
}
Community
  • 1
  • 1
user427969
  • 3,836
  • 6
  • 50
  • 75
  • But how does this give you the cell in the `tableView:heightForRowAtIndexPath:` method? That would be needed to access the label from the cell. – rmaddy Oct 29 '12 at 03:42
  • I have updated my answer to some links that might be helpful to access from heightForRowAtIndexPath: method – user427969 Oct 29 '12 at 03:54
0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579