IOS-UITabView example
Summary
This chapter describes how to use UITabView. You can use the three pages of UITabViewController to demonstrate different situations, including simple tables, segmented tables with subtitles and index tables.
Running result
Project Construction Overview
1. For how to use UITabViewController, refer to previous articles.
2. The UITabView control class must implement the data source and proxy methods corresponding to the control. At the same time, you must specify the data source and proxy of the UITabView in IB as the control class of the interface.
3. the main implementation method involves several segments, the number of lines in each segment, how to draw each cell, and what the segment title is. If you need an index, You have to implement the index title and click the index title to locate the segment.
4. Because this project does not use ARC, there was a memory management problem at the beginning, mainly because the member variable value assignment was a light copy. The correct method is self. _ value = value, instead of _ vale = value. The former calls the setting method, while the latter only assigns a value to the pointer.
Main Code and comments
H file
/// IndexViewController. h // TableViewDemo /// Created by arbboter on 14/12/5. // Copyright (c) 2014 arbboter. All rights reserved. // # import
@ Interface IndexViewController: UIViewController
{NSMutableArray * _ arraySection; NSMutableArray * _ arrayMember; NSArray * _ arrayIndex;} @ property (nonatomic, retain) NSMutableArray * _ arraySection; @ property (nonatomic, retain) NSMutableArray * _ arrayMember; @ property (nonatomic, retain) NSArray * _ arrayIndex; @ end
M file
/// IndexViewController. m // TableViewDemo /// Created by arbboter on 14/12/5. // Copyright (c) 2014 arbboter. all rights reserved. // # import "IndexViewController. h "@ implementation IndexViewController @ synthesize _ arrayMember; @ synthesize _ arraySection; @ synthesize _ arrayIndex; # pragma UITableViewDataSource
// The number of rows in the corresponding segment-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {NSInteger ret = 0; ret = [[_ arraySection objectAtIndex: section] count]; return ret;} // draw a cell-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = nil; static NSString * CellIdentifier = @ "Cell1"; cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; if (cell = nil) {cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: CellIdentifier] autorelease];} cell. textLabel. text = [[_ arraySection objectAtIndex: indexPath. section] objectAtIndex: indexPath. row]; cell. imageView. image = [UIImage imageNamed: [NSString stringWithFormat: @ "%@.png", [_ arrayIndex objectAtIndex: indexPath. section]; return cell;} // index Title List-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {NSMutableArray * index = [[NSMutableArray alloc] init]; for (int I = '0'; I <= '9'; I ++) {[index addObject: [NSString stringWithFormat: @ "% c", I];} for (int I = 'a'; I <= 'Z'; I ++) {[index addObject: [NSString stringWithFormat: @ "% c", I];} self. _ arrayIndex = index; [index release]; return _ arrayIndex;} // click the index title to locate the segment-(NSInteger) tableView :( UITableView *) tableView sectionForSectionIndexTitle :( NSString *) title atIndex :( NSInteger) index {return index;} // number of segments-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView; {return [_ arraySection count];} // return segment title-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {return [_ arrayIndex objectAtIndex: section];} # pragma UITableViewDelegate-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {NSString * strRow = nil; NSArray * array = [_ arraySection objectAtIndex: indexPath. section]; strRow = [array objectAtIndex: indexPath. row]; NSString * msg = [[NSString alloc] initWithFormat: @ "you selected-% @", strRow]; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "prompt" message: msg delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [alert show]; [tableView deselectRowAtIndexPath: indexPath animated: YES]; [msg release]; [alert release];}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. NSMutableArray * arrayIndex = [[NSMutableArray alloc] init]; // numeric name for (char I = '0'; I <= '9'; I ++) {int nMem = arc4random () % 7 + 4; NSMutableArray * arrayMem = [[NSMutableArray alloc] init]; for (int j = 0; j
Download project code