Usage of UITableView in iPhone applications

Source: Internet
Author: User

IPhone applicationsMediumUITableViewThe usage summary is the content to be introduced in this article.UITableViewIPhone applications are inseparable.UITableViewTherefore, it is particularly important to understand the Table control. The following describesUITableViewTwo ways to display data are described.

The content of data display is mainly the UITableView control, but we need to define a class to load data for it and a callback function to define interaction with users, these two aspects are implemented through delegation. According to the MVC design, Apple places the UITableView in a UITableViewController class, and uses the UITableViewController class instance to control more attributes of the UITableView. We need to create a class of the controller type, which can be inherited into the parent class of UITableView, or not inherited, and only implement data source delegation and operation delegation.

1. Create a subclass Based on UITableViewController.

 
 
  1. @interface RootViewController : UITableViewController {  
  2. NSArray *dataArray ;  
  3. }  
  4. @property(nonatomic,retain) NSArray *dataArray ; 

Because the UITableViewController class already contains the DeleGate <UITableViewDelegate, UITableViewDataSource>, we do not need to manually add these delegates here. We can view our assumptions by viewing the source code:

 
 
  1. UIKIT_EXTERN_CLASS @interface UITableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
  2. {  
  3.   @private  
  4.     UITableViewStyle _tableViewStyle;  
  5.     id               _keyboardSupport;  
  6.     struct {  
  7.         int clearsSelectionOnViewWillAppear:1;  
  8.     } _tableViewControllerFlags;  

Through the above code, we can see that UITableViewController has inherited the <UITableViewDelegate, UITableViewDataSource> delegates.

In this way, in the implementation file, we only need to implement the required functions in the two delegates.

However, we still cannot do this. We have not completed the task. The class we created still needs a container that can load the instance of this class, which is like buying a bottle of mineral water, water must be packed in a bottle container) you can drink it in it. The instance we created is like water. We also need to create a bottle container) to hold the water. This container can be a UIView instance, A UINavigationController instance, or a Nib file. After these preparations, you can see the effect.

2. Now let's look at the second case: create a common control instance.

If we create an instance of the general control class, as shown in the following figure:

 
 
  1. @interface RootViewController:UITableViewController  
  2. <UITableViewDelegate,UITableViewDataSource> 
  3. {  
  4. NSArray *dataArray;  
  5. }  
  6. @property(nonatomic,retain) NSArray *dataArray ; 

In this case, we need to manually add the inheritance delegate to inherit the data source and data operation.

Then, the rest is the same as the others above. It is necessary to implement the class defined in the delegate, load the data, load the data into the container, and then display the data.

Create a simple table

Reference code:

 
 
  1. //.h  
  2. @interface ViewBaseAppViewController : UIViewController   
  3. <UITableViewDelegate,UITableViewDataSource> 
  4. {  
  5. IBOutlet UITableView *m_tableView;  
  6. }  
  7. //.m  
  8. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  9.     // Return the number of rows in the section.  
  10. return 10;  
  11. }  
  12.  
  13. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  14. {    
  15.     static NSString *CellIdentifier = @"Cell";  
  16.  
  17.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  18.     if (cell == nil) {  
  19.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  20.     }  
  21.     cell.textLabel.text =@"xingchen";  
  22.     return cell;  

Summary:IPhone applicationsMediumUITableViewI hope this article will help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.