2014-02-22 102 views
0

我添加了一個UILabel作爲我的tableview的內容視圖。 UILabel中的文本在滾動時重疊。以下是代碼。UITableViewCell - 滾動時ContentView重疊

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.chatTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Group"]; 
    static NSString *CellIdentifier = @"Group"; 

    UITableViewCell *cell = [self.chatTableView dequeueReusableCellWithIdentifier:CellIdentifier 
                    forIndexPath:indexPath]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = self.latestTrimText; 

    UILabel *cellLabel = [[UILabel alloc]init]; 
    cellLabel.text = self.dateOfLatestTrim; 
    cellLabel.frame = CGRectMake(15, 0, 150, 30); 
    [cell.contentView addSubview:cellLabel]; 

    return cell; 
} 

我可以通過改變UITableViewCell *cell = [self.chatTableView dequeueReusableCellWithIdentifier:nil];

但後來解決這個問題,實現代碼如下的滾動不會一帆風順。是否有另一種方法來解決這個問題?

回答

0

tableview單元格被回收並可能重複使用無限次。這是重用標識符所代表的意思。

小心不要在if(cell == nil)塊之外添加任何子視圖。現在,您只需向同一個單元實例添加一個新標籤即可。這就是爲什麼你的文本互相重疊。

我建議您閱讀有關細胞如何回收和重複使用的文檔。

您的修復是不正確的,因爲通過傳遞零重用ID,您只是阻止回收過程發揮作用。你爲tableview的每一行創建一個新的實例。這就是爲什麼你有性能問題。