【问题标题】:(custom) UITableViewCell's mixing up after scrolling(自定义)UITableViewCell 在滚动后混在一起
【发布时间】:2017-07-01 20:02:38
【问题描述】:

我已经解决这个问题几个星期了。

基本上,当我在 TableView 中向上/向下滚动时,使用 IB 中设计的自定义单元格时,所有内容都会混淆和放错位置

我尝试了多种解决方案,但都无济于事,您将不得不稍微原谅我的代码。

人们一直建议为表格单元格创建一个子视图,但我不知道该怎么做=/对于 iOS 开发来说还是很新的,所以如果你有可能的答案,请尽可能详细说明。

再一次,对不起我的代码 =/

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

    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    NSInteger intCellTag;

    NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];


    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];


        [[[NSBundle mainBundle] loadNibNamed:@"EventsCustomTVCell" owner:self options:nil] lastObject];
        cell = tvCell;
        self.tvCell = nil;


        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.tag = intCellTag;


        intCellTag++;

        UIImage *customCellBG = [UIImage imageNamed:@"EventsCustomTableCellBG.png"];
        UIImageView *customCellBGImageView = [[UIImageView alloc] initWithImage: customCellBG];
        customCellBGImageView.contentMode = UIViewContentModeScaleToFill;
        cell.backgroundView = customCellBGImageView;
        [customCellBGImageView release];




        [cell.contentView addSubview:imgThumbnail];
        [cell.contentView addSubview:lblName];
        [cell.contentView addSubview:lblDescription];
        [cell.contentView addSubview:lblDate];

    }



    imgThumbnail.image = [UIImage imageNamed:[dictionary objectForKey: @"Thumbnail"]];
    lblName.text = [dictionary objectForKey:@"Title"];
    lblDescription.text = [dictionary objectForKey:@"Description"];
    lblDate.text = [dictionary objectForKey:@"Date"];


    return cell;


}

【问题讨论】:

    标签: iphone objective-c ios uitableview


    【解决方案1】:

    看起来您正在尝试在定义每个 UITableViewCell 时混合隐喻——从 .xib 加载并手动创建子视图。这当然没有错,但是您可以将图像和标签直接放入 tableviewCell 中,如下所示:

    这是显示每一行的代码(在 IB 中,您自然会为要在每行自定义的每个 UIKit 对象分配非零唯一标签)

    #define kImageTag 1
    #define kLabel1Tag 2
    #define kLabel2Tag 3
    #define kLabel3Tag 4
    
    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"MyTvCell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
            self.tvCell = nil;
            [[NSBundle mainBundle] loadNibNamed:@"TvCell" owner:self options:nil];
            cell = self.tvCell;
        }
    
        UIImageView *iv =  (UIImageView *) [cell viewWithTag:kImageTag];
        UILabel *lbl1 = (UILabel *) [cell viewWithTag:kLabel1Tag];
        UILabel *lbl2 = (UILabel *) [cell viewWithTag:kLabel2Tag];
        UILabel *lbl3 = (UILabel *) [cell viewWithTag:kLabel3Tag];
    
        iv.image = [UIImage imageNamed:@"myimage.png"];
        lbl1.text = @"howdy";
        lbl2.text = @"there";
        lbl3.text = @"foo";
    
        return cell;
    }
    

    【讨论】:

      【解决方案2】:
      iOS Swift: I have done following to resolve my issue
      
      let CellIdentifier: String = "CellIdentifier\(indexPath.section)\(indexPath.row)"
      
              var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as UITableViewCell?
      
              if cell == nil {
                  cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: CellIdentifier)
              }
      

      【讨论】:

        【解决方案3】:
        imgThumbnail
        lblName
        lblDescription
        lblDate
        

        您应该为这些对象分配一个标签(您可以在界面生成器中执行此操作)。
        如果您配置单元格,您可以在单元格中查询标签并设置其属性。

        现在,您保留对添加的最后一个单元格中的标签的引用,并且每次表格要求新单元格时,您都会更改这些标签。

        假设您在 interfacebuilder 中将标签 10 分配给标签 lblDescription

        然后你会替换

        lblDescription.text = [dictionary objectForKey:@"Description"];
        

        UILabel *lblDescription = (UILabel *)[cell viewWithTag:10];
        lblDescription.text = [dictionary objectForKey:@"Description"];
        

        编辑:我假设 imgThumbnail 等是您单元格的子视图,但您再次添加它们。如果我的假设是正确的,您应该摆脱 [cell.contentView addSubview...]。

        如果我错了,你应该去掉 imgThumbnail 等作为你的视图控制器的实例变量。并在每次创建新单元格时添加单独的 UIView。就像你对背景视图所做的那样。但是当您配置单元格值时,您已经分配了一个标签并使用该标签。

        【讨论】:

        • 天啊,你的传奇!有效!我对标签和对象 ID 感到困惑。哦!但是我无法让图像保持原样,我如何在图像上添加标签?
        • 仍然坚持下去,你有机会发给我演示代码吗?谢谢 :)
        【解决方案4】:

        你可以使用

        NSString *cellIdentifier =[NSString stringWithFormat:@"%d",indexPath.row];

        而不是

        静态 NSString *CellIdentifier = @"Cell";

        【讨论】:

        • 在发布多个问题的复制和粘贴样板/逐字答案时要小心,这些往往会被社区标记为“垃圾邮件”。
        【解决方案5】:

        您正在向每个单元格添加相同的子视图对象

        [cell.contentView addSubview:imgThumbnail];
        [cell.contentView addSubview:lblName];
        [cell.contentView addSubview:lblDescription];
        [cell.contentView addSubview:lblDate];
        

        每个单元格都需要自己的一组子视图对象。

        【讨论】:

          猜你喜欢
          • 2015-02-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-01
          相关资源
          最近更新 更多