当我向 UITableViewCell 添加子视图时遇到问题。
当表格尺寸低于 iPhone 尺寸时,它可以工作。
但是当尺寸更大时,当我滚动时会产生一些可怕的效果:
它应该是这样的:
Then I think it comes from the cell reuse.
Here is a sample of my code :
- (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;
}
子视图是类的属性,并在方法 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)