mobile conf
TRANSCRIPT
@dchohfi1
UITableView
@dchohfi2
Die!o Chohfi TuriniInstutor na Caelum
Desenvolvedor na MakeYou
@dchohfi
@dchohfi3
BusaoSPEpicLyrics
@dchohfi4
UITableViewUITableViewStylePlain UITableViewStyleGrouped
@dchohfi5
UITableViewUITableViewStylePlain
muito rápido
customizável
UIScrollView
@dchohfi6
section
} rows
section
} rows
}NSIndexPath
}NSIndexPath
@dchohfi7
UITableViewCell
Header (UIView)
Tudo é classe! Lembre-se que é orientado a objetos!
@dchohfi8
NSIndexPath
informação
UITableViewCell
UITableViewCellStyle
initWithStyle:reuseIdentifier:
@dchohfi9
<UITableViewDataSource>
UITableView
@interface DCViewController : UIViewController <UITableViewDataSource>
self.tableView.dataSource = self;
@dchohfi10
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
@dchohfi11
100 linhas = 100 UITableViewCellUITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"Cell"];
@dchohfi12
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
dequeueReusableCellWithIdentifier:
if(!cell){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];}
@dchohfi13
Ok, mas como sair disso:
@dchohfi14
Pra isso:
@dchohfi15
@dchohfi16
Primeiro de tudo: precisamos aprender a customizar uma célula
@interface DCCustomCell : UITableViewCell
@end
@dchohfi17
Podemos até criar um XIB pra ela :Ddesenhamos a tela
alteramos a classe ta interface principal para a nossa customizada
temos que LEMBRAR do identifier
e para acessar os elementos visuais, criamos IBOutlet
@dchohfi18
e a API evoluiu bastante :D
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
iOS > 5
e podemos remover um if! \o/
UINib *customNib = [UINib nibWithNibName:@"DCCustomCell" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:customNib forCellReuseIdentifier:@"DCCustomCell"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"DCCustomCell"];
ri!istramos o novo xib na tabela
@dchohfi19
@dchohfi20
A!ora precisamos melhorar a performance.
Primeira re!ra: 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..
@dchohfi21
open source FTW!
@dchohfi22
SDWebIma!e
@dchohfi23
Muito simples!
[self.avatar setImageWithURL:[NSURL URLWithString:user[@"profile_image_url"]]];
@dchohfi24
Mas, ainda temos problema de performance
cada subview é desenhada separadamente
e a!ora?
temos que desenhar tudo de uma vez! sem xib :(
@dchohfi25
Podemos desenhar todos os conteúdos estáticos de uma vez só!
NSString- (CGSize)drawAtPoint:(CGPoint)point forWidth:(CGFloat)width withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode;
- (void)drawRect:(CGRect)rect;
quando a célula for desenhada
UIImage - (void)drawInRect:(CGRect)rect;
@dchohfi26
Porém sempre que o conteúdo da célula mudar, precisamos mandar ela se redesenhar.
- (void) setTweetData: (NSDictionary *) tweet { self.twitterData = tweet; NSURL *userAvatarUrl = [NSURL URLWithString:self.twitterData[@"user"][@"profile_image_url"]]; [self.avatar setImageWithURL:userAvatarUrl]; [self setNeedsDisplay];}
setNeedsDisplay
@dchohfi27
@dchohfi28
E quando queremos celulas com alturas variaveis?
podemos saber o tamanho de um texto, com uma fonte, de maneira simples:
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size;
a!ora nós temos o controle :D
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
NSString
@dchohfi29
Cuidados que precisamos tomar
evitar desenhar elementos com coordenadas quebradas
evitar desenhar todas as subviews,drawRect: FTW!
reutilizar as celulas sempre que possível
evitar sombras e bordas arredondas
deixar tudo calculado antes
@dchohfi30
CUIDADO MANO!
@dchohfi31
10% de desconto nos livros da
Cupom: MOBILECONF no site
10% de desconto nos cursos da Caelum também \o/
@dchohfi32
valeu galera :D