IPhone UITableView)

Source: Internet
Author: User

UITableView is a "directory view" or "table view" (table view). This table view displays or edits information in the form of a list. It consists of one column and multiple rows. You can navigate to any row in a table view in vertical scrolling mode, and customize the display mode of each row of data.

 

When creating a table view, you can select two types of table views: UITableViewStylePlain or UITableViewStyleGrouped. The former is sorted by index, and the latter is displayed by group.

 

Generally, each UITableView has a UITableViewController, UITableViewDelegate, and UITableViewDataSource class. The UITableViewController class is used as the UITableView view control class (the Controller role in MVC) to manage UITableView. Like most UIViewController classes, it controls the life cycle functions of UITableView. UITableViewDelegate acts as the interface of the delegated object in the delegate mode. Like most iPhone delegate interfaces, it provides a part that the UITableView subclass cannot maintain consistent behavior, here, the reader can customize the display style of the table view, or even every element of the table view. Its important interface definition is as follows:

 

@ Protocol UITableViewDelegate <NSObject, UIScrollViewDelegate>

@ Optional

 

// Display customization

-(Void) tableView :( UITableView *) tableView willDisplayCell :( UITableViewCell *) cell forRowAtIndexPath :( NSIndexPath *) indexPath;

 

// Variable height support

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

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

-(CGFloat) tableView :( UITableView *) tableView heightForFooterInSection :( NSInteger) section;

 

// Section header & footer information. Views are preferred over title shoshould you decide to provide both

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

-(UIView *) tableView :( UITableView *) tableView viewForFooterInSection :( NSInteger) section;

 

// Accessories (disclosures ).

-(Void) tableView :( UITableView *) tableView accessoryButtonTappedForRowWithIndexPath :( NSIndexPath *) indexPath;

 

// Selection

// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.

-(NSIndexPath *) tableView :( UITableView *) tableView willSelectRowAtIndexPath :( NSIndexPath *) indexPath;

 

// Called after the user changes the selection.

-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath;

 

// Editing

-(UITableViewCellEditingStyle) tableView :( UITableView *) tableView editingStyleForRowAtIndexPath :( NSIndexPath *) indexPath;

 

// Indentation

-(NSInteger) tableView :( UITableView *) tableView indentationLevelForRowAtIndexPath :( NSIndexPath *) indexPath; // return depth of row for hierarchies

 

@ End

 

UITableViewDataSource provides data sources in the table view. The following table lists common data sources in the table view:

 

Method
Description
 
TableView: numberOfRowsInSection:
Number of rows in a specific Section
 
NumberOfSectionsInTableView:
Number of sections in the table view of a specific data source
 
TableView: cellForRowAtIndexPath:
Get the cell content from the data source and put it on a specific row
 
SectionIndexTitlesForTableView:
Obtains the title of a table view of a data source.
 
TableView: commitEditingStyle: forRowAtIndexPath
Modify the cell content
 
TalbeView: canEditRowAtIndexPath:
Returns a Boolean value to notify the table view whether a row can be modified.
 
TableView: canMoveRowAtIndexPath:
Returns a Boolean value to indicate whether a row in the table view can be moved.
 
TableView: moveRowAtIndexPath: toIndexPath:
Allows a table view cell to be moved.
 


 

The table View data source interface provides common methods for table view Data Source Operations. tableView: numberOfRowsInSection and tableView: cellForRowAtIndexPath: are two methods that must be implemented by each table's data source, the former tells the table view how many rows of cells exist, and the latter tells the table view what the content of each cell is. By implementing these two methods, the program can provide the basic information required by a table view and call the table view.

 

In the following example, I will write a table view with a navigation Panel. This complex type of control is everywhere on the iPhone, such as iPod, memo, and alarm.

 

Ü first, create a "Window-based" project and name it "TableProjectOne ".

Ü create a subclass of UIViewController and name it "MyViewController ".

Ü create an empty xib and name it "MyViewController ".

Ü open MyViewController. xib in Interface Build.

Ü drag a view from the Library to the MyViewController window, and then add a table view to the newly added view to ensure that the table view is fully filled with the view.

Ü change the File's Owner to MyViewController and connect the view of MyViewController to the newly added view.

Ü here MyViewController is created, save and exit Interface Build.

Ü open MainWindow. xib and drag a Navigation Controller to the window.

Ü drag a View Controller to the Navigation Controller and change its class name and Nib name to MyViewController.

Ü drag a Bar Button Item to the Navigation Controller and change the title to "view2 ".

 

Modify TableProjectOneAppDelegate. h as follows:

 

# Import <UIKit/UIKit. h>

# Import "MyViewController. h"

 

@ Interface TableProjectOneAppDelegate: NSObject <UIApplicationDelegate> {

UIWindow * window;

IBOutlet UINavigationController * viewController;

}

 

@ Property (nonatomic, retain) IBOutlet UIWindow * window;

@ Property (nonatomic, retain) IBOutlet UINavigationController * viewController;

 

@ End

 

The implementation file TableProjectOneAppDelegate. m is as follows:

 

# Import "TableProjectOneAppDelegate. h"

 

@ Implementation TableProjectOneAppDelegate

 

@ Synthesize window;

@ Synthesize viewController;

 

-(Void) applicationDidFinishLaunching :( UIApplication *) application {

[Window addSubview: self. viewController. view];

// Override point for customization after application launch

[Window makeKeyAndVisible];

}

 

-(Void) dealloc {

[Window release];

[ViewController release];

[Super dealloc];

}

@ End

 

The final result is as follows:

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.