You can achieve this in two ways:
1. Use the data in the dynamic array nsmutablearray to display the data
1. Create an empty application project, create viewcontroller and homeviewcontroller, and import the file in appdelegate. M,
And add the following red flag code to method-(bool) Application: didfinishlaunchingwitexceptions:
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {self. window = [[[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds] autorelease]; // override point for customization after application launch. self. window. backgroundcolor = [uicolor whitecolor]; homeviewcontroller * homeviewcontroller = [[homeviewcontroller alloc] init]; self. window. rootviewcontroller = homeviewcontroller; [homeviewcontroller release]; [self. window makekeyandvisible]; return yes;
}
2. Add the table view control on homeviewcontroller. XIB.
Associate the datasource and delegate of outlets with the file's owner,
Purpose: (1) datasource: add the uitableviewdatasource protocol to homeviewcontroller to display data in table view by using related Protocol methods in this class.
(2) delegate: add the uitableviewdelegate protocol to homeviewcontroller, so that you can use the relevant protocol methods in this class to respond to user interaction operations in Table view.
Add protocol in homeviewcontroller. h
# Import <uikit/uikit. h> @ interface homeviewcontroller: uiviewcontroller <uitableviewdelegate, uitableviewdatasource >{}@ end
Objective: to add all the protocols and be prepared. Improve the efficiency and reliability of code writing.
3. write code in homeviewcontroller. m
# Import "homeviewcontroller. H "@ interface homeviewcontroller () @ end @ implementation parameters * listofcontacts; // declare a dynamic array-(void) viewdidload {listofcontacts = [[nsmutablearray alloc] init]; // allocate memory and initialize [listofcontacts addobject: @ "Zhang San"]; [listofcontacts addobject: @ "Zhang 1"]; [listofcontacts addobject: @ "Zhang 2"]; [listofcontacts addobject: @ "3"]; [listofcontacts addobject: @" 4"]; [listofcontac TS addobject: @ "5"]; [listofcontacts addobject: @" 6"]; [listofcontacts addobject: @ "7"]; [listofcontacts addobject: @" 8"]; [listofcontacts addobject: @ "9"]; [listofcontacts addobject: @" 11"]; [listofcontacts addobject: @ "12"]; [listofcontacts addobject: @" 13"]; [listofcontacts addobject: @ "14"]; [listofcontacts addobject: @" 15"]; [listofcontacts addobject: @ "16"]; [listofcontacts addobject: @" 17"]; [Listofcontacts addobject: @ "18"]; [listofcontacts addobject: @" 19"]; [Super viewdidload];} // tableview using uitableviewdatasource Protocol: cellforrowatindexpath: method // This method is used to fill the data into the cells in the table view./* this event is triggered when the data of each cell is filled in the table view. NOTE: If we have a total of 200 data items, it does not mean that this event will be triggered for 200 consecutive times. If the current screen only displays 10 rows of data, it will only trigger 10 times, this event will continue to be triggered only when the user rolls the table view to generate a new cell. */-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * cellindentifier = @ "Contact"; uitableviewcell * cell = [tableview progress: cellindentifier];/* dequeuereusablecellwithidentifier method to obtain uitableviewcell-type objects. It also obtains an object that has been used and can be reused in tableview. Imagine: If the array contains 1000 elements and we instantiate a uitableviewcell object for each element, the system will overflow or even crash. In fact, each user can see more than a dozen cells on a screen. By scrolling up and down the screen, they can eliminate some displayed cells, these cell object systems will be retained for reuse when we need to display new cells, thus saving system resources. This method contains the cellidentifier parameter, which is used to specify the reusable cell of the ID you need. It is useful when multiple tables exist on the same interface. Of course, if we do not obtain reusable cells, we need to use the initwithstyle: reuseidentifier: Method of uitableviewcell to directly instantiate a cell. The reuseidentifier parameter is used to set the reusable identifier of the table. */If (cell = nil) {Cell = [[[uitableviewcell alloc] initwithstyle: Invalid reuseidentifier: cellindentifier] autorelease];} nsstring * cellvalue = [Your objectatindex: indexpath. row]; cell. textlabel. TEXT = cellvalue;
// Flag: Disclosure indicator, disclosure button, and checkmark. The default value is none.
// Cell. accessorytype = uitableviewcellaccessorydisclosureindicator;
// Cell. accessorytype = uitableviewcellaccessorydetaildisclosurebutton; // cell. accessorytype = uitableviewcellaccessorycheckmark; cell. accessorytype = uitableviewcellaccessorynone; // Add image in cell uiimage * image = [uiimage imagenamed: @ "avatar.png"]; cell. imageview. image = image; return cell;} // use the tableview: numberofrowsinsection of uitableviewdatasource Protocol: Method // This method is used to set the number of rows in Table view to display data-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {return [listofcontacts count];} // Add title and script information-(nsstring *) tableview :( uitableview *) tableview titleforheaderinsection :( nsinteger) Section {
Return @ "Contact List ";
}-(Nsstring *) tableview :( uitableview *) tableview titleforfooterinsection :( nsinteger) Section {return @ "Author: What if ";}
// Uitableviewdelegate protocol method, select project-(void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {nsstring * contactselected = [listofcontacts objectatindex: [indexpath row]; nsstring * MSG = [[nsstring alloc] initwithformat: @ "your selected contact: % @", contactselected]; uialertview * Alert = [[uialertview alloc] initwithtitle: @ "select contact" message: MSG delegate: Self cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; [alert show]; [alert release]; [contactselected release]; [MSG release];} // method of the uitableviewdelegate protocol, indent in the table-(nsinteger) tableview :( uitableview *) tableview indentationlevelforrowatindexpath :( nsindexpath *) indexpath {return [indexpath row] % 9;}-(void) dealloc {
[Listofcontacts release]; [Super dealloc];}-(void) viewdidunload {[Super viewdidunload];}-(bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation {return (interfaceorientation = uiinterfaceorientationportrait);} @ end
2. Provide data through the plist file to display data and facilitate grouping
1. Add the contacts. plist File
2.
Add code in homeviewcontroller. h
# Import <uikit/uikit. h> @ interface homeviewcontroller: uiviewcontroller <uitableviewdelegate, uitableviewdatasource >{}@ property (nonatomic, retain) nsdictionary * contacttitles; // store all the contact information @ property (nonatomic, retain) nsarray * groups; // All category names are stored in the array
@ End
3. Add code in homeviewcontroller. m
# Import "homeviewcontroller. H "@ interface homeviewcontroller () @ end @ implementation homeviewcontroller @ synthesize contacttitles; @ synthesize groups;-(void) viewdidload {nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "contacts" oftype: @ "plist"]; // plist file path nsdictionary * dict = [[nsdictionary alloc] initwithcontentsoffile: path]; self. contacttitles = dict; [dict release]; nsarray * array = [[contacttitles allkeys] sortedarrayusingselector: @ selector (compare :)]; self. groups = array; [Super viewdidload];} // tableview using the uitableviewdatasource Protocol: cellforrowatindexpath: method-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * cellindentifier = @ "Contact"; uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellindentifier]; If (cell = nil) {Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellindentifier] autorelease];} nsstring * Group = [groups objectatindex: [indexpath section]; nsarray * contactsection = [contacttitles objectforkey: Group]; cell. textlabel. TEXT = [contactsection objectatindex: [indexpath row]; // Add an image to a cell. uiimage * image = [uiimage imagenamed: @ "avatar.png"]; cell. imageview. image = image; return cell;}-(nsinteger) numberofsectionsintableview :( uitableview *) tableview {return [groups count];} // tableview using the uitableviewdatasource Protocol: numberofrowsinsection: method // This method is used to set the number of rows of data to be displayed in table view-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {nsstring * Group = [groups objectatindex: section]; nsarray * contactsection = [contacttitles objectforkey: Group]; return [contactsection count];} // Add the title and script information-(nsstring *) tableview :( uitableview *) tableview titleforheaderinsection :( nsinteger) Section {nsstring * Group = [groups objectatindex: section]; return group;}-(nsstring *) tableview :( uitableview *) tableview titleforfooterinsection :( nsinteger) section {return @ "Author: What if ";}
/* // Uitableviewdelegate protocol method, select project-(void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {nsstring * contactselected = [listofcontacts objectatindex: [indexpath row]; nsstring * MSG = [[nsstring alloc] initwithformat: @ "your selected contact: % @", contactselected]; uialertview * Alert = [[uialertview alloc] initwithtitle: @ "select contact" message: MSG delegate: Self cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; [alert show]; [alert release]; [contactselected release]; [MSG release];} * // uitableviewdelegate protocol method, indent in the table-(nsinteger) tableview :( uitableview *) tableview indentationlevelforrowatindexpath :( nsindexpath *) indexpath {return [indexpath row] % 9 ;}*/
// Index function
-(Nsarray *) sectionindextitlesfortableview :( uitableview *) tableview {return groups;} // events triggered after a user clicks the flag. Only the detaildisclosure button has this event-(void) tableview :( uitableview *) tableview accessorybuttontappedforrowwithindexpath :( nsindexpath *) indexpath {// enter the details page of the project.
}
-(Void) dealloc {[contacttitles release]; [groups release]; [Super dealloc];}-(void) viewdidunload {[Super viewdidunload];}-(bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation {return (interfaceorientation = uiinterfaceorientationportrait);} @ end
4. Change the style to grouped in the XIB file.
Display Effect:
Note:
How to modify cells:
Textlabel. Font use uifont to set the font of cell labels
Textlabel. linebreakmode use uilinebreakmode to specify how the text of the cell label is wrapped
Textlabel. text sets the content of the cell tag to an nsstring
Textlabel. textalignment use uitextalignment to set the cell label text alignment
Textlabel. textcolor use uicolor to set the color of the cell label text
Textlabel. selectedtextcolor use uicolor to set the color of the selected text
Imageview. Image: sets the Image view of a cell to a uiimage.
Imageview. selectedimage: Set the content of the selected cell to uiimage.