开发者

How to avoid slowdowns when scrolling table view?

I have custom table view cell with images (loaded from app document directory), labels, shadows etc. and when I scroll the table view it causes a lot of lags. How can I avoid these lags? I think it's possible to cache table view cell, or get a picture of the table view cell, but I don't know how to implement this. Please, help me :)

In cellForRowAtIndexPath: I set the data in if (cell == nil) block, but slowdowns are still there.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReus开发者_C百科ableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        // ...configuring is here...
    }

    return cell;
}

P.S. Images in cell are high resolution, but reduced to fit...


You should load your images in a background thread. If you're on iOS 4+, you can use GCD (Grand Central Dispatch) to load these asynchronously.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *CellIdentifier = @"ImageCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifier] autorelease];
    }
    NSString *imagepath = //get path to image here
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        dispatch_sync(dispatch_get_main_queue(), ^{
            [[cell imageView] setImage:image];
            [cell setNeedsLayout];
        });
    });
    return cell;
}


Check This out: http://www.markj.net/iphone-asynchronous-table-image/

This might help.


I think you are loading images from URL thats why you are facing lag in scrolling.. you should use Lazy table images check out this - http://developer.apple.com/iphone/library/samplecode/LazyTableImages/index.html

have a look into above sample code and post your exp. in comments

update 1 - try to remove elements from cell one by one.. and check which element is cause this ..

update 2 - ok.. then you know images are causing issues.. so you need to load them using a thread or you can also use lazy table images concept.. seems image size is quite high.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜