Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将子视图添加到UITableViewCell_Iphone_Ios_Uitableview - Fatal编程技术网

Iphone 将子视图添加到UITableViewCell

Iphone 将子视图添加到UITableViewCell,iphone,ios,uitableview,Iphone,Ios,Uitableview,向UITableViewCell添加子视图时遇到问题。 当桌子的大小低于iPhone的大小时,它就工作了 但当大小变大时,当我滚动时会产生一些可怕的效果: 应该是这样的: 那么我认为它来自于细胞的再利用。 以下是我的代码示例: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *kCellId

向UITableViewCell添加子视图时遇到问题。 当桌子的大小低于iPhone的大小时,它就工作了

但当大小变大时,当我滚动时会产生一些可怕的效果:

应该是这样的:

那么我认为它来自于细胞的再利用。 以下是我的代码示例:

- (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方法中配置。 如果你能告诉我我做错了什么,那将是一种解脱

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

像这样,对所有交换机情况都执行此操作

[cell.contentView addSubview:clockImage]

试试这段代码

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