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
[Plain]
# 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
# 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
[Plain]
@ Synthesize dataArray1, dataArray2, dataArray3;
@ Synthesize indexArray;
-(Void) viewDidLoad
{
[Super viewDidLoad];
NSArray * array1 = [[NSArray alloc] initWithObjects: @ "A", @ "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 values to the array
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity: 5];
Self. indexArray = array;
[Array release];
[IndexArray addObjectsFromArray: array1];
[IndexArray addObjectsFromArray: array2];
[IndexArray addObjectsFromArray: array3];
[Array1 release];
[Array2 release];
[Array3 release];
}
// Set the Header value of the Section
-(NSString *) tableView :( UITableView *) tableView
TitleForHeaderInSection :( 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 indicate the number of sections of the Table that must be loaded to Table View.
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
Return 3;
}
// Set the number of rows in the table to the number of elements in the array.
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) 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 * CellIdentifier = @ "Cell ";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
If (cell = nil)
{
Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
Cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
If (indexPath. section = 0)
// Set the cell string content
Cell. textLabel. text = [self-> dataArray1 objectAtIndex: indexPath. row];
Else if (indexPath. section = 1)
// Set the cell string content
Cell. 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];
}
@ Synthesize dataArray1, dataArray2, dataArray3;
@ Synthesize indexArray;
-(Void) viewDidLoad
{
[Super viewDidLoad];
NSArray * array1 = [[NSArray alloc] initWithObjects: @ "A", @ "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 values to the array
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity: 5];
Self. indexArray = array;
[Array release];
[IndexArray addObjectsFromArray: array1];
[IndexArray addObjectsFromArray: array2];
[IndexArray addObjectsFromArray: array3];
[Array1 release];
[Array2 release];
[Array3 release];
}
// Set the Header value of the Section
-(NSString *) tableView :( UITableView *) tableView
TitleForHeaderInSection :( 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 indicate the number of sections of the Table that must be loaded to Table View.
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
Return 3;
}
// Set the number of rows in the table to the number of elements in the array.
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) 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 * CellIdentifier = @ "Cell ";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
If (cell = nil)
{
Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
Cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
If (indexPath. section = 0)
// Set the cell string content
Cell. textLabel. text = [self-> dataArray1 objectAtIndex: indexPath. row];
Else if (indexPath. section = 1)
// Set the cell string content
Cell. 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: