Neko Neko - 3 years ago 54
iOS Question

How to get a cell without calling cellForRowAtIndexPath in objective C iOS?

We can get a cell from:

UITableViewCell * cell = [self tableView:tableVie cellForRowAtIndexPath:indexPath]


but it will calling the method
cellForRowAtIndexPath:


if I know the cell's tag or indexPath , it's there a way to get the specified cell without to calling
cellForRowAtIndexPath:
?




Add some reason why I can't call
cellForRowAtIndexPath:indexPath


I want to get the cell in the method cellForHeightAtIndexPath to reset the height of each cell. :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell * cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
CommentsListResponseDataModel * model = self.liveCommentData[indexPath.row -1];

if (model.viewHeight > 0) {
return model.viewHeight;
} else {
if (model.parentComment) {
CommentParentCell * cc = (CommentParentCell *)cell;
return cc.viewHeight;
} else {
CommentSingleCell * cc = (CommentSingleCell *)cell;
return cc.viewHeight;
}
}
}

}


But this will become a dead loop because it all
cellForRowAtIndexPath:indexPath
and
cellForRowAtIndexPath:indexPath
will recall
cellForHeightAtIndexPath:indexPath

Answer Source

As of my understanding, you are trying to get the cell object by calling UITableView datasource method. But when you try to do like that then it will be infinite loop call and you will never get the cell object. So in order to get cell object from UITableView, do the following steps.

  • Declare your UITableView as global to the your ViewController, something like UITableView *_tableView; or @IBOutlet weak UITableView *_tableView;

  • Then try to call the

//specify the item indexpath
NSIndexPath *_indexPath = [NSIndexPath indexPathForItem:3 inSection:1];

//now try to access cell object by passing cell indexPath
UITableViewCell *_cell = [_tableView cellForRowAtIndexPath:_indexPath];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download