[英]heightForRowAtIndexPath being called too early
我目前正在通過Json獲得Twitter提要,並且在獲得tweet的長度之前,將調用heightForRowAtIndexPath。 因此,當heightForRowAtIndexPath加載時,fullTweet.length始終為零。 我正在嘗試調整單元格的大小,例如http://gyazo.com/632d09685268e1737d3c58bf1718cbff.png,這樣我就不會浪費任何多余的空格。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(fullTweet.length >= 50) {
return 50.0f;
} else
return 92.0f;
}
我的方法如何運作
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TweetCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
NSString *text = [tweet objectForKey:@"text"];
cell.textLabel.text = text;
fullTweet = text;
NSLog(@"%i", fullTweet.length);
cell.textLabel.numberOfLines = 3;
return cell;
}
有任何想法嗎 ?
似乎您嘗試使用實例變量fullTweet
將單元格的文本從cellForRowAtIndexPath
傳遞到heightForRowAtIndexPath
。
那是行不通的,因為首先對所有單元 heightForRowAtIndexPath
,然后對可見單元 cellForRowAtIndexPath
。
因此, heightForRowAtIndexPath
應該改為從數據源獲取信息,例如:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
NSString *text = [tweet objectForKey:@"text"];
if ([text length] <= 50) {
return 50.0f;
} else {
return 92.0f;
}
}
收到數據后,只需在UITableView
上調用reloadData
。 這將強制表視圖重新加載所有單元格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.