Iphone UITableViewCell重用和图像重新渲染
我有一个UITableView,它有18个部分,每个部分有1个标题标题和1个单元格。在每个单元格中,都会有一个水平的UIScrollView,其中包含多个图像 问题是。。。我可以正确加载图像和单元格,但当图像数量增加,单元格滚动到顶部时,其他单元格中会弹出来自不同单元格的图像。。。所有的渲染和图像都是错误的。。。应该在第3节或第4节中出现的图像出现在第1节中……等等 数据源是从JSON加载的,JSON是正确的。当图像不多且只有5-8个部分时,一切都能完美工作。所以,这让我相信是重用UITableViewCell导致了这个问题 有人能帮我一下吗。。。谢谢以下是UITableViewCell的代码Iphone UITableViewCell重用和图像重新渲染,iphone,ios,uitableview,Iphone,Ios,Uitableview,我有一个UITableView,它有18个部分,每个部分有1个标题标题和1个单元格。在每个单元格中,都会有一个水平的UIScrollView,其中包含多个图像 问题是。。。我可以正确加载图像和单元格,但当图像数量增加,单元格滚动到顶部时,其他单元格中会弹出来自不同单元格的图像。。。所有的渲染和图像都是错误的。。。应该在第3节或第4节中出现的图像出现在第1节中……等等 数据源是从JSON加载的,JSON是正确的。当图像不多且只有5-8个部分时,一切都能完美工作。所以,这让我相信是重用UITable
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
// add a placeholder cell while waiting on table data
if ([self.datasource count] == 0 && indexPath.section == 0 && indexPath.row == 0)
{
UILabel *Fetch;
static NSString *infoCell = @"infoCell";
cell = [tableView dequeueReusableCellWithIdentifier:infoCell];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:infoCell] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CellFetch = [[[UILabel alloc] initWithFrame:CGRectMake(15.0, 120.0, 280.0, 80.0)] autorelease];
CellFetch.tag = FETCH_TAG;
CellFetch.font = [UIFont boldSystemFontOfSize:16.0];
CellFetch.textColor = [UIColor grayColor];
CellFetch.shadowColor = [UIColor whiteColor];
CellFetch.shadowOffset = CGSizeMake(0, -0.5);
CellFetch.textAlignment = UITextAlignmentCenter;
[cell.contentView addSubview:CellFetch];
} else {
CellFetch = (UILabel *)[cell.contentView viewWithTag:FETCH_TAG];
}
Fetch.text = fetchStatus;
return cell;
} else {
UIScrollView *imgScroll;
static NSString *imgCell = @"imgCell";
cell = [tableView dequeueReusableCellWithIdentifier:imgCell];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:imgCell] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
imgScroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 108.0)]autorelease];
imgScroll.tag = SCROLLVIEW_TAG;
imgScroll.showsVerticalScrollIndicator = NO;
imgScroll.showsHorizontalScrollIndicator = NO;
} else {
imgScroll = (UIScrollView *)[cell.contentView viewWithTag:SCROLLVIEW_TAG];
}
NSArray *sorted = [[self.datasource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
//NSArray *allDistrict = [self.datasource allValues];
NSString *key = [sorted objectAtIndex:indexPath.section];
NSArray *img = [self.datasource valueForKey:key];
int column = 0;
int space = 10;
int width = 80;
int x = space+5.0;
for (NSUInteger i=0; i < img.count; i++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
NSDictionary *Info = [img objectAtIndex:i];
NSString *iconURL = [NSString stringWithFormat:@"%@",[Info objectForKey:@"ImgURL"]];
button.frame = CGRectMake(x , 4, 80, 80);
[button setImageWithURL:[NSURL URLWithString:iconURL] placeholderImage:[UIImage imageNamed:@"pholder"]];
NSString *imgID = [Info objectForKey:@"ImgID"];
int k = [imgID intValue];
button.tag = k;
[button addTarget:self action:@selector(onButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[imgScroll addSubview:button];
x+=space+width;
column++;
}
[imgScroll setContentSize:CGSizeMake(column*90+10, 108)];
[cell.contentView addSubview:imgScroll];
}
// Configure the cell...
return cell;
//自定义表视图单元格的外观。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=nil;
//在等待表数据时添加占位符单元格
if([self.datasource count]=0&&indepath.section==0&&indepath.row==0)
{
UILabel*获取;
静态NSString*infoCell=@“infoCell”;
单元格=[tableView dequeueReusableCellWithIdentifier:infoCell];
如果(单元格==nil)
{
单元格=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
重用标识符:infoCell]自动释放];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
CellFetch=[[UILabel alloc]initWithFrame:CGRectMake(15.01220.0280.0)]自动释放];
CellFetch.tag=FETCH\u标记;
CellFetch.font=[UIFont boldSystemFontOfSize:16.0];
CellFetch.textColor=[UIColor grayColor];
CellFetch.shadowColor=[UIColor whiteColor];
CellFetch.shadowOffset=CGSizeMake(0,-0.5);
CellFetch.textAlignment=UITextAlignmentCenter;
[cell.contentView addSubview:CellFetch];
}否则{
CellFetch=(UILabel*)[cell.contentView视图WithTag:FETCH_TAG];
}
Fetch.text=fetchStatus;
返回单元;
}否则{
UIScrollView*imgScroll;
静态NSString*imgCell=@“imgCell”;
单元格=[tableView dequeueReusableCellWithIdentifier:imgCell];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:imgCell]自动释放];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
imgScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0.0,0.03203108.0)]autorelease];
imgScroll.tag=滚动视图\标签;
imgScroll.showsVerticalScrollIndicator=否;
imgScroll.showshorizontalscrolindicator=否;
}否则{
imgScroll=(UIScrollView*)[cell.contentView视图带标记:SCROLLVIEW_标记];
}
NSArray*排序=[[self.datasource allKeys]使用选择器排序数组:@selector(不区分大小写比较:)];
//NSArray*allDistrict=[self.datasource allValues];
NSString*key=[sorted objectAtIndex:indexath.section];
NSArray*img=[self.datasource valueForKey:key];
int列=0;
int空间=10;
整数宽度=80;
int x=空间+5.0;
对于(整数i=0;i
}编写
[cell.contentView addSubview:imgScroll]代码>在单元==nil
部分中
像
if (cell == nil)
{
cell = [[[UITableViewCell alloc] nitWithStyle:UITableViewCellStyleDefault reuseIdentifier:imgCell] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
imgScroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 108.0)]autorelease];
imgScroll.tag = SCROLLVIEW_TAG;
imgScroll.showsVerticalScrollIndicator = NO;
imgScroll.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:imgScroll];
}
尝试使每个分区的单元格标识符都是唯一的,这样它们就不会在分区之间重复使用:
cell = [tableView dequeueReusableCellWithIdentifier:@"section1cell"];
cell = [tableView dequeueReusableCellWithIdentifier:@"section2cell"];
我见过这样的问题,在某些部分的细胞没有图像,但在其他部分有图像。所以当它重用单元格时,除非您特别将image属性设置为nil。。。你可以得到一张以前拍过的照片
更新:
这是我所说的关于分区号的内容
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *section = [NSString stringWithFormat:@"section%@cell", indexPath.section];
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:section];
// some configuration
return cell;
}
谢谢…但是节计数是动态的,所以在加载json之前我不知道有多少节…所以,这个解决方案不起作用。节包含在传递给委托方法“cellforrowatinexpath:(nsindepath*)indepath”的indepath中,可以从indepath.section获得