Summary
This chapter is a brief example of how Uitabview is used, using the three pages of Uitabviewcontroller to illustrate different situations, including simple tables, segmented sub-style tables, and index tables.
Run results
Engineering Construction Overview
Use the 1.UITabViewController to refer to the previous article
2. the control class using the Uitabview must implement the data source and proxy methods for the control, and it is necessary to specify the Uitabview data source and the control class for the interface in IB.
3. The main method of implementation has several paragraphs, the number of rows per segment, how each cell is drawn, what the segment title is, and if an index is required, the index title should be implemented, and the index title will be clicked to locate the segment.
4. Since no arc is used in this project, there is a memory management problem at the beginning, mainly when the member variable is assigned a shallow copy. The correct use method is Self._value = value, not _vale = value, which invokes the set method, which is simply a pointer assignment.
main code and comments
H file
indexviewcontroller.h// tableviewdemo//// Created by arbboter on 14/12/5.// Copyright (c) 2014 Arbboter. All rights reserved.//#import <UIKit/UIKit.h> @interface Indexviewcontroller:uiviewcontroller < Uitableviewdatasource, uitableviewdelegate>{ 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; The number of rows in the corresponding segment of the #pragma uitableviewdatasource<nsobject>//-(nsinteger ) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{nsinteger ret = 0; ret = [[_arraysection objectatindex:section] count]; return ret;} Draw 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:c Ellidentifier] 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 = [[Nsmutablearra] Y 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 navigate to 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 chose-%@", Strrow]; uialertview* alert = [[Uialertview alloc] initwithtitle:@ "hint" 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<nmem; J + +) {[Arraymem addobject:[nsstring stringwithformat:@ "%c%c%c", I, Arc4random ()%10+ ' 0 ', arc4random ()%10 + ' 0 ']; } [arrayindex Addobject:arraymem]; }//Letter name for (char i= ' A '; i<= ' Z '; i++) {int nmem = Arc4random ()%7+4; nsmutablearray* Arraymem = [[Nsmutablearray alloc] init]; for (int j=0; j<nmem; J + +) {[Arraymem addobject:[nsstring stringwithformat:@ '%c%c%c ', I, Arc4random ( )%26+ ' A ', arc4random ()%26+ ' a ']; } [arrayindex Addobject:arraymem]; } self._arraysection = arrayindex; Self._arraymember = [arrayindex objectatindex:0]; [Arrayindex release];} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} -(void) dealloc{for (int i=0; i<_arraysection.count; i++) {[[_arraysection objectatindex:i] release]; } [_arraysection release]; [_arraymember ReleasE]; [Super Dealloc];} @end
Engineering Code Download
Ios-uitabview Example