2013-06-01 3 views
2

저는 현재 Json을 통해 Twitter 피드를 받고 있으며, tweet의 길이를 알기 전에 heightForRowAtIndexPath가 호출되고 있습니다. heightForRowAtIndexPath가로드 될 때 fullTweet.length는 항상 0입니다. 이 여분의 빈 공간을 낭비하지 않도록이 http://gyazo.com/632d09685268e1737d3c58bf1718cbff.png 같은 셀의 크기를 조정하려고합니다.heightForRowAtIndexPath가 너무 일찍 호출되고 있습니다.

- (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; 
} 

모든 아이디어를 어떻게 작동 하는가?

답변

1

인스턴스 변수 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; 
    } 
} 
0

데이터를 받으면 UITableViewreloadData으로 전화하십시오. 이렇게하면 테이블 뷰가 모든 셀을 다시로드하게됩니다.

관련 문제