16. Primeiro de tudo: precisamos aprender
a customizar uma célula
@interface DCCustomCell : UITableViewCell
@end
16 @dchohfi
17. Podemos até criar um XIB pra ela :D
desenhamos a tela temos que LEMBRAR
do identifier
alteramos a classe ta interface
principal para a nossa customizada
e para acessar os elementos visuais, criamos IBOutlet
17 @dchohfi
18. e a API evoluiu bastante :D
iOS > 5
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
rigistramos o novo xib na tabela
UINib *customNib = [UINib nibWithNibName:@"DCCustomCell"
bundle:[NSBundle mainBundle]];
[self.tableView registerNib:customNib forCellReuseIdentifier:@"DCCustomCell"];
e podemos remover um if! o/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"DCCustomCell"];
18 @dchohfi
20. Agora precisamos melhorar a performance.
Primeira regra:
NUNCA FAÇA DOWNLOAD SÍNCRONO
NSURL *avatarURL = [NSURL URLWithString:user[@"profile_image_url"]];
self.avatar.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:avatarURL]];
a cada download da imagem, tudo trava, inclusive seu
usuário..
20 @dchohfi
24. Mas, ainda temos problema de performance
cada subview é desenhada separadamente
e agora?
temos que desenhar tudo de uma
vez! sem xib :(
24 @dchohfi
25. Podemos desenhar todos os conteúdos estáticos de uma
vez só!
- (CGSize)drawAtPoint:(CGPoint)point
forWidth:(CGFloat)width
NSString withFont:(UIFont *)font
lineBreakMode:(NSLineBreakMode)lineBreakMode;
UIImage - (void)drawInRect:(CGRect)rect;
quando a célula for desenhada
- (void)drawRect:(CGRect)rect;
25 @dchohfi
26. Porém sempre que o conteúdo da célula mudar,
precisamos mandar ela se redesenhar.
setNeedsDisplay
- (void) setTweetData: (NSDictionary *) tweet {
self.twitterData = tweet;
NSURL *userAvatarUrl = [NSURL URLWithString:self.twitterData[@"user"][@"profile_image_url"]];
[self.avatar setImageWithURL:userAvatarUrl];
[self setNeedsDisplay];
}
26 @dchohfi
28. E quando queremos celulas com alturas variaveis?
podemos saber o tamanho de um texto, com uma fonte, de
maneira simples:
- (CGSize)sizeWithFont:(UIFont *)font
NSString constrainedToSize:(CGSize)size;
agora nós temos o controle :D
- (CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
28 @dchohfi
29. Cuidados que precisamos tomar
reutilizar as celulas sempre que possível
evitar desenhar todas as subviews,
drawRect: FTW!
evitar sombras e bordas arredondas
evitar desenhar elementos
com coordenadas quebradas
deixar tudo calculado antes
29 @dchohfi