1 Preface
When I was working yesterday, I encountered the index creation problem for tableview. Sometimes, because the data volume in tableview is so large that I need to create an index on the right side to search for it. Today I specially sorted it out below for your reference, learn from each other.
2. code example
Zyviewcontroller. h
# Import <uikit/uikit. h> @ interface zyviewcontroller: uitableviewcontroller <uitableviewdatasource, uitableviewdelegate> // set the index title @ property (nonatomic, retain) nsmutablearray * indexarray; // set the cell content under each section @ property (nonatomic, retain) nsarray * dataarray1; @ property (nonatomic, retain) nsarray * dataarray2; @ property (nonatomic, retain) nsarray * dataarray3; @ end
Zyviewcontroller. m
@ Synthesize dataarray1, dataarray2, dataarray3; @ synthesize indexarray;-(void) viewdidload {[Super viewdidload]; nsarray * array1 = [[nsarray alloc] Comment: @ "", @ "B", @ "C", @ "D", @ "E", nil]; nsarray * array2 = [[nsarray alloc] initwithobjects: @ "F ", @ "g", @ "H", @ "I", @ "J", nil]; nsarray * array3 = [[nsarray alloc] initwithobjects: @ "K ", @ "L", @ "M", @ "N", @ "O", nil]; self. dataarray1 = array1; self. dataarray2 = array2; Self. dataarray3 = array3; // assign nsmutablearray * array = [[nsmutablearray alloc] initwithcapacity: 5] to the array; self. indexarray = array; [array release]; [indexarray addobjectsfromarray: array1]; [indexarray objects: array2]; [indexarray objects: array3]; [array1 release]; [array2 release]; [array3 release];} // set the header value of the section-(nsstring *) tableview :( uitableview *) tableviewtitlefor Headerinsection :( nsinteger) Section {nsstring * Key = [indexarray objectatindex: section]; Return key ;} # pragma mark-# pragma mark table View data source methods // set the index array of the table-(nsarray *) sectionindextitlesfortableview :( uitableview *) tableview {return self. indexarray;} // allow the data source to inform you of the number of sections of the table that must be loaded to table view. -(Nsinteger) numberofsectionsintableview :( uitableview *) tableview {return 3;} // you can specify the number of rows in the table as the number of elements in the array) section {If (Section = 0) {return [self. dataarray1 count];} else if (Section = 1) return dataarray2.count; else return dataarray3.count ;} // The content of each row is the value of the corresponding index of the array // customize the appearance of table View cells. -(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * handle = @ "cell"; uitableviewcell * cell = [tableview progress: cellidentifier]; if (cell = nil) {Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier]; cell. accessorytype = uitableviewcellaccessorydisclosureindicator;} If (indexpath. section = 0) // set the cell string content. textlabel. TEXT = [Self-> dataarray1 objectatindex: indexpath. row]; else if (indexpath. section = 1) // set the cell string content. textlabel. TEXT = [Self-> dataarray2 objectatindex: indexpath. row]; else // set the cell string content cell. textlabel. TEXT = [Self-> dataarray3 objectatindex: indexpath. row]; return cell;}-(void) dealloc {[indexarray release]; [dataarray1 release]; [dataarray2 release]; [dataarray3 release]; [Super dealloc];}
Running result:
3 conclusion
The above is all content and I hope it will help you.
Demo: http://download.csdn.net/detail/u010013695/5347887