簡體   English   中英

Objective-C:每次向上和向下滾動時都會寫入UITableView單元格

[英]Objective-C: UITableView cells get written over each time when scrolling up and down

我有一個UITableView ,它通過CoreData填充,並且剛剛注意到一些奇怪的東西。 我在UITable中大約有20行,當我向下滾動表並再次向上時,單元格的標簽會被寫在現有文本的頂部,並且每次我再次向上和向上時都會繼續執行。 我的CellForRowAtIndexPath代碼是:

  // Customize the appearance of table view cells.
  - (UITableViewCell *)tableView:(UITableView *)tableView   cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Some constants ---
const NSInteger TOP_LABEL_TAG = 1001;
const NSInteger BOTTOM_LABEL_TAG = 1002;
UILabel *topLabel;
UILabel *bottomLabel;
const CGFloat LABEL_HEIGHT = 20;
//CGRect Imageframe = CGRectMake(7, 5, 35, 35);
//----



static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
}

//custom implementation of UIView for lazy loading of image 
//LazyUIImageView *lazy = [[LazyUIImageView alloc] initWithFrame:Imageframe];


//[cell addSubview:lazy];



Post *post = [fetchedResultsController objectAtIndexPath:indexPath];

NSSet *medias = post.PostMedia;
Media *media = [medias anyObject];
NSSet *thumbs  = media.MediaThumb;
Thumb *thumb = [thumbs anyObject];


UIImage *img = thumb.url;

if (img)
    cell.imageView.image = img;
else 
    cell.imageView.image = post.authorpic;

//The creation of the top label
topLabel =
[[[UILabel alloc]
  initWithFrame:
  CGRectMake(
             35 + 2.0 * cell.indentationWidth,
             0.5 * (tableView.rowHeight - 2 * LABEL_HEIGHT),
             tableView.bounds.size.width -
             45 - 4.0 * cell.indentationWidth
             - 35,
             LABEL_HEIGHT)]
 autorelease];
[cell.contentView addSubview:topLabel];

topLabel.tag = TOP_LABEL_TAG;
topLabel.backgroundColor = [UIColor clearColor];
topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
//---------------------------------

//The creation of the bottom label
bottomLabel =
[[[UILabel alloc]
  initWithFrame:
  CGRectMake(
             35 + 2.0 * cell.indentationWidth,
             0.5 * (tableView.rowHeight - 2 * LABEL_HEIGHT) + LABEL_HEIGHT,
             tableView.bounds.size.width -
             35 - 4.0 * cell.indentationWidth
             - 35,
             LABEL_HEIGHT)]
 autorelease];
[cell.contentView addSubview:bottomLabel];
//--------------------------------

//
// Configure the properties for the text that are the same on every row
//
bottomLabel.tag = BOTTOM_LABEL_TAG;
bottomLabel.backgroundColor = [UIColor clearColor];
bottomLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
bottomLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];

//
// Create a background image view.
//
cell.backgroundView =
[[[UIImageView alloc] init] autorelease];
cell.selectedBackgroundView =
[[[UIImageView alloc] init] autorelease];;


topLabel.text = post.title;
bottomLabel.text = @"put something here";


//
// Set the background and selected background images for the text.
// Since we will round the corners at the top and bottom of sections, we
// need to conditionally choose the images based on the row index and the
// number of rows in the section.
//
UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
if (row == 0 && row == sectionRows - 1)  //all
{
    rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
    selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
}
else if (row == 0) //top
{
    rowBackground = [UIImage imageNamed:@"topRow.png"];
    selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
}
else if (row == sectionRows - 1) //bottom
{
    rowBackground = [UIImage imageNamed:@"bottomRow.png"];
    selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"];
}
else //middle
{
    rowBackground = [UIImage imageNamed:@"middleRow.png"];
    selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"];
}
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;


//[lazy release];

return cell;

}

感謝您的幫助!

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

此行告訴您的應用重用現有的屏幕外單元格(如果存在)。 所以基本上你正在使用一個已經存在的單元格並在子視圖已經退出的相同位置添加一個新的子視圖。 您需要在添加新子視圖之前清除現有的子視圖。

更新以回答您的評論:

jan的代碼將刪除現有的子視圖。 您可以將它添加到上面的代碼中,因為if單元格的else子句是nil語句。

因此,如果您沒有可重用的單元格(單元格== nil),您的代碼將創建一個新代碼,否則您的代碼將從現有單元格中刪除舊的子視圖:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
}
else {
    UIView* subview;
    while ((subview = [[[cell contentView] subviews] lastObject]) != nil)
      [subview removeFromSuperview];
}

實現相同目標的另一種更簡潔的方法是:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
}
else {
    [[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
}

這是因為正在緩存單元格,並且您不斷添加內容視圖而不刪除已添加的內容。

您可以使用更具體的單元格標識符,僅在創建單元格時添加到內容視圖,或者在再次添加標簽之前清除內容視圖。

要清除單元格的子視圖,您可以添加以下內容:

if(cell == nil)
{...}
else
{
    UIView* subview;
    while ((subview = [[[cell contentView] subviews] lastObject]) != nil)
      [subview removeFromSuperview];
}

當cell!= nil表示它是你已經創建的單元格並添加子視圖時,這就是它們需要被清除的原因。

不想自動釋放UITableViewCell,因為多次滾動並更改設備位置時,您的應用可能會崩潰。

當我們為UITableView使用重用標識符時,它理解當我們沒有指定要重用的單元標識符的索引值時,需要重用單元格並且單元格中的值被重寫。

我們需要專門告訴表視圖單元,在索引路徑上重用單元,即

NSString *cellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];

並在if(cell == nil)中使用此單元格標識符

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellIdentifier:cellidentifier];

這將消除單元格之間的錯誤通信,並且單元格中的值不會相互重寫。更多可以避免索引位置或單元索引值之間的通信麻煩。希望這有幫助,謝謝:)

當cell不為nil時,您可以在else條件下刪除子視圖。

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

    //create  a uitableviewcell
    if(cell == nil)
    {
     //Do whatever you want to do 

    }else{
        [[cell.contentView viewWithTag:YOUR_TAG] removeFromSuperview];
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM