2014-11-06 58 views
0

我參與了TableView閃爍問題。當我通過loadmore在tableview中追加數據時,TableView閃爍。如果我使用這個代碼,那麼一切都很好。TableView閃爍在更多的數據追加在IOS

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return 400; 
} 

但我正在計算每一行的高度。因爲每個行高都不相同。然後,我使用該代碼,應用該代碼後,My TableView閃爍。

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

if(_buddyWallSection) 
    temp = arrayBuddyList; 
else 
    temp = arrayMyWallList; 

CGFloat height = 320; 
if([[temp objectAtIndex:indexPath.row/2] isKindOfClass:[NSString class]]) 
{ 
    return height; 
} 
CGSize maximumLabelSize = CGSizeMake(280,9999); 
GolferWall *wallComnt = [temp objectAtIndex:indexPath.row/2]; 
CGSize expectedSize = [[self plainTextFromHTMLText:wallComnt.contentTextHTML]sizeWithFont:[UIFont fontWithName:FontNameArial size:15] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping]; 
height = expectedSize.height; 

NSLog(@"expected size %f", height); 

if(wallComnt.imageNames.count == 0) 
    return 130 + (height); 

if (height < 100) 
{ 
    height = 350; 
} 
else 
    height = height * 2 ; 

return height; 
} 

可能請您檢查我缺少的東西嗎?

謝謝

+0

正如你所看到的,調用該方法在你的UITableView每一行和要執行的CPU密集型任務的Wi我們可以通過下面的例子來說明CGSize expectedSize = [[self plainTextFromHTMLText:wallComnt.contentTextHTML] sizeWithFont:[UIFont fontWithName:FontNameArial size:15] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping]; 所以我的建議是預先評估每行的大小並將該信息存儲在可從tableview訪問的對象中:heightForRowAtIndexPath: – Lolloz89 2014-11-06 08:42:24

+0

您是否偶然實現了willDisplayCell方法? – ZeMoon 2014-11-06 08:43:48

+0

@ Lolloz89是的,所以它需要時間。我們有另一種解決方法嗎? – 2014-11-06 08:44:47

回答

0

這裏的問題是,你每次一排大約是要顯示執行CPU密集型任務。更準確地說,問題在於以下行:

CGSize expectedSize = [[self plainTextFromHTMLText:wallComnt.contentTextHTML]sizeWithFont:[UIFont fontWithName:FontNameArial size:15] 
constrainedToSize:maximumLabelSize 
lineBreakMode:NSLineBreakByWordWrapping]; 

解決方案是預先評估該大小,每個對象一次。例如,在您的GolferWall類,你可以添加一個屬性是這樣的:

@property (nonatonic, assign, readonly) CGFloat contentSize; 

而在GolferWall init方法,你可以簡單地複製你的tableView已經寫的代碼:在

- (instancetype) init 
{ 
    self = [super init]; 
    *** your init stuff here *** 
    self.contentSize = [[self plainTextFromHTMLText:self.contentTextHTML]sizeWithFont:[UIFont fontWithName:FontNameArial size:15] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping]; 

    return self; 
} 

然後:heightForRowAtIndexPath的tableView:heightForRowAtIndexPath:只需更換驗證碼:

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

if(_buddyWallSection) 
temp = arrayBuddyList; 
else 
    temp = arrayMyWallList; 

CGFloat height = 320; 
if([[temp objectAtIndex:indexPath.row/2] isKindOfClass:[NSString class]]) 
{ 
    return height; 
} 
CGSize maximumLabelSize = CGSizeMake(280,9999); 
GolferWall *wallComnt = [temp objectAtIndex:indexPath.row/2]; 

// The magic is here 
CGSize expectedSize = wallComnt.contentSize; 
height = expectedSize.height; 

NSLog(@"expected size %f", height); 

if(wallComnt.imageNames.count == 0) 
    return 130 + (height); 

if (height < 100) 
{ 
    height = 350; 
} 
else 
    height = height * 2 ; 

return height; 
}