{"id":5939,"date":"2011-02-22T11:36:22","date_gmt":"2011-02-22T10:36:22","guid":{"rendered":"http:\/\/www.devapp.it\/wordpress\/?p=5939"},"modified":"2011-02-22T11:36:22","modified_gmt":"2011-02-22T10:36:22","slug":"t086-creare-una-lista-di-preferiti-parte-2","status":"publish","type":"post","link":"https:\/\/www.devapp.it\/wordpress\/t086-creare-una-lista-di-preferiti-parte-2\/","title":{"rendered":"T#086 \u2013 Creare una lista di Preferiti (Parte 2)"},"content":{"rendered":"<p><a href=\"http:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2011\/02\/t086-Creare-una-lista-di-preferiti-xcode-iphone-ipad-00.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2011\/02\/t086-Creare-una-lista-di-preferiti-xcode-iphone-ipad-00.jpg\" alt=\"t086-Creare-una-lista-di-preferiti-xcode-iphone-ipad-00\" title=\"t086-Creare-una-lista-di-preferiti-xcode-iphone-ipad-00\" width=\"250\" height=\"193\" class=\"alignleft size-full wp-image-5961\" srcset=\"https:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2011\/02\/t086-Creare-una-lista-di-preferiti-xcode-iphone-ipad-00.jpg 250w, https:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2011\/02\/t086-Creare-una-lista-di-preferiti-xcode-iphone-ipad-00-150x115.jpg 150w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a> Rieccoci con la seconda parte del nostro tutorial. Se non avete ancora letto <a href=\"http:\/\/www.devapp.it\/wordpress\/t085-creare-una-lista-di-preferiti-parte-1.html\" target=\"_Blank\">la prima parte<\/a> su come creare una lista di preferiti, vi consiglio di farlo ora. Come promesso, in quest&#8217;ultima parte vedremo come mostrare i preferiti nella tableView che vi avevo preparato, come modificare questa lista e come far si che le righe nella tabella aprano le relative detailView. Iniziamo con la cosa fondamentale: mostrare i preferiti.<\/p>\n<h4>Mostrare la lista dei preferiti<\/h4>\n<p>Dal momento che abbiamo gi\u00e0 tutto pronto (vedi prima parte del tutorial) non ci resta che modificare la classe che gestisce la nostra tableView dei preferiti, il file &#8220;SecondTable.m&#8221;. Per prima cosa, importiamo gli header delle classi che ci permettono di mostrare gli elementi salvati:<!--more--><\/p>\n<pre lang=\"objc\" line=\"1\" escaped=\"true\">\r\n#import \"Favorite.h\"\r\n#import \"FavoritesData.h\"\r\n<\/pre>\n<p>Inseriamo ora il nostro caro osservatore, come abbiamo fatto per salvare i preferiti:<\/p>\n<pre lang=\"objc\" line=\"1\" escaped=\"true\">\r\n- (void) _refreshFav {\r\n\t[self.tableView reloadData];\r\n}\r\n\r\n\/\/ in viewDidLoad\r\n    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_refreshFav) name:REFRESH_FAVORITES object:nil];\r\n\r\n\/\/ in dealloc\r\n    [[NSNotificationCenter defaultCenter] removeObserver:self];\r\n<\/pre>\n<p>Infine, modifichiamo i metodi della tableView come segue, in modo da effettivamente mostrare i preferiti:<\/p>\n<pre lang=\"objc\" line=\"1\" escaped=\"true\">\r\n- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView\r\n{\r\n    \/\/ Return the number of sections.\r\n    return 1;\r\n}\r\n\r\n- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section\r\n{\r\n    \/\/ Return the number of rows in the section.\r\n    return [[FavoritesData sharedFavoritesData] getFavorites].count;\r\n}\r\n\r\n- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath\r\n{\r\n    static NSString *CellIdentifier = @\"Cell\";\r\n    \r\n    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];\r\n    if (cell == nil) {\r\n        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];\r\n    }\r\n    \r\n    \/\/ Configure the cell...\r\n    Favorite *fav = (Favorite *)[[[FavoritesData sharedFavoritesData] getFavorites] objectAtIndex:indexPath.row];\r\n    \r\n\tcell.textLabel.text = fav.favId;\r\n\tcell.detailTextLabel.text = fav.description;\r\n\t\r\n    \/\/ Inserisce l'immagine FullStar al lato sinistro della cella\r\n\tNSString *nomeImmagine = [NSString stringWithFormat:@\"FullStar.png\"];\r\n\t[[cell imageView] setImage:[UIImage imageNamed:nomeImmagine]];\r\n    \r\n    return cell;\r\n}\r\n\r\n\/\/ Cell Accessory\r\n- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {\r\n\treturn UITableViewCellAccessoryDisclosureIndicator;\r\n}\r\n\r\n\/\/ CellHeight\r\n- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {\r\n\treturn 50;\r\n}\r\n<\/pre>\n<p>A questo punto, compilando ed eseguendo il nostro progetto dovremmo essere in grado di poter indicare un elemento come preferito e vederlo nella tableView \u201cpreferiti\u201d. Manca solo il poter selezionare una delle righe in modo che queste aprino le relative detailView:<\/p>\n<pre lang=\"objc\" line=\"1\" escaped=\"true\">\r\n\/\/ importiamo l'header della prima tableView\r\n#include \"FirstTable.h\"\r\n\r\n\/\/ modifichiamo il metodo didSelectRowAtIndexPath\r\n- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath\r\n{\r\n    Favorite *fav = (Favorite *)[[[FavoritesData sharedFavoritesData] getFavorites] objectAtIndex:indexPath.row];\r\n\tNSString *selectedItem = fav.favId;\r\n    \r\n    if ([selectedItem isEqualToString:@\"Titolo Riga 1\"]) {\r\n        Detail1 *detailViewController = [[Detail1 alloc] initWithNibName:@\"Detail1\" bundle:nil];\r\n        [self.navigationController pushViewController:detailViewController animated:YES];\r\n        [detailViewController release];\r\n    } else if ([selectedItem isEqualToString:@\"Titolo Riga 2\"]) {\r\n        Detail2 *detailViewController = [[Detail2 alloc] initWithNibName:@\"Detail2\" bundle:nil];\r\n        [self.navigationController pushViewController:detailViewController animated:YES];\r\n        [detailViewController release];\r\n    } else {\r\n        Detail3 *detailViewController = [[Detail3 alloc] initWithNibName:@\"Detail3\" bundle:nil];\r\n        [self.navigationController pushViewController:detailViewController animated:YES];\r\n        [detailViewController release];\r\n    }\r\n}\r\n<\/pre>\n<p>Notate come sia in questa classe, che in FistTable, abbia usato sempre &#8220;isEqualToString&#8221; e mai &#8220;==&#8221; per confrontare la riga e l&#8217;elenco dei preferiti\/degli elementi. Questo perch\u00e8 altrimenti, alla chiusura dell&#8217;applicazione l&#8217;indirizzo in memoria delle varie stringe cambia e l&#8217;eguaglianza in riapertura dell&#8217;app non \u00e8 pi\u00f9 verificata. <strong>Quando si devono confrontare delle stringe conviene sempre usare &#8220;isEqualToString&#8221;<\/strong>.<\/p>\n<h4>Modificare la tableView: modificare l&#8217;ordine delle celle ed eliminare le righe<\/h4>\n<p>Per concludere, rendiamo la nostra tableView modificabile:<\/p>\n<pre lang=\"objc\" line=\"1\" escaped=\"true\">\r\n\/\/in viewDidLoad\r\nself.navigationItem.rightButtonItem = self.editButtonItem;\r\n\r\n\/\/fuori da viewDidLoad\r\n- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {\r\n    return YES;\r\n}\r\n\r\n- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { \r\n\t\r\n\t\/\/controlla se l'azione compiuta \u00e8 un'eliminazione\r\n\tif (editingStyle == UITableViewCellEditingStyleDelete) {\r\n\t\t\r\n\t\tFavorite *fav = (Favorite *)[[[FavoritesData sharedFavoritesData] getFavorites] objectAtIndex:indexPath.row];\r\n\t\t\r\n\t\tif(fav)\r\n\t\t{\r\n\t\t\t[tableView beginUpdates];\r\n\t\t\t\r\n\t\t\t[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];\r\n\t\t\t[[FavoritesData sharedFavoritesData] removeFavoriteById:fav.favId];\r\n\t\t\t\r\n\t\t\t[tableView endUpdates];\r\n\t\t}\r\n\t}\r\n}\r\n\r\n- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath\r\n{\r\n\t[[FavoritesData sharedFavoritesData] saveFavorites];\r\n}\r\n\r\n- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {\r\n    \/\/ Return NO if you do not want the item to be re-orderable.\r\n    return YES;\r\n}\r\n<\/pre>\n<p>Abbiamo finito. Proviamo a compilare ed eseguire la nostra applicazione e tutto dovrebbe funzionare.<\/p>\n<p>Alla prossima!<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2011\/02\/AddToFavorites-parte-2-finale.zip\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2010\/05\/download_icon.png\" alt=\"\" width=\"33\" height=\"40\" align=\"middle\" \/><\/a> Se avete problemi con il tutorial, <a href=\"http:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2011\/02\/AddToFavorites-parte-2-finale.zip\">questo \u00e8 il nostro file di progetto.<\/a><\/p>\n<p><center><br \/>\n<a href=\"http:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2011\/02\/t086-Creare-una-lista-di-preferiti-xcode-iphone-ipad-01.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2011\/02\/t086-Creare-una-lista-di-preferiti-xcode-iphone-ipad-01.jpg\" alt=\"t086-Creare-una-lista-di-preferiti-xcode-iphone-ipad-01\" title=\"t086-Creare-una-lista-di-preferiti-xcode-iphone-ipad-01\" width=\"380\" height=\"714\" class=\"aligncenter size-full wp-image-5960\" srcset=\"https:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2011\/02\/t086-Creare-una-lista-di-preferiti-xcode-iphone-ipad-01.jpg 380w, https:\/\/www.devapp.it\/wordpress\/wp-content\/uploads\/2011\/02\/t086-Creare-una-lista-di-preferiti-xcode-iphone-ipad-01-159x300.jpg 159w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/a><br \/>\n<\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rieccoci con la seconda parte del nostro tutorial. Se non avete ancora letto la prima parte su&#8230;<\/p>\n","protected":false},"author":539,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[574,88,72,242,133,27],"class_list":["post-5939","post","type-post","status-publish","format-standard","hentry","category-tutorial-pratici","tag-gestione-preferiti-xcode","tag-programmazione-iphone","tag-tabbar","tag-tutorial-xcode","tag-uitable","tag-uitableview"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/posts\/5939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/users\/539"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/comments?post=5939"}],"version-history":[{"count":10,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/posts\/5939\/revisions"}],"predecessor-version":[{"id":5965,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/posts\/5939\/revisions\/5965"}],"wp:attachment":[{"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/media?parent=5939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/categories?post=5939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devapp.it\/wordpress\/wp-json\/wp\/v2\/tags?post=5939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}