如何在UITableView中自定义UITableViewCell而不会出现任何冻结

时间:2013-02-08 05:49:52

标签: iphone ios uitableview reuseidentifier

我已经自定义了表视图单元格,创建了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,是不是?没有定制,我已经看到它不是多次,现在它总是返回零。

1 个答案:

答案 0 :(得分: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
 });
});