Iphone UITableView重用标识符:在顶部和底部重复元素
我的UITableView上下单元格在屏幕上滚动时交替出现问题。我相信这是reuseIdentifier的一个问题,但我不确定如何解决它。PlaceTableViewCell是Loren Britcher的ABTableViewCell的一个子类,用于快速滚动Iphone UITableView重用标识符:在顶部和底部重复元素,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我的UITableView上下单元格在屏幕上滚动时交替出现问题。我相信这是reuseIdentifier的一个问题,但我不确定如何解决它。PlaceTableViewCell是Loren Britcher的ABTableViewCell的一个子类,用于快速滚动 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NS
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *uniqueIdentifier = @"Cell";
PlaceTableViewCell *cell = (PlaceTableViewCell *)[tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];
if(cell == nil){
cell = [[PlaceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:uniqueIdentifier];
}
Places *place = [self.placesArray objectAtIndex:[indexPath row]];
cell.place_title = place.title;
cell.place_street = place.street;
cell.place_thumb = place.thumb_path;
return cell;
}
编辑:
从ABTableViewCell的子类中:
- (void)drawContentView:(CGRect)r
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
UIColor *textColor = [UIColor blackColor];
if(self.selected)
{
backgroundColor = [UIColor clearColor];
textColor = [UIColor whiteColor];
}
[backgroundColor set];
CGContextFillRect(context, r);
CGPoint p;
p.x = 42;
p.y = 2;
NSLog(@"place_thumb: %@", place_thumb);
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:place_thumb]];
UIImage *image = [UIImage imageWithData:imageData];
[image drawAtPoint: p];
p.x = p.x + 50;
[textColor set];
[place_title drawAtPoint:p withFont:place_titleFont];
p.y = 25;
textColor = [UIColor lightGrayColor];
[textColor set];
[place_street drawAtPoint:p withFont:place_streetFont];
}
您的cellForRowAtIndexPath看起来是正确的。您需要发布UITableView的所有数据源方法。我有一种预感,这个问题可能发生在您的numberOfRows方法中
是否根据数组大小设置行数?
另外,您确定数组保持不变吗?您遇到了什么问题?如果向下滚动并返回顶部,最后一个元素将取代第一个元素。如果您上下滚动,则相反的情况发生在第一个元素变为最后一个元素。它们是否都在同一节中?请尝试将字符串更改为其他任何内容,但据我所知,您显示的代码中没有任何错误数组保持不变。。。我还发布了drawContentView,我怀疑这可能是问题的根源。。。在同一个线程上加载带有dataWithContentsOfURL的图像会导致延迟吗?实际上,我尝试加载一个静态图像而不是远程图像,但问题仍然存在……嗯,这相当令人困惑。当为每个单元格设置不同的标识符时会发生什么?为了便于实验,请使用indexPath.row作为cellIdentifier。如果复制仍然存在,那么我们可以排除表视图的数据源方法的问题。
- (void)drawContentView:(CGRect)r
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
UIColor *textColor = [UIColor blackColor];
if(self.selected)
{
backgroundColor = [UIColor clearColor];
textColor = [UIColor whiteColor];
}
[backgroundColor set];
CGContextFillRect(context, r);
CGPoint p;
p.x = 42;
p.y = 2;
NSLog(@"place_thumb: %@", place_thumb);
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:place_thumb]];
UIImage *image = [UIImage imageWithData:imageData];
[image drawAtPoint: p];
p.x = p.x + 50;
[textColor set];
[place_title drawAtPoint:p withFont:place_titleFont];
p.y = 25;
textColor = [UIColor lightGrayColor];
[textColor set];
[place_street drawAtPoint:p withFont:place_streetFont];
}