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.
- @interface RootViewController : UITableViewController {
- NSArray *dataArray ;
- }
- @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:
- UIKIT_EXTERN_CLASS @interface UITableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
- {
- @private
- UITableViewStyle _tableViewStyle;
- id _keyboardSupport;
- struct {
- int clearsSelectionOnViewWillAppear:1;
- } _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:
- @interface RootViewController:UITableViewController
- <UITableViewDelegate,UITableViewDataSource>
- {
- NSArray *dataArray;
- }
- @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:
- //.h
- @interface ViewBaseAppViewController : UIViewController
- <UITableViewDelegate,UITableViewDataSource>
- {
- IBOutlet UITableView *m_tableView;
- }
- //.m
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- // Return the number of rows in the section.
- return 10;
- }
-
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
-
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
- }
- cell.textLabel.text =@"xingchen";
- return cell;
- }
Summary:IPhone applicationsMediumUITableViewI hope this article will help you.