由于阴影和边框导致UITableView滞后 [英] UITableView Lagging due to Shadows and Borders

查看:94
本文介绍了由于阴影和边框导致UITableView滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码为UITableViewCell的背景添加边框颜色和阴影.我的问题是,这段代码在tableView本身上造成了巨大的滞后.

I have the following code to add a border colour and drop shadow to the background of my UITableViewCell. My problem is that this code causes a huge lag on the tableView itself.

请您告诉我如何优化代码,防止UITableView的滞后?

Please can you tell me how I can optimise my code, preventing the lag of the UITableView?

if ([cell viewWithTag:012] == nil && comment.isReply == NO) {
    UIImageView *iv = [[[UIImageView alloc] initWithFrame:frame] autorelease];
    [iv setImage:[UIImage imageNamed:@"paper"]];
    [iv setTag:012];
    [cell insertSubview:iv atIndex:0];

    [iv.layer setBorderWidth:1.0];
    [iv.layer setBorderColor:[[UIColor whiteColor] CGColor]];

    [iv.layer setShadowColor:[[UIColor blackColor] CGColor]];
    [iv.layer setShadowOffset:CGSizeMake(0, 1)];
    [iv.layer setShadowOpacity:0.75];

}

else if ([cell viewWithTag:012] == nil && comment.isReply == YES) {

    frame.origin.x += 35;

    UIImageView *iv = [[[UIImageView alloc] initWithFrame:frame] autorelease];
    [iv setImage:[UIImage imageNamed:@"paper"]];
    [iv setTag:012];
    [cell insertSubview:iv atIndex:0];

    UIImage *arrow = [UIImage imageNamed:@"arrow"];
    UIImageView *ivs = [[[UIImageView alloc] initWithFrame:CGRectMake(-12, ([cell frame].size.width / 2) + ([arrow size].width/2) , arrow.size.width, arrow.size.height)] autorelease];
    [cell addSubview:ivs];

    [iv.layer setBorderWidth:1.0];
    [iv.layer setBorderColor:[[UIColor whiteColor] CGColor]];

    [iv.layer setShadowColor:[[UIColor blackColor] CGColor]];
    [iv.layer setShadowOffset:CGSizeMake(0, 0)];
    [iv.layer setShadowOpacity:0.75];

}

推荐答案

您应避免在每次加载时操纵单元格,而应在初始化/创建单元格时调整UI.

You should avoid manipulating the cell on each load, instead you should adjust the UI when the cell is initialized/created.

为了说明这一点,每次滚动一个新的单元格(或多个单元格)都可以使用cellForRowAtIndexPath:方法加载,当前您正在对该方法进行很多视图更改,但是在某些情况下不需要这样做(例如,新单元格与刚滚动到屏幕外的单元格的类型相同).将此UI修改移动到初始化单元格的位置,而不是交换数据的位置.您可以使用子类来完成此操作,也可以像这样.

To illustrate, every time you scroll a new cell (or multiple) could be loaded using the cellForRowAtIndexPath: method, currently you are doing a lot of view changes in this method, but there could be cases where this is not needed (for example the new cell is the same type as the one just scrolled off screen). Move this UI modification to where the cell is initialized not where the data is swapped. You could do this with a subclass or simply like this.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Reuse id
    static NSString *identifier1 = @"identifer-1";
    static NSString *identifier2 = @"identifier-2";
    static NSString *regular = @"regular";

    UITableViewCell *cell;

    if (comment.isReply == NO) {
        cell = [tableView dequeueReusableCellWithIdentifier: identifier1];

        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier1] autorelease];

            // Do the UI modification here
        }
    } else if (comment.isReply == YES) {
        cell = [tableView dequeueReusableCellWithIdentifier: identifier2];

        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier2] autorelease];

            // Do the UI modification here
        }
    } else {
        // Regular cell
        cell = [tableView dequeueReusableCellWithIdentifier: regular];

        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: regular] autorelease];
        }
    }

    // Load the data into the cell

    return cell;
}

希望您能找到我要解决的问题,关键是尽可能减少繁琐的工作,并让UITableView缓存发挥更大的作用.

Hope you get where I'm going with this, the key is to do heavy stuff as little as possible and let the UITableView caching have a greater effect.

这篇关于由于阴影和边框导致UITableView滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆