滚动后使用可取消查询的单元格重叠uitableview单元格的内容



当我使用dequeReusableCell时,当我向下或向上滚动时,最后一个单元格(在可见屏幕之外)的内容会重叠,但每次创建新单元格时都有效。我想使用可取消查询的功能。

下面的代码可以工作,但当我取消注释时,可查询问题的部分再次出现。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"Cell";
    //Code causing problem
    /*UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(!cell)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }*/
    //Code working fine
    UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 180, 20)];
    [titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
    UIImageView *headerImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
    headerImageView.image=[UIImage imageNamed:@"offer_details.jpg"];
    UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 20)];
    [detailLabel setFont:[UIFont systemFontOfSize:14]];
    UIButton *callButton=[[UIButton alloc]initWithFrame:CGRectMake(10, 30, 20, 20)];
    [callButton setBackgroundColor:[UIColor orangeColor]];
    MKMapView *mapView=[[MKMapView alloc] initWithFrame:CGRectMake(10, 30, 320, 200)];
    switch (indexPath.row) {
        case 0:
            [cell.contentView addSubview:headerImageView];
            [cell setBackgroundColor:[UIColor blackColor]];
            titleLabel.text=@"Get great offer Upto 20% Off";
            titleLabel.frame=CGRectMake(10, 220, 230, 20);
            titleLabel.textColor=[UIColor colorWithRed:251.0/255.0f green:78.0f/255.0f blue:8.0f/255.0f alpha:1];
            detailLabel.frame=CGRectMake(10, 240, 300, 20);
            detailLabel.textColor=[UIColor whiteColor];
            detailLabel.text=@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt.";
            [cell.contentView addSubview:detailLabel];
            break;
        case 1:
            titleLabel.text=@"Details";
            detailLabel.text=@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ";
            [cell.contentView addSubview:detailLabel];
            break;
        case 2:
            titleLabel.text=@"Collection Methods";
            detailLabel.text=@"Call and Reserve";
            detailLabel.frame=CGRectMake(50, 30, 300, 20);
            [cell.contentView addSubview:detailLabel];
            [cell.contentView addSubview:callButton];
            break;
        case 3:
            titleLabel.text=@"Location";
            [cell.contentView addSubview:mapView];
            break;
        case 4:
            titleLabel.text=@"About us";
            detailLabel.text=@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
            [cell.contentView addSubview:detailLabel];
            break;
        case 5:
            titleLabel.text=@"Legal";
            detailLabel.text=@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
            [cell.contentView addSubview:detailLabel];
            break;
        default:
            break;
    }
    detailLabel.numberOfLines=0;
    [detailLabel sizeToFit];
    [cell.contentView addSubview:titleLabel];

    return cell;
}

编辑:

我是否可以使用可取消查询的单元格,因为我的行的内容不常见?一行包含图像,一行包含地图视图,一行仅包含标签。提前Thanx。

第2版:

@HRM现在显示除第一个单元格外的详细标签空白

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

{static NSString*cellIdentifier=@"Cell";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//UITableViewCell *cell;
if(!cell)
{
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 180, 20)];
    [titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
    titleLabel.tag=1001;
    [cell.contentView addSubview:titleLabel];
    switch (indexPath.row) {
        case 0:
        {
            UIImageView *headerImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
            headerImageView.image=[UIImage imageNamed:@"offer_details.jpg"];
            headerImageView.tag=1002;
            UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 20)];
            [detailLabel setFont:[UIFont systemFontOfSize:12]];
            detailLabel.tag=1003;
            detailLabel.numberOfLines=0;
            [detailLabel sizeToFit];
            [cell.contentView addSubview:headerImageView];
            [cell.contentView addSubview:detailLabel];
        }
            break;
        case 1:
        {
            UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 20)];
            [detailLabel setFont:[UIFont systemFontOfSize:12]];
            detailLabel.numberOfLines=0;
            [detailLabel sizeToFit];
            [cell.contentView addSubview:detailLabel];
            detailLabel.tag=1004;
        }
            break;
        case 2:
        {
            UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 20)];
            [detailLabel setFont:[UIFont systemFontOfSize:12]];
            UIButton *callButton=[[UIButton alloc]initWithFrame:CGRectMake(10, 30, 15, 15)];
            [callButton setBackgroundColor:[UIColor orangeColor]];
            detailLabel.numberOfLines=0;
            [detailLabel sizeToFit];
            [cell.contentView addSubview:detailLabel];
            detailLabel.tag=1005;
            [cell.contentView addSubview:callButton];
        }
            break;
        case 3:
        {
            MKMapView *mapView=[[MKMapView alloc] initWithFrame:CGRectMake(0, 30, 320, 140)];
            [cell.contentView addSubview:mapView];
        }
            break;
        case 4:
        {
            UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 20)];
            [detailLabel setFont:[UIFont systemFontOfSize:12]];
            detailLabel.tag=1006;
            detailLabel.numberOfLines=0;
            [detailLabel sizeToFit];
            [cell.contentView addSubview:detailLabel];
        }
            break;
        case 5:
        {
            UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 20)];
            [detailLabel setFont:[UIFont systemFontOfSize:12]];
            detailLabel.tag=1007;
            detailLabel.numberOfLines=0;
            [detailLabel sizeToFit];
            [cell.contentView addSubview:detailLabel];
        }
            break;
        default:
            break;
    }
}

switch (indexPath.row) {
    case 0:
    {
        [cell setBackgroundColor:[UIColor blackColor]];
        UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1001];
        UILabel *detailLabel=(UILabel*)[cell.contentView viewWithTag:1003];
        titleLabel.text=@"Get great offer Upto 20% Off";
        titleLabel.frame=CGRectMake(10, 210, 230, 20);
        titleLabel.textColor=[UIColor colorWithRed:251.0/255.0f green:78.0f/255.0f blue:8.0f/255.0f alpha:1];
        detailLabel.frame=CGRectMake(10, 230, 300, 20);
        detailLabel.textColor=[UIColor whiteColor];
        detailLabel.text=@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt.";
    }
        break;
    case 1:
    {
        UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1001];
        UILabel *detailLabel=(UILabel*)[cell.contentView viewWithTag:1004];
        titleLabel.text=@"Details";
        detailLabel.text=@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ";
    }
        break;
    case 2:
    {
        UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1001];
        UILabel *detailLabel=(UILabel*)[cell.contentView viewWithTag:1005];
        titleLabel.text=@"Collection Methods";
        detailLabel.text=@"Call and Reserve";
        detailLabel.frame=CGRectMake(30, 30, 300, 20);
    }
        break;
    case 3:
    {
        UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1001];
        titleLabel.text=@"Location";
    }
        break;
    case 4:
    {
        UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1001];
        UILabel *detailLabel=(UILabel*)[cell.contentView viewWithTag:1006];
        titleLabel.text=@"About us";
        detailLabel.text=@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. nn   Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
    }
        break;
    case 5:
    {
        UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1001];
        UILabel *detailLabel=(UILabel*)[cell.contentView viewWithTag:1007];
        titleLabel.text=@"Legal";
        detailLabel.text=@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. nn   Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
    }
        break;
    default:
        break;
}


return cell;

}

当您使用dequeueReusableCellWithIdentifier时,tableView将重用现有单元格,而在您的代码中,您正试图一次又一次地创建新控件,因此它将重叠。最好的方法是这样做。请仔细检查。希望能有所帮助。

根据您的评论进行更新。

static NSString *cellIdentifier= @"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//If tableView dont have cell available for reuse, then 
//create a new cell and attach the controls to it
if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    if(indexPath.row == 0){
        UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 180, 20)];
    [titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
        titleLabel.tag = 1002;
        [cell.contentView addSubview:titleLabel];
    }
}
//Now we have a cell object with controls attached. 
//Now to update the contents on the control, just get the control from cell 
//using its tag and set your data from datasource
if(indexPath.row == 0){
    UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:1002];
    titleLabel.text = //Text from your datasource for this row
}

请注意,您还需要注意heightForRowAtIndexPath中的高度。同样的逻辑也将应用于此。

以下是我的建议:

  1. 创建一个自定义的UITableViewCell类,并在其initWithStyle:reuseIdentifier:中创建所有必需的子视图。您还需要将子视图指定给属性以供外部使用
  2. 告诉您的UITableView对象在viewDidLoad中注册类或NIB
  3. 然后,在tableView:cellForRowAtIndexPath:中,您可以访问单元的属性并对其进行适当更新

这样就不会在每次调用tableView:cellForRowAtIndexPath:时重新创建单元或其子视图。

我建议通过objective-C枚举从cellForRowAtInddexPath中UITableViewCell的内容视图中删除子视图。查看下面的代码以供参考。

for(UIView *view in cell.contentView.subviews){  
    if ([view isKindOfClass:[UIView class]]) {  
        [view removeFromSuperview];   
    }
}

最新更新