我目前正在通过Json获取Twitter feed,在我可以获取tweet的长度之前,heightForRowAtIndexPath已经被调用了。因此,当heightForRowAtIndexPath加载时,fullTweet.length始终为零。我正在尝试像这样调整单元格的大小,这样我就不会浪费任何额外的空格。
- (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;
}有什么想法吗?
发布于 2013-06-02 05:06:22
您似乎尝试使用实例变量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;
}
}发布于 2013-06-02 05:06:21
当您收到数据时,只需在UITableView上调用reloadData即可。这将强制表格视图重新加载所有单元格。
https://stackoverflow.com/questions/16877189
复制相似问题