ios - UITableView 重复内容

标签 ios objective-c uitableview

我知道这是一个由来已久的问题,但这次我对它做了一些改动。

正如标题所说,我的 UITableView 是重复内容。在这种情况下,单元格的背景颜色会发生变化,而当将其关闭并返回屏幕时,单元格的背景颜色不应该发生变化。

我知道这可能是由于重复使用了单元格,但我的问题是我不能只给每个单元格一个唯一的单元格标识符,因为我已经在 Storyboard中构建了我的单元格,这需要在其中输入它。

那么我怎样才能解决这个问题,同时让我的单元格设计在我的 Storyboard中呢?

一点代码:

    static NSString *cellIdentifier = @"Cell";
        SLDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

        if (!cell)
        {
            cell = [[SLDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

    [cell setBackgroundColor:[UIColor slateBackgroundGrey]]; //Set standard background colour
    [cell.titleLbl setText:[self.tableTitles objectAtIndex:indexPath.row]]; //cell titles from an array (static data I made)

    //This will occur for each cell, here are two examples
    if ([cell.titleLbl.text isEqualToString:@"Aircraft"])
    {
        NSString *detailText = @"";
        if (!self.flight.aircraft) detailText = @"N/A";
        else detailText = self.flight.aircraft.name;
        [cell.detailLbl setText:detailText];
    }
    if ([cell.titleLbl.text isEqualToString:@"Delete"])
    {
        UIView *containerView = [cell.titleLbl superview];
        [containerView setBackgroundColor:[UIColor triggerRed]];
        [cell.titleLbl setTextColor:[UIColor whiteColor]];
        [cell.disclosureIndicator setHidden:YES];
        [cell.detailLbl setText:@""];
    }

在删除单元格的情况下,我更改了单元格的颜色属性。

子类化 prepareForReuse 以重置单元格数据无法停止重复数据,就像在每个单独的单元格 if 语句中设置颜色一样(如上所示)。

谢谢。

最佳答案

无论何时返回一个单元格,无论是否重复使用,都必须配置所有单元格属性。否则,您会遇到上次保留某些属性的情况。无论单元格是从 Storyboard 还是其他方式创建的,这都适用,这是一个单元格重用问题,仅此而已。

关于ios - UITableView 重复内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21817107/

相关文章:

ios - 使用 AFNetworking 从 Django 服务器下载文件

ios - NSArray 在方法结束时丢失值

ios - 为什么非零数字无法将 BOOL 属性设置为 YES?

ios - 无法在 cellForRowAtIndexPath 中获取单元格框架或约束

ios - 如何使用左右滑动手势切换标签栏选项卡?

ios - 通过单击自定义UIButton删除UITableView行

ios - MKMapView 不会缩放或动画

ios - 赌博许可证是否必须集成 iOS 应用程序的应用程序内投注?

swift - 保存到核心数据时 NSFetchedResultsController 不更新

ios - 如何在 UIKeyboard 可见时更改 UITableView 大小?