将子视图添加到UITableViewCell

8

当我向UITableViewCell添加子视图时,遇到了一些问题。当表格大小小于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;
}

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

3个回答

5

尝试使用这段代码

- (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;
}

4
switch (indexPath.row) {
    case 0:


        if (![cell.contentView viewWithTag:11]) {

            titleEvent.tag = 11;

            [cell.contentView addSubview:titleEvent];
        }


        break;
    case 1:
        if (![cell.contentView viewWithTag:11]) {

            logoAsso.tag = 11;

            [cell.contentView addSubview:logoAsso];
        }

对于所有的switch case都可以这样做。


-3
[cell.contentView addSubview:clockImage];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接