20.04.2013 Views

Tutorial pratici per iPhone SDK v1.6 - Get a Free Blog

Tutorial pratici per iPhone SDK v1.6 - Get a Free Blog

Tutorial pratici per iPhone SDK v1.6 - Get a Free Blog

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

isponderà alla cella che abbiamo creato poco fa in Interface Builder.<br />

Alla riga 11, infine, troviamo la definizione di un metodo “scale”, che ci <strong>per</strong>metterà di ridimensionare<br />

l’immagine <strong>per</strong> essere contenuta interamente nella UIImageView della cella <strong>per</strong>sonalizzata.<br />

Questo ci <strong>per</strong>metterà di non sprecare memoria inutilmente.<br />

Definiamo, ora, il contenuto del file “TabellaTwitterViewController.m”. Iniziamo inserendo le<br />

seguenti intestazioni:<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

#import "TabellaTwitterViewController.h"<br />

#import "Tweet.h"<br />

@implementation TabellaTwitterViewController<br />

@synthesize lista, cellaNib;<br />

Modifichiamo, poi, il metodo “tableView: numberOfRowInSection:” impostando come valore<br />

di ritorno la dimensione della lista:<br />

1<br />

2<br />

3<br />

- (NSInteger)tableView:(UITableView *)tableView<br />

numberOfRowsInSection:(NSInteger)section {<br />

return [lista count];<br />

}<br />

Ora dobbiamo implementare il metodo “tableView: cellForRowAtIndexPath:”. È un po’ diverso<br />

dal solito, eccovelo:<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

14<br />

15<br />

16<br />

17<br />

18<br />

19<br />

20<br />

21<br />

- (UITableViewCell *)tableView:(UITableView *)tableView<br />

cellForRowAtIndexPath:(NSIndexPath *)indexPath {<br />

static NSString *CellIdentifier = @"Cell";<br />

UITableViewCell *cell = [tableView<br />

dequeueReusableCellWithIdentifier:CellIdentifier];<br />

if (cell == nil) {<br />

! ! [[NSBundle mainBundle] loadNibNamed:@"cellaTweet"<br />

owner:self options:NULL];<br />

! ! cell = cellaNib;<br />

}<br />

!<br />

! Tweet *elemento = [lista objectAtIndex:indexPath.row];<br />

! UILabel *testoLabel = (UILabel*)[cell viewWithTag:2];<br />

! testoLabel.text = elemento.testo;<br />

! UILabel *userLabel = (UILabel*)[cell viewWithTag:1];<br />

! userLabel.text = elemento.user;<br />

!<br />

! UIImageView *immagine = (UIImageView*)[cell viewWithTag:3];<br />

! [immagine setImage:[self scale:elemento.immagine<br />

toSize:CGSizeMake(60, 60)]];<br />

!<br />

! return cell;<br />

}<br />

<strong>Tutorial</strong> <strong>pratici</strong> <strong>per</strong> <strong>iPhone</strong> <strong>SDK</strong>! 150

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!