Helpex - Trao đổi & giúp đỡ Đăng nhập
18

I wand to know the content of the cell when returning the heightForRowAtIndexPath. In this function how do I get the cell object?

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

}

If I try this

 [tableView cellForRowAtIndexPath:indexPath]

it fails and goes off in an infinite loop.

Any idea?

18 hữu ích 0 bình luận 29k xem chia sẻ
26

Call self (the delegate) rather than the tableView itself.

id cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
26 hữu ích 5 bình luận chia sẻ
2

Use: func dequeueReusableCellWithIdentifier(identifier: String) -> AnyObject? of UITableView.
Don't use: func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> AnyObject
Possible Reason: heightForRowAtIndexPath gets called before cellForRowAtIndexPath and this method uses the index path to perform additional configuration based on the cell’s position in the table view. But since, there is still no cell yet, hence it goes to infinite loop I think.

2 hữu ích 0 bình luận chia sẻ
1

The cell hasn't been created when heightForRowAtIndexPath: is called, so there's no way to "get" a cell. Instead, look at your model and determine the height of the cell that way.

If you're concerned about long strings and needing more vertical space in your cell, consider using sizeWithFont:forWidth:lineBreakMode:. Docs here: https://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html

1 hữu ích 0 bình luận chia sẻ
0

I don't think you can or you should. You'll need to determine from indexPath which data you will be displaying in the corresponding cell and thus calculate the needed height of the cell.

So you'll need to write some code to get from indexPath to the right data in your data model.

0 hữu ích 1 bình luận chia sẻ
-4
uitableviewcell *ce=[self.view viewwithtag:indexPath.row]; 

it returns the cell object.

-4 hữu ích 1 bình luận chia sẻ
loading
Không tìm thấy câu trả lời bạn tìm kiếm? Duyệt qua các câu hỏi được gắn thẻ iphone objective-c ios ipad , hoặc hỏi câu hỏi của bạn.

Có thể bạn quan tâm

loading