Objective c RowAtIndex路径的高度调用太早
我目前通过Json获取Twitter提要,在我获取推文长度之前,HeightForRowatineXpath被调用。因此,加载heightForRowAtIndexPath时,fullTweet.length始终为零。我正试图像这样调整单元格的大小,这样我就不会浪费任何额外的空格Objective c RowAtIndex路径的高度调用太早,objective-c,uitableview,twitter,height,cells,Objective C,Uitableview,Twitter,Height,Cells,我目前通过Json获取Twitter提要,在我获取推文长度之前,HeightForRowatineXpath被调用。因此,加载heightForRowAtIndexPath时,fullTweet.length始终为零。我正试图像这样调整单元格的大小,这样我就不会浪费任何额外的空格 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if(fullTweet
- (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;
}
有什么想法吗?当您收到数据时,只需调用您的
UITableView
。这将强制表视图重新加载所有单元格。您似乎试图使用实例变量fullTweet
将单元格文本从cellforrowatinexpath
传递到heightforrowatinexpath
这无法工作,因为首先为所有单元格调用heightforrowatinexpath
,然后
为可见的单元格调用cellforrowatinexpath
因此,heightforrowatinexpath
应该从数据源获取信息,
比如:
- (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;
}
}
-(CGFloat)tableView:(UITableView*)tableView rowatinexpath的高度:(nsindepath*)indepath
{
NSDictionary*tweet=[tweets objectAtIndex:indexath.row];
NSString*text=[tweet objectForKey:@“text”];
如果([文本长度]