0

我已经自定义了表格视图单元格,创建了 nib 文件及其对应的 .h 和 .m 文件。一切正常,除了滚动时的缓慢,我怎样才能避免冻结可能是什么问题?

obj = [self.listData objectAtIndex: [indexPath row]];


if (obj != nil) {

    static NSString *CellIdentifier;

    if (obj->status == status1) {
        CellIdentifier = @"Cell_italic";
    } else if (obj->status == status2) {
        CellIdentifier = @"Cell_cent";
    } else {
        CellIdentifier = @"Cell";
    }

    custom_cell *cell = (custom_cell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"custom_cell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        char file_path[MAX_PATH]; // Holds file path.
        memset(file_path, 0, MAX_PATH);


        // Get path
        // Set Image
        UIImage *cellImage = [UIImage imageWithContentsOfFile:fle_path];

        if (cellImage != nil) {
            [cell.image_view setImage:cellImage];
        } 

        NSString *combined_name = [NSString stringWithCString:obj->combined_name encoding:NSASCIIStringEncoding];
        NSString *email = [NSString stringWithCString:obj->email encoding:NSASCIIStringEncoding];

        // Set name
        if (obj->status == status1)
        {
            cell.name_label.text = combined_name;

            cell.name_label.font = [UIFont fontWithName:@"Georgia-Italic" size:18];
            cell.email_label.text = email;

            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            return cell;
        }

        . . .
        . . .


}

dequeueReusableCellWithIdentifier 总是返回 nil,对吗?如果没有自定义,我已经多次看到它不是 nil,现在它总是返回 nil。

4

1 回答 1

1

每次都加载图像,这就是为什么表格滚动冻结的原因,UITableView您可以通过多种方式实现延迟加载,请参考下面的链接和演示可能会对您有所帮助:-

https://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html

https://github.com/rs/SDWebImage //README Section says,how to use it in your app.

https://github.com/nicklockwood/AsyncImageView/

您还可以使用 UI 线程在后台进程中加载​​图像

dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(backgroundQueue,^{
  // background process
  image = [NSData dataWithContentsOfFile:imageName];
  dispatch_async(mainQueue,^{
    // always update GUI from the main thread
    // uiimageview.image = image.... etc
 });
});
于 2013-02-08T05:57:52.560 回答