我知道如果我在自定义单元格中添加了一些图像和子视图,那么我必须重复使用单元格,以便自定义控件不会出现在其他单元格上,但在这里我还有其他问题。我只想在第一部分的第一个单元格上安装ImageView,所以我在下面的代码中使用了IndexPath.Section == 0和IndexPath.Row == 0条件,但问题是当我滚动表格时,另一个单元格将满足这个条件,我的代码也将在该单元格上创建imageview。我尝试过标记它并使用相同的标记cellView,但它也没有帮助。单元问题是禁用少数单元的用户交互。最终滚动后,它会禁用所有单元格的用户交互。无论如何要解决这个问题吗?
感谢。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section == 0 && indexPath.row == 0) {
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease];
UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease];
[imageView setFrame: CGRectMake(10, 10, 54, 54)];
[cellView addSubview:imageView];
cell.backgroundView = cellView;
return cell;
} else if(indexPath.row == 0) {
NSString * title = [NSString string];
switch (indexPath.section) {
case 1:
title = @"Friends";
break;
case 2:
title = @"Accounts";
break;
case 3:
title = @"Stats";
break;
default:
title = nil;
break;
}
cell.textLabel.text = title;
cell.userInteractionEnabled = NO;
return cell;
}
cell.textLabel.text = @"Test";
return cell;
}
[已解决]正确代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.section == 0 && indexPath.row == 0) {
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease];
cell.imageView.image = imageView.image;
cell.textLabel.text = nil;
cell.textLabel.textColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = YES;
return cell;
} else if(indexPath.row == 0) {
NSString * title = [NSString string];
switch (indexPath.section) {
case 1:
title = @"Friends";
break;
case 2:
title = @"Accounts";
break;
case 3:
title = @"Stats";
break;
default:
title = nil;
break;
}
cell.imageView.image = nil;
cell.textLabel.text = title;
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = NO;
return cell;
}
cell.imageView.image = nil;
cell.textLabel.text = [cellItems objectAtIndex:(rows+indexPath.row-1)];
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = YES;
return cell;
}
[改进代码]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *NormalCellIdentifier = @"NormalCell";
static NSString *TitleCellIdentifier = @"TitleCell";
NSString *neededCellType;
if(indexPath.section == 0 && indexPath.row == 0) {
neededCellType = TitleCellIdentifier;
} else {
neededCellType = NormalCellIdentifier;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:neededCellType] autorelease];
//Only add content to cell if it is new
if([neededCellType isEqualToString: TitleCellIdentifier]) {
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease];
cell.imageView.image = imageView.image;
}
}
if([neededCellType isEqualToString: NormalCellIdentifier]) {
NSString * title;
if(indexPath.row == 0) {
switch (indexPath.section) {
case 1:
title = @"Friends";
break;
case 2:
title = @"Accounts";
break;
case 3:
title = @"Stats";
break;
default:
title = nil;
break;
}
cell.textLabel.text = title;
cell.textLabel.textColor = [UIColor redColor];
cell.userInteractionEnabled = NO;
} else {
cell.userInteractionEnabled = YES;
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.text = @"Test";
}
}
return cell;
}
答案 0 :(得分:1)
我认为你的问题是细胞的重复使得它不会被创建为新细胞的细胞具有一些你必须重新定义的属性。例如,尝试将cell.userInteractionEnabled = YES分配给所有其他情况并查看结果。
答案 1 :(得分:0)
问题在于,您不允许以后重新使用正确显示图像的单元格,并且图像视图仍在那里。
以下是两个解决方案:
在创建图像视图时设置标记值,然后在设置单元格时,包含要检查的代码并在必要时删除旧的imageView。
为需要图像视图的单元格和不需要图像视图的单元格分配不同的重用标识符。然后确保您只是在创建单元格时向单元格添加新图像视图,而不是在重复使用它们时。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *NormalCellIdentifier = @"NormalCell"; static NSString *TitleCellIdentifier = @"TitleCell"; NSString *neededCellType;
if(indexPath.section == 0 && indexPath.row == 0) { neededCellType = TitleCellIdentifier; } else { neededCellType = NormalCellIdentifier; }
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:neededCellType] autorelease];
//Only add content to cell if it is new if([neededCellType isEqualToString: TitleCellIdentifier]) { UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease]; UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease]; [imageView setFrame: CGRectMake(10, 10, 54, 54)]; [cellView addSubview:imageView]; cell.backgroundView = cellView; } }
if([neededCellType isEqualToString: NormalCellIdentifier]) { NSString * title; if(indexPath.row == 0) {
switch (indexPath.section) { case 1: title = @"Friends"; break; case 2: title = @"Accounts"; break; case 3: title = @"Stats"; break; default: title = nil; break; } cell.textLabel.text = title; cell.userInteractionEnabled = NO;
}
else { cell.textLabel.text = @"Test"; return cell; } } }
(最后几行掉了代码框)。应该这样做。