2013-01-04 39 views
0

是什麼讓UITableView上的滾動變得如此波濤洶涌?在我看來,下面的代碼是一個罪魁禍首。我很難用其他的東西代替這個邏輯。UITableView由於removeFromSuperview而不能平滑滾動

for (UIView *view in cell.contentView.subviews) { 
[view removeFromSuperview]; } 

這就是我正在做的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

for (UIView *view in cell.contentView.subviews) { 
    [view removeFromSuperview]; 
} 

BGMArticleAbstract *articleAbstract = [self.section.articleAbstracts objectAtIndex:indexPath.row]; 
[cell.contentView addSubview:[self getHedlineFromArticleAbstract:articleAbstract]]; 
[cell.contentView addSubview:[self getThumbnailImageFromArticleAbstract:articleAbstract]]; 
[cell.contentView addSubview:[self getAbstractParaFromArticleAbstract:articleAbstract]]; 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

return cell; } 

我在做,因爲我正在創建一個動態單元格高度,因此將contentSubview添加到contentview。有什麼辦法可以使這個滾動視圖工作順利嗎?感謝您的幫助。

+0

您是否嘗試過不重新加載單元格?這樣每次都會給你一個乾淨的標準,每當生成一個新的單元時,你就不必從cell.contentView中刪除所有的子視圖。 – Echihl

回答

3

您應該根據需要設計您的手機。添加標籤和任何你需要的單元格,然後改變這些已經可用的子視圖的內容。
如果您需要顯示圖像,請將一個UIImageView添加到單元格中,並且只更改它的圖像屬性。相同的文本字段等。

你這樣做使內置緩存沒用,因爲你一次又一次地重新生成所有子視圖的方式..

爲了更加提高的表現,你可以自己做電池的繪製。

蘋果有一個相當不錯的示例項目: http://developer.apple.com/library/ios/#samplecode/AdvancedTableViewCells/Introduction/Intro.html

1

你是正確的,問題是你如何恢復細胞引起的。正確的模式如下...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // see if cell contains our image views. A reused cell will, but a new cell won't 
    UIImageView *imageViewA = (UIImageView *)[cell viewWithTag:32]; 
    UIImageView *imageViewB = (UIImageView *)[cell viewWithTag:33]; 
    UIImageView *imageViewC = (UIImageView *)[cell viewWithTag:34]; 

    if (!imageViewA) { 
     // the cell must be new, so create it's image views 
     // you should be able to borrow most of this code from your getHeadline/thumbnail/etc methods. 
     // the good news is that this relatively expensive code runs only for new 
     // cells and there are only a few of those - only enough to fill the visible frame 
     imageViewA = [[UIImageView alloc] initWithFrame:CGRectMake(/* frame it here */)]; 
     [cell.contentView addSubview:imageViewA]; 
     imageViewA.tag = 32; 

     imageViewB = [[UIImageView alloc] initWithFrame:CGRectMake(/* frame it here */)]; 
     [cell.contentView addSubview:imageViewB]; 
     imageViewB.tag = 33; 

     imageViewC = [[UIImageView alloc] initWithFrame:CGRectMake(/* frame it here */)]; 
     [cell.contentView addSubview:imageViewC; 
     imageViewC.tag = 34; 

     // this too, need only be done upon creation 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    // now, whether it's a new cell or a reused cell, we have image views 

    BGMArticleAbstract *articleAbstract = [self.section.articleAbstracts objectAtIndex:indexPath.row]; 

    // change your methods getHeadline... getThumbnail... etc to answer UIImages 
    // not UIImageViews, which are setup only for new cells 

    imageViewA.image = [self getHedlineFromArticleAbstract:articleAbstract]]; 
    imageViewB.image = [self getThumbnailImageFromArticleAbstract:articleAbstract]]; 
    imageViewC.image = [self getAbstractParaFromArticleAbstract:articleAbstract]]; 

    // as a side note, once you get these methods returning images (more like model objects) 
    // rather than image views (view objects) they might be more appropriately placed 
    // in the BGMArticleAbstract class rather than the view controller 

    return cell; 
} 
+0

請注意,對於iOS6及更高版本,dequeueReusableCellWithIdentifier永遠不會返回nil,如果沒有任何可重用的單元格,它將爲您創建一個單元格。 –

+0

好點@KaanDedeoglu。在6+中,而不是檢查單元格爲零,首先執行viewWithTag分配,並檢查那些爲零....或在ib – danh

+0

中創建圖像視圖是的,我認爲這種情況最好通過在IB中創建的自定義UITableViewCell來解決:) –