UITableView is the most commonly used control in app development and is powerful enough for the display of data. The basic usage of TableView is described in a simple example. (For beginners, master drifting over)
@interface tableviewtestviewcontroller:uiviewcontroller<uitableviewdatasource,uitableviewdelegate>{
UITableView *datatable;
Nsmutablearray *dataarray1; Defining data Arrays 1
Nsmutablearray *dataarray2;//defining data Arrays 2
Nsmutablearray *titlearray;//Definition Header Array
}
-(void) viewdidload
{
[Superviewdidload];
Initialize TableView
DataTable = [[[Uitableviewalloc] initwithframe:cgrectmake (0, 0, 320, 420)];//Specify location size
[datatablesetdelegate:self];//Specifies the delegate
[datatablesetdatasource:self];//Specifies the data delegate
[self.viewaddsubview:datatable];//Loading TableView
DataArray1 = [[Nsmutablearrayalloc] initwithobjects:@ "China", @ "United States", @ "United Kingdom", nil];//initialize data array 1
DataArray2 = [[Nsmutablearrayalloc] initwithobjects:@ "Yellow Man", @ "Black", @ "white", nil];//initialize data array 2
Titlearray = [[Nsmutablearrayalloc] initwithobjects:@ "Country", @ "race", nil];//Initialize header array
}
-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
{
Return YES for supported orientations
return (interfaceorientation = = uiinterfaceorientationportrait);
}
Title for each section display
-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (Nsinteger) section{
Switch (section) {
Case 0:
return [Titlearray objectatindex:section];//extracts the elements of the header array to display the caption
Case 1:
return [Titlearray objectatindex:section];//extracts the elements of the header array to display the caption
Default
return @ "Unknown";
}
}
Specify how many partitions (section), default to 1
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {
return [Titlearray count];//returns the number of elements in the header array to determine the number of partitions
}
Specify how many rows are in each partition, default to 1
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{
Switch (section) {
Case 0:
return [dataArray1 count];//each partition usually corresponds to a different array, returning its number of elements to determine the row count of the partition
Break
Case 1:
return [DataArray2 Count];
Break
Default
return 0;
Break
}
}
Draw cell
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {
static NSString *cellidentifier = @ "Cell";
Initialize the cell and specify its type, or you can customize the cell
UITableViewCell *cell = (uitableviewcell*) [TableView Dequeuereusablecellwithidentifier:cellidentifier];
if (cell = = nil)
{
cell = [[[Uitableviewcellalloc]
Initwithframe:cgrectzero
Reuseidentifier:cellidentifier] autorelease];
}
Switch (indexpath.section) {
Case 0://corresponds to the respective partition
[[Cell Textlabel] settext:[dataarray1 objectatindex:indexpath.row]];//add data to cell
Break
Case 1:
[[Cell Textlabel] Settext:[dataarray2 ObjectAtIndex:indexPath.row];
Break
Default
[[Cell Textlabel] settext:@ "Unknown"];
}
Return cell;//back to cell
}
TableView also has a lot of high-difficulty properties and interfaces, which I will slowly make up in the future.
The above example introduces the use of TableView in function, but it has a great limitation in data processing. When we want to request data from the server, facing a variety of possible data (the number of major index groups is not stable) at this time the switch above will not meet our needs.
Using switch but the structure of the code is clear, but its limitations are deadly (the number of case in switch can not be specified dynamically), the following alternative method to solve the above problem.
Changes to the code based on the original:
@interface tableviewtestviewcontroller:uiviewcontroller<uitableviewdatasource,uitableviewdelegate>{
UITableView *datatable;
Nsmutablearray *dataarray1;
Nsmutablearray *dataarray2;
Nsmutablearray *titlearray;
Nsmutablearray *dataarray; Added an array to hold the array DataArray
}
-(void) viewdidload
{
[Superviewdidload];
DataTable = [[[Uitableviewalloc] initwithframe:cgrectmake (0, 0, 320, 420)];
[Datatablesetdelegate:self];
[Datatablesetdatasource:self];
[Self.viewaddSubview:DataTable];
DataArray1 = [[Nsmutablearrayalloc] initwithobjects:@ "China", @ "United States", @ "UK", nil];
DataArray2 = [[Nsmutablearrayalloc] initwithobjects:@ "Yellow man" @ "Black", @ "white", nil];
Titlearray = [[Nsmutablearrayalloc] initwithobjects:@ "Country", @ "race", nil];
DataArray = [[Nsmutablearrayalloc] initwithobjects:dataarray1, dataArray2, nil]; Initializes a dataarray, an array of elements
}
-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
{
Return YES for supported orientations
return (interfaceorientation = = uiinterfaceorientationportrait);
}
Set the personality title, here through the UIView to design the title, rich in functionality, change more.
-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section {
UIView *view = [[[Uiviewalloc] initwithframe:cgrectmake (0, 0, +/-)] autorelease];
[View setbackgroundcolor:[uicolorbrowncolor]];//change the color of the title, also available pictures
UILabel *label = [[Uilabelalloc] Initwithframe:cgrectmake (5, 5, 100, 30)];
Label.textcolor = [Uicolorredcolor];
Label.backgroundcolor = [Uicolorclearcolor];
Label.text = [Titlearrayobjectatindex:section];
[View Addsubview:label];
return view;
}
Specify the height of the caption
-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (Nsinteger) section{
return 40;
}
Each section shows the title, with the above this is not
-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (Nsinteger) section{
}
Specify how many partitions (section), default to 1
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {
return [Titlearraycount];
}
Specify how many rows are in each partition, default to 1
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{
/* Switch (section) {
Case 0:
return [dataArray1 Count];
Break
Case 1:
return [DataArray2 Count];
Break
Default
return 0;
Break
}*/
/* for (int i = 0; i < [Titlearray count]; i++) {
if (section = = i) {
return [[DataArray objectatindex:section] count];
}
}*/
The above method is also feasible, we refer to the comparison
return [[DataArray objectatindex:section] count]; Takes the elements in the DataArray and determines the number of rows in the partition based on each element (array).
}
Draw cell
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {
static NSString *cellidentifier = @ "Cell";
UITableViewCell *cell = (uitableviewcell*) [TableView
Dequeuereusablecellwithidentifier:cellidentifier];
if (cell = = nil)
{
cell = [[[Uitableviewcellalloc]
Initwithframe:cgrectzero
Reuseidentifier:cellidentifier] autorelease];
}
/*switch (indexpath.section) {
Case 0:
[[Cell Textlabel]
Settext:[dataarray1 ObjectAtIndex:indexPath.row]];
Break
Case 1:
[[Cell Textlabel]
Settext:[dataarray2 ObjectAtIndex:indexPath.row]];
Break
Default
[[Cell Textlabel]
settext:@ "Unknown"];
}*/
The above method is also feasible, we compare.
[[Cell Textlabel] settext:[[dataarrayobjectatindex:indexpath.section]objectatindex:indexpath.row];
As above, take out the Element (array) corresponding to each partition in the DataArray, and use it to fetch the value. (Everyone should have the imagination, copy the code to try to understand)
return cell;
}
Change the height of a row
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{
Return40;
}
Detailed usage of UITableView