0

如果人多的话,我的tableView卷轴会滞后。最多 20 个单元格运行良好,但高于 - 它在滚动时开始滞后。请建议一个具有更好滚动结果的实现。这是我的做法:

我已经定义了一个自定义UITableViewCell类。

该单元格有 4 个标签和一个imageView(每个出口是一个综合属性): 在此处输入图像描述

tableView我在我的 中放置了一个viewController,并像这样填充它:

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

    static NSString *CellIdentifier = @"CustomCell";

    MyCustomCell *cell = (MyCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
        for (id currentObject in topLevelObjects)
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (MyCustomCell *) currentObject;
                break;
            }
    }
    [cell.label_descr setText:@"bla-bla-bla"];
    [cell.label_date setText:@"bla-bla-bla"];
    [cell.label_time setText:@"bla-bla-bla"];
    [cell.label_numeric setText:@"bla-bla-bla"];
    [cell.image_view setImage:[UIImage imageWithContentsOfFile:@"bla-bla-bla"]];

    return cell;
}

如您所见,每个单元格中的文本数量非常少,并且用于该图像的图像UIImageView约为 25x25 .png 文件。

tableView的应该有 300 多个单元格(不要怪我,我有一个“来自地狱的客户”)。

请提出一种使tableView滚动更流畅,没有(很多)滞后的方法。或者另一种方式来向我的“来自地狱”的客户展示那些“该死的超过 300 个细胞”。

提前300谢谢!

PS:对不起,如果重复,但找​​到的解决方案根本没有帮助。

编辑:

关于用于 imageView 的图像:

我只使用 2 个不同的图像:

  1. 一个“复选标记”——交易完成
  2. 和“待处理” - 正在进行的交易

也许我使用在我的 tableViewCell xib 中定义 2 个 imageView 插座,并且只选择所需的 imageView,而不是每次都设置所需的图像?

找到解决方案,感谢大家,尤其是 iNoob 和 max_。

  1. 在tableViewCell 的xib 中,我将“checkMark”设置为imageView 的默认图像。
  2. 在定义单元格的值时cellForRowAtIndexPath,只有在需要时,我才会说:

    if_I_should_present_a_pending_image:

       [cell setPending];
    

用“待定”图像替换“checkMark”(tableViewCell 类中定义的方法):

- (void)setPending{
    self.image_view.animationImages = [NSArray arrayWithObjects:    
                                    [UIImage imageNamed:@"pending_1.png"],
                                    [UIImage imageNamed:@"pending_2.png"],
                                    [UIImage imageNamed:@"pending_3.png"],
                                    [UIImage imageNamed:@"pending_4.png"],
                                    nil];
    self.image_view.animationDuration = 2.0;
    self.image_view.animationRepeatCount = 0;
    [self.image_view startAnimating];
}

l

在那之后,桌子像魅力一样滚动。再次感谢大家。干杯。

4

3 回答 3

1
  1. 不要遍历所有子视图:cell = [topLevelObjects objectAtIndex:0];
  2. 使用 gcd 在后台加载图像,并将它们存储在 NSDictionary 中以便于访问:

伪代码:

If caches dict contains an object for the URL you want to load
    Retrieve that image from the dict
    Set it as the image
Else
    Load the image using dispatch_async()
    Add it to the dict
于 2012-08-03T12:50:08.497 回答
0

我发现这篇文章建议以编程方式创建单元而不是使用 nib 文件可以快 5-10%。我不知道这是否属实,因此请持保留态度,但可能值得一试。

于 2012-08-03T12:51:45.063 回答
0

用以下代码替换您的代码并尝试一下。

对于以下代码:

1)在您的控制器中获取您的 UITableViewCell 的 IBOutlet 。对于下面的代码,它是myCustomTableViewCell

 MyCustomCell *customCell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"];
 if(customCell == nil) {
     [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
     customCell = myCustomTableViewCell;
  }

希望它会奏效。

于 2012-08-03T12:52:24.813 回答