UITableView yes app Commonly used controls in development , It's very powerful , Mostly used for data display . Here is a simple example to introduce tableview The basic usage of .( Suitable for novice , The master floats by )

@interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{

UITableView *DataTable;

NSMutableArray *dataArray1; // Define data array 1

NSMutableArray *dataArray2;// Define data array 2

NSMutableArray *titleArray;// Define the title array

}

- (void)viewDidLoad

{

[superviewDidLoad];

// initialization tableview

DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];// Specify the location size

[DataTablesetDelegate:self];// Designated delegation

[DataTablesetDataSource:self];// Specify data delegation

[self.viewaddSubview:DataTable];// load tableview

dataArray1 = [[NSMutableArrayalloc] initWithObjects:@" China ", @" The United States ", @" The British ", nil];// Initialize the data array 1

dataArray2 = [[NSMutableArrayalloc] initWithObjects:@" The yellow race ", @" The black race ", @" The white race ", nil];// Initialize the data array 2

titleArray = [[NSMutableArrayalloc] initWithObjects:@" Country ", @" race ", nil];// Initialize the title array

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

// Every section The title of the display

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

switch (section) {

case 0:

return [titleArray objectAtIndex:section];// Extract the elements of the title array to display the title

case 1:

return [titleArray objectAtIndex:section];// Extract the elements of the title array to display the title

default:

return @"Unknown";

}

}

// Specify how many partitions there are (Section), The default is 1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return [titleArray count];// Returns the number of elements in the title array to determine the number of partitions

}

// Specify how many rows in each partition , The default is 1

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

switch (section) {

case 0:

return  [dataArray1 count];// Each partition usually corresponds to a different array , Returns the number of elements to determine the number of rows in 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";

// initialization cell And specify its type , You can also customize cell

UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil)

{

cell = [[[UITableViewCellalloc]

initWithFrame:CGRectZero

reuseIdentifier:CellIdentifier] autorelease];

}

switch (indexPath.section) {

case 0:// Corresponding to each partition

[[cell textLabel]  setText:[dataArray1 objectAtIndex:indexPath.row]];// to cell Add data

break;

case 1:

[[cell textLabel]  setText:[dataArray2 objectAtIndex:indexPath.row]];

break;

default:

[[cell textLabel]  setText:@"Unknown"];

}

return cell;// return cell

}

tableview There are also many difficult properties and interfaces , I'll make it up later .

The above example introduces in function tableview Use , But it has great limitations in data processing . When we want to request data from the server , Faced with a variety of possible data ( The number of major index groups is unstable ) At this point, the above switch Will not be able to meet our needs .

Use switch But the structure of the code is clear , But its limitations are fatal (switch in case The number of cannot be specified dynamically ), Here's another way to solve the above problem .

Code changes 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 ", @" The United States ", @" The British ", nil];

dataArray2 = [[NSMutableArrayalloc] initWithObjects:@" The yellow race ", @" The black race ", @" The white race ", nil];

titleArray = [[NSMutableArrayalloc] initWithObjects:@" Country ", @" race ", nil];

dataArray = [[NSMutableArrayalloc] initWithObjects:dataArray1, dataArray2, nil]; // initialization dataArray, Elements are arrays

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

// Make a personal title , Through here UIview To design the title , Rich in functions , There are many changes .

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

UIView *view = [[[UIViewalloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];

[view setBackgroundColor:[UIColorbrownColor]];// Change the color of the title , You can also use 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 title

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 40;

}

// Every section The title of the display , Don't use the one above

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

}

// Specify how many partitions there are (Section), The default is 1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return [titleArraycount];

}

// Specify how many rows in each partition , The default is 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 , Let's make a comparison

return [[dataArray objectAtIndex:section] count];  // take dataArray The elements in , And according to each element ( Array ) To determine the number of rows in a partition .

}

// 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 , Let's compare .

[[cell textLabel] setText:[[dataArrayobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];

// ditto , Take out dataArray The elements corresponding to each partition in ( Array ), And we can get the value through it . ( You have to have imagination , Copy the code and you'll see )

return cell;

}

// Change the height of the line

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return40;

}

 
 
classification :  iPhone
label :  UITableView
 

UITableView More related articles on the detailed use of

  1. ( Reprint )UITableView Detailed explanation of

    NSIndexPath Type is used to get user selection indexPath, In other functions , If you need to know which user has chosen cell, It can save a lot of trouble . You don't have to build global variables anymore section and row. NSIndexPat ...

  2. [ turn ] Detailed arrangement :UITableView Optimization techniques

      original text :http://www.cocoachina.com/ios/20150602/11968.html   Recently I saw a good open source project on Weibo VVeboTableViewDemo, It's about how to optimize ...

  3. UITableView Detailed notes

    style // Ordinary UITableViewStylePlain, // grouping UITableViewStyleGrouped // Tabular view UITableView * tableView = [[UI ...

  4. UITableView Comprehensive analysis , It's very detailed

    --UIKit And UITableView summary stay iOS In development UITableView It's the most widely used control , We can see its shadow everywhere in the software we usually use , Similar to wechat .QQ. Sina Weibo and other software are basically everywhere U ...

  5. UITableView( Two )

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  6. iOS edit UITableView( according to iOS Programming )

    Last project we finished  JXHomepwner  Simple application demonstration , Project address . In this section, we need to build on the previous section , Add some response user actions . Including adding , Delete and move tables . Edit mode UITableView There is one named   e ...

  7. Use Autolayout Realization UITableView Of Cell Dynamic layout and height dynamic change

    This article is translated from :stackoverflow Someone is here. stackoverflow I asked a question on the Internet : 1 How to be in UITableViewCell Use in Autolayout To achieve Cell The row height is calculated automatically for the content and subviews of , and ...

  8. JSPatch To update the online App What happened in BUG( Super detailed )

    JSPatch What is the function of ? Simply speaking :( There are specific operation steps and errors in the operation process ) 1.iOS The application is launched to AppStore Need to wait for apple to review , Generally, the audit time needs 1 To 2 Zhou . Although the program is on the shelves ...

  9. Use UITableView The grouping style of

    Grouping style, as the name suggests, is right TableView Group the data rows in , Each group has one header and footer. TableView in header The English text of is capitalized ,footer The English text of is lowercase . As shown in the figure below ...

Random recommendation

  1. Linux Get through ssh Password-free login

    image hadoop and spark This kind of cluster , because master Nodes need to control slave node , And the interaction information between the nodes , Therefore, each node needs to be able to log in without password . adopt RSA Save password , The basic operation is as follows : Step 1: ...

  2. IP The format of the first part of the protocol and the four protocols used with it (ARP,RARP,ICMP,IGMP)

    Catalog IP Protocol header format address resolution protocol ARP Reverse Address Resolution Protocol RARP Internet control message protocol ICMP Internet Group Management Protocol IGMP IP The head of the datagram IP Datagram header format : The highest position is on the left , Write it down as 0 bit: The lowest point is on the right ...

  3. kvm Virtual machine time modification

    In a virtualized environment , Virtual machines are running for a long time , Time will slow down , The usual practice is to configure ntpdate Scheduled task to synchronize time with time server .KVM The virtual machine adopts... By default utc Time , It needs special modification , And consider kvm Time synchronization problem .1 ...

  4. Luogu P1238 Labyrinth

    It's because of a small oversight , More than half an hour of water problems Title Description There is one m*n The labyrinth of grid ( Express m That's ok .n Column ), Some of them are walkable and some are not , If you use 1 You can go ,0 You can't go , The file reads into this m*n Data and starting point . The end point ( Start and end ...

  5. Use Maven Profile Realize multi environment construction

    In the development process , Our software will run in different environments , For example, development environment . Test environment . Production environment , And our software is in different environments , Some configurations may be different , For example, data source configuration . Log file configuration . As well as some basic configuration in the process of software operation , Every time ...

  6. About android stay Service Pop up in Dialog Dialog box

    Create a good AlertDialog After the type object , want dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); At the same time ...

  7. MemSQL Start[c]UP 2.0 - Round 2

    I can't sleep at night anyway , Going to get to 1 I'll start the game at 12:00 ,6 I only did 2 A title , And the hand speed is slow , There is still a lot of room for improvement . A topic : Given two 0,1 strand (len<=100000), But it's not a normal binary string , It is q Base string ,q ...

  8. yii2 Source learning notes ( fourteen )

    Module Class is the base class of module and application class . yiisoft\yii2\base\Module.php <?php /** * @link http://www.yiiframework.com/ * ...

  9. weex in Vuex state Use storage Persistence

    stay weex Use in Vuex As state Management tools , The problem is coming. , How to make state It can be persistent ?weex Official supply store modular , So we can try to use this module to persist state. Let's take a look at the introduction of this module : stora ...

  10. Eclipse Delete in tomcat server Causes the version to be unable to be recreated tomcat server

    stay Eclipse Creates one in Web After the project , The project needs to be deployed to Tomcat Release in . Sometimes we meet in New Server In the dialog box, select Tomcat 6/7 And then you can't click “Next” The problem with buttons , As shown in the figure below : ...