Pregunta

Tengo el siguiente código para agregar un color de borde y una sombra de caída al fondo de mi UableViewcell. Mi problema es que este código causa un gran retraso en la vista de tabla.

Por favor, ¿puede decirme cómo puedo optimizar mi código, evitando el retraso de 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];

}
¿Fue útil?

Solución

Debe evitar manipular la celda en cada carga, en su lugar, debe ajustar la interfaz de usuario cuando la celda se inicialice/crea.

Para ilustrar, cada vez que desplaza una nueva celda (o múltiple) podría cargarse utilizando el cellForRowAtIndexPath: Método, actualmente está haciendo muchos cambios de vista en este método, pero podría haber casos en los que esto no sea necesario (por ejemplo, la nueva celda es del mismo tipo que el que acaba de desplazarse fuera de la pantalla). Mueva esta modificación de la interfaz de usuario a donde se inicializa la celda no donde se intercambian los datos. Podrías hacer esto con una subclase o simplemente así.

- (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;
}

Espero que llegues a donde voy con esto, la clave es hacer cosas pesadas lo menos posible y dejar que el almacenamiento en caché de UableView tenga un mayor efecto.

Otros consejos

Además de los otros consejos de optimización aquí, especificando un shadowPath en tu CALayer mejorará el rendimiento del dibujo de la sombra. Podrías determinar un camino para la sombra con algo como esto ...

iv.layer.shadowPath = [UIBezierPath bezierPathWithRect:iv.bounds].CGPath;

También es posible que desee mirar el shouldRasterize Bit en Calayer. Esto hace que la capa se renderice como un mapa de bits. Asegúrese de proporcionar también una escala de Rasterizations que coincida con su dispositivo si sigue esta ruta.

cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top