UITableView и CALayer путаница - PullRequest
       7

UITableView и CALayer путаница

0 голосов
/ 08 апреля 2011

Я получил очень простой UITableViewCell подкласс. MyTableViewCell.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>


@interface MyTableViewCell : UITableViewCell {
    CALayer *backgroundLayer;
}

@end

MyTableViewCell.m

#import "MyTableViewCell.h"
#import <QuartzCore/QuartzCore.h>


@implementation MyTableViewCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        backgroundLayer = [[CALayer alloc] init];
        [backgroundLayer setBackgroundColor:[[UIColor yellowColor] CGColor]];
        [[self layer] addSublayer:backgroundLayer];
    }
    return self;
}


- (void)layoutSublayersOfLayer:(CALayer *)layer {
    [backgroundLayer setFrame:[layer frame]];
    [super layoutSublayersOfLayer:layer];
}


- (void)dealloc {
    [backgroundLayer release];
    [super dealloc];
}


@end

Странно, вот результат: enter image description here.

Может кто-нибудь объяснить это ?! Почему слой не рисует все ячейки, а только каждую вторую ячейку?

РЕДАКТИРОВАТЬ: Вот мой tableView:cellForRowAtIndexPath: метод:

static NSString *reuseIdentifier = @"cellReuseIdentifier";

MyTableViewCell *cell = (MyTableViewCell *)[aTableView dequeueReusableCellWithIdentifier:reuseIdentifier];

if (cell == nil)
    cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];

return cell;

Ответы [ 2 ]

0 голосов
/ 25 ноября 2011

Хотя это не относится к вашему вопросу, вы должны добавить свои слои в [слой self.contentView].В противном случае функции редактирования, такие как delete-swipe, не будут отображаться правильно.

0 голосов
/ 08 апреля 2011

Используя этот метод, добились цели:

- (void)layoutSublayersOfLayer:(CALayer *)layer {
    CGRect frame = [layer frame];
    frame.origin = CGPointZero;
    [backgroundLayer setFrame:frame];
    [super layoutSublayersOfLayer:layer];
}
...