【问题标题】:Clear cells in UITableView to put other views on cells清除 UITableView 中的单元格以将其他视图放在单元格上
【发布时间】:2012-01-14 11:10:44
【问题描述】:

根据选择的类别,我将拥有具有不同子视图的单元格。我选择了一个新类别,重新加载数据等,但是当我在它们类别之间切换时,这些视图不是与其他子视图一起显示新单元格,而是相互叠加,如何纠正? 这是我的代码:

//cell for row at indexPath    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }

    if ([currentCategory isEqualToString:@"Projects"])
    {
        Project *pr=[projectsArray objectAtIndex:indexPath.row];
        NSLog(@"Project ID %i, ProjectName %@", pr.ident, pr.projectName);

        UILabel *nameLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 100)];
        nameLabel.text=pr.projectName;

        UIImageView *iv=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 192)];
        iv.image=pr.baseImage;
        [cell addSubview:iv];
        [cell addSubview:nameLabel];
    }
    else if ([currentCategory isEqualToString:@"Glossaire"])
    {            
        Glossaire *gl=[glossaireArray objectAtIndex:indexPath.row];
        UILabel *nameLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
        nameLabel.font=[UIFont fontWithName:@"Arial" size:25.0f];
        nameLabel.text=gl.glossaireName;
        nameLabel.backgroundColor=[UIColor redColor];
        UILabel *introLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 50, 200, 50)];
        introLabel.font=[UIFont fontWithName:@"Arial" size:16.0f];
        introLabel.text=gl.intro;
        introLabel.backgroundColor=[UIColor redColor];
        UILabel *descriptionLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 100, 350, 100)];
        descriptionLabel.font=[UIFont fontWithName:@"Arial" size:16.0f];
        descriptionLabel.text=gl.description;
        descriptionLabel.backgroundColor=[UIColor redColor];
        NSLog(@"Glossaire ID: %i, NAME: %@ INTRO: %@ Description %@", gl.ident, gl.glossaireName, gl.intro, gl.description);
        [cell addSubview:nameLabel];
        [cell addSubview:introLabel];
        [cell addSubview:descriptionLabel];            
    }

    return cell;
}
//And switching between categories
- (IBAction)viewProjects:(id)sender 
{
    currentCategory=@"Projects";
    projectsArray=[dbm fetchProjectsSummary];
    [mainTable reloadData];            
}

- (IBAction)viewGlossaire:(id)sender
{
    currentCategory=@"Glossaire";
    glossaireArray=[dbm fetchGlossaireSummary];
    [mainTable reloadData];
}

他们还说重用标识符已被弃用,它的新版本是什么?谢谢!

【问题讨论】:

  • 请编辑您的问题并格式化您提供的代码。
  • 您是否使用 IB 创建单元格?如果是,您可以创建两种自定义类型的单元格并根据currentCategory 返回正确的一种。同样可以通过继承 UITableViewCell 并以编程方式添加 UI 元素来完成。
  • 我知道,谢谢)但我已经选择了我的方法)谢谢)

标签: ios uitableview


【解决方案1】:

我刚刚在cellForRowAtIndexPath memory management回答了一个类似的问题。

基本上,单元格会被重复使用,因此您每次显示单元格时都会将子视图添加到单元格中,并且它们会随着时间的推移而增加。您可以为每个单元格布局使用不同的 CellIdentifier,这样具有一个布局的单元格就不会被重新用于需要不同布局的单元格,或者您可以摆脱这个逻辑:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}

只要有这个:

   UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil];

这样您的单元格就不会每次都被重复使用,并且您不必担心清理上次使用时的内容。

要真正回答您的问题,您可以遍历单元格子视图并说 [view removeFromSuperview] 以每次都将其清除,但我认为这不是一个好的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多