Created by Guo Chai April 23, 2015 23:18:08
// ===================================
The implementation of the simple Address Book:
// ===================================
implement group show to show student contacts.
1. To use the TableView exhibition to show the student address book, according to the contact person group to make use of the partition display to contact the person, the partition header shows the group name (A~Z). and provides a partitioned index.
2. How to use the UITableViewCell exhibition to contact the person's avatar, name, contact? 3. Check the contact details of the person entering into the page, display the contact information (avatar, name, gender, yearage, contact, style, hobby). 4, define the Model class (Student). Make use of contactlist.plist to provide a data source.
implementation status (some rotten):
Pictures and names are a bit rotten, mainly small functions have been realized.
is mainly based on the UITableView to achieve.
The data is stored in the plist file, and the data is removed and encapsulated in the student:
NSDictionary * DIC = [[Nsdictionary alloc]initwithcontentsoffile:@]/users/lanou3g/desktop/lesson/Address Book practice/Contacts Practice/contact List.plist "] ; Nsmutabledictionary *contactdic = [Nsmutabledictionary dictionary]; Self.contactdic = Contactdic; For (NSString * key in dic) {Nsarray * arr = [dic Objectforkey:key]; Nsmutablearray * Contactsarray = [Nsmutablearray array]; For (Nsdictionary * PDic in arr) {person * p = [[Person alloc]init]; P.name = [PDic objectforkey:@ "name"]; P.sex = [PDic objectforkey:@ "sex"]; P.photo = [PDic objectforkey:@ "Photo"]; P.age = [[PDic objectforkey:@ "Age"] intvalue]; [Contactsarray Addobject:p]; [P release]; } [Contactdic Setobject:contactsarray Forkey:key]; }
Number of rows in a partition:
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{ nsstring * key = [[ Self.contactdic AllKeys] objectatindex:section]; Long Count = [[Self.contactdic Objectforkey:key] count]; return count;}
Number of partitions in the Address book:
-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{ return [[Self.contactdic AllKeys] count];}
Use the reuse mechanism to create a cell:
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ UITableViewCell * cell = [TableView dequeuereusablecellwithidentifier:@ "Guozai"]; if (!cell) { cell = [[[UITableViewCell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@] GuoZai [] autorelease]; Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator; } Nsarray * arr = getpersons (indexpath,self.contactdic); Person * per = [arr ObjectAtIndex:indexPath.row]; Cell.textLabel.text = Per.name; Cell.detailTextLabel.text = Per.sex; Cell.imageView.image = [UIImage ImageNamed:per.photo]; return cell;}
In my own encapsulation method, take out the number of student in the dictionary:
nsarray* getpersons (Nsindexpath *indexpath,nsmutabledictionary * dic) { NSString * key = [[dic AllKeys] Objectatindex : Indexpath.section]; Nsarray * arr = [dic objectforkey:key]; return arr;}
Area title:
-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{ return [[ Self.contactdic AllKeys] objectatindex:section];}
Edit Status:
-(void) Rightbtnclick: (Uibarbuttonitem *) btn{ [Self.tableview setediting:yes animated:yes];}
Rows that can be edited:
-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) indexpath{ if (! Indexpath.section &&!indexpath.row) { return NO; } return YES;}
Edit format:
-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{ if (!indexpath.section && indexpath.row = = 1) { return uitableviewcelleditingstyledelete; } else return uitableviewcelleditingstyleinsert;}
Edit Complete: Move data
-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{ if (Editingstyle = = uitableviewcelleditingstyledelete) { Nsmutablearray * arr = (Nsmutablearray *) getpersons (Indexpath, _contactdic); [Arr RemoveObjectAtIndex:indexPath.row]; [TableView Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationleft]; } if (Editingstyle = = Uitableviewcelleditingstyleinsert) {person * p = [[person alloc]init]; P.name = @ "Guo Chai"; P.age = +; P.sex = @ "male"; Nsmutablearray * arr = (Nsmutablearray *) getpersons (Indexpath, _contactdic); [arr insertobject:p AtIndex:indexPath.row]; [TableView Insertrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationleft];} }
Three steps to move:
-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) indexpath{return YES; -(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: ( Nsindexpath *) destinationindexpath{Nsmutablearray * Oldarr = (Nsmutablearray *) getpersons (SourceIndexPath, Self.conta Ctdic); person * p = [[Oldarr ObjectAtIndex:sourceIndexPath.row] retain]; [Oldarr RemoveObjectAtIndex:sourceIndexPath.row]; [TableView Deleterowsatindexpaths:@[sourceindexpath] withrowanimation:uitableviewrowanimationleft]; Nsmutablearray * NEWARR = (Nsmutablearray *) getpersons (Destinationindexpath, self.contactdic); [NewArr insertobject:p AtIndex:destinationIndexPath.row]; [P release];} -(Nsindexpath *) TableView: (UITableView *) TableView Targetindexpathformovefromrowatindexpath: (Nsindexpath *) Sourceindexpath Toproposedindexpath: (Nsindexpath *) proposeddestinationindexpath{if (sourceindexpath.section = = Proposeddestinationindexpath.sectiOn) {return proposeddestinationindexpath; } else return sourceindexpath;}Page jump, view details page:
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{ Thirdviewcontroller * THIRDVC = [[Thirdviewcontroller alloc]init]; Nsmutablearray * arr = (Nsmutablearray *) getpersons (Indexpath, _contactdic); Thirdvc.per = [arr objectAtIndex:indexPath.row]; [Self.navigationcontroller PUSHVIEWCONTROLLER:THIRDVC Animated:yes]; [THIRDVC release];}
The attribute is used to pass the value.
Some memory release issues themselves consider putting.
Implementation of UI development----Simple Address Book