gpt4 book ai didi

iphone - 向 UITableViewCell 添加 subview

转载 作者:可可西里 更新时间:2023-11-01 03:12:06 25 4
gpt4 key购买 nike

我在向 UITableViewCell 添加 subview 时遇到了问题。它在表格尺寸小于 iPhone 尺寸时有效。

但是当尺寸更大时,当我滚动时它会产生一些可怕的效果:

enter image description here

应该是这样的:

enter image description here那么我认为它来自于细胞复用。这是我的代码示例:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *kCellIdentifier = @"UITableViewCellStyleSubtitle";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil) {
//construct the cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:kCellIdentifier] autorelease];


//clear the previuous content
NSLog(@"Il y a %d subviews", [[[cell contentView] subviews] count]);
//[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
NSLog(@"Il y a %d subviews", [[[cell contentView] subviews] count]);
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
}

switch (indexPath.row) {
case 0:
[cell addSubview:titleEvent];
break;
case 1:
//load the owner logo
[cell addSubview:logoAsso];
break;
case 2:
//StartDate
[cell addSubview:clockImage];
break;
case 3:
//EndDate
[cell addSubview:clockEndImage];
break;
case 4:
//Address
[cell addSubview:adress];
break;
case 5:
//map
[cell addSubview:map];
break;
case 6:
//header
[Graphism configureSeparationCell:cell];
break;
case 7:
//descritpion
[cell addSubview:descriptionEvent];
break;
default:
break;
}
return cell;
}

subview 是类的属性,在viewDidLoad方法中配置。如果您能告诉我我做错了什么,那将是一种解脱。

最佳答案

试试这段代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"] autorelease];

} else {
UIView *subView = (UIView *)[cell.contentView viewWithTag:1];
if ([subView superview]) {
[subView removeFromSuperview];
}
}

UIView *subView = [[UIView alloc] init];
subView.tag = 1;
[cell.contentView addSubview:subView];
[subView release];

return cell;
}

关于iphone - 向 UITableViewCell 添加 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7173432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com