问题描述
我有一个UITableView
,它通过 CoreData 填充,并且刚刚发现了一些奇怪的东西.我在UITable中大约有20行左右,当我向下滚动表格并再次向上备份时,单元格的标签被写在现有文本的顶部,并且每次我上下滚动时都会继续这样做.我的CellForRowAtIndexPath
代码是:
I have a UITableView
that gets populated via CoreData, and have just noticed something strange. I have about 20 rows or so in the UITable, and when I scroll down the table and back up again, the cell's label gets written over the top of existing text, and keeps on doing it each time i go down and up again. My code for the CellForRowAtIndexPath
is:
// 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];
此行告诉您的应用重复使用现有的屏幕外单元(如果存在).因此,基本上,您要获取一个已经存在的单元格,并在子视图已经存在的位置添加一个新的子视图.您需要先清除现有的子视图,然后再添加新的子视图.
This line is telling your app to reuse existing offscreen cells if they exist. So basically you are taking an already existing cell and adding a new subview to it in the same place that a subview already exits. You need to clear out the existing subviews before adding your new subview.
根据您的评论进行更新:
Update in answer to your comment:
jan的代码将删除现有的子视图.您可以将其添加到上面的代码中,作为if单元格为nil语句的else子句.
jan's code will remove the existing subviews. You would add it to your code above as the else clause of your if cell is nil statement.
因此,如果您没有可用的可重用单元格(单元格== nil),则您的代码将创建一个新的单元格,否则您的代码将从现有单元格中删除旧的子视图:
So if you don't have a reusable cell available (cell == nil), your code will create a new one, else your code will remove the old subviews from the existing cell:
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];
}
实现同一件事的另一种更简洁的方法是:
an alternate, more terse way of achieving the same thing would be:
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)];
}
这篇关于Objective-C:每次向上和向下滚动时,都会重写UITableView单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!