This is the first article in the UITableView blog series, which uses Xib and arc coding to explain some of the simple, but easily overlooked, things that uitableview use, and I'll tell the reader how to use it and never forget it.
The steps for the operation are as follows
(1) First drag a UITableView control to the Xib file, such as,
It's nothing, everybody, don't be too afraid UITableView, it is just a UI control, with UIButton and uilabel nothing different, can be arbitrarily dragged.
(2) Next we instantiate this UITableView object in the corresponding Xxxviewcontroller, here is a little trick to explain, First click on the Xxxviewcontroller.xib file, this time Xcode is displayed in the contents of the Xib file, this time double-click the XxxViewController.h file, when the small window opens the. h file, which facilitates the following operations.
At the midpoint of the Xxxviewcontroller.xib file, click TableView, and then drag it to the XxxViewController.h file in the small window, such as
(3) Release the mouse, this time will pop up the following interface,
Here I named the UITableView object as Thetableview, and you can name it any name, which is just a variable name that you modify. Click the Connect button so that Xcode automatically helps us generate a property statement in the XxxViewController.h file.
?
| 1 |
@property (nonatomic, strong) IBOutlet UITableView *theTableView; |
This is the benefit of using xib, saving a lot of code, isn't it?
(4) This step is very important, set Thetableview delegate and DataSource to self, in the Xxxviewcontroller.xib file, right-drag the mouse pointer to file ' s Owner, such as,
The following screen will pop up,
Click DataSource, then repeat the above action, then click Delegate. This interface operation is equivalent to executing the self.theTableView.delegate = self; and self.theTableView.dataSource = self; statement. I'd still like to remind you to two times right-drag UITableView point to file ' Owner, set DataSource and delegate, respectively, in order.
(5) above are through the Xib to set the interface, this saves a lot of code, I explained the operation of the process. OK, xib drag is basically so much content, here is the code implementation details.
In the xxxviewcontroller.m file, implement the Uitableviewcontrollerdelegate and Uitableviewcontrollerdatasource protocols, and define a nsmutablearray instance variable Instanc e variable, named _infoarray,
?
| 12345 |
@interface xxxViewController()<UITableViewDelegate,UITableViewDataSource>{ NSMutableArray *_infoArray;}@end |
Initializes the contents of the _infoarray array in-viewdidload,
?
| 12345 |
- ( void ) Viewdidload { &NBSP;&NBSP;&NBSP;&NBSP; Code class= "CPP plain" >[super viewdidload]; &NBSP;&NBSP;&NBSP;&NBSP; _infoarray = [[nsmutablearray alloc] initwithobjects:@ "Zero" ,@ "one" ,@ "I" ,@ ,@ , nil]; } |
Well, this step is to set the Thetableview data source, someone will ask what the data source is, simply speaking, the data source is the Thetableview control to display the content, like Label.text = @ "Label content", where @ "label content" The string is the data source for the label control. So we can see that UITableView is the perfect use of the MVC design pattern to achieve, where xxxviewcontroller as the role of the controller, Thetableview is the view, and here the _infoarray is the data source, actually here _ Infoarray is not a complete data model, but it also allows for a higher level of abstraction.
(6) above we obey the uitalbeviewdelegate and Uitableviewdatasource agreement, then we need to implement the Proxy method in the Xxxviewcontroller Central, then some readers will ask, UITableView There are so many proxy methods, I always do not remember clearly, here I will tell you how to remember clearly, will not forget.
In all UITableView proxy methods, returning list rows (Nsinteger) and setting list contents (UITableViewCell *) is an essential two proxy method, The return values of the two methods are Nsinteger and UITableViewCell * content, so we just need to remember the return value, and Xcode will automatically prompt us which method to choose.
For example, I want to implement a method that returns the number of rows in a list, when only input-(Nsinteger) TableView is required, and Xcode prompts us for a number of methods, as shown in
This time I choose the second numberofrowsinsection: (Nsinteger) section method, and then return the number of arrays in the _infoarray, as shown in the following code,
?
| 1234 |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return[_infoArray count];} |
This sets the number of rows that are returned.
Next I'm going to set the list to show, we know it's return value is UITableViewCell * type, so I write this directly,-(UITableViewCell *) Tableview,xcode automatically prompts me the complete proxy method, as shown in,
Fortunately, the way to return UITableViewCell * content is only one, I do not have to choose to press the Enter button directly, then I will implement Cellforrowatindexpath: (Nsindexpath *) Indexpath method, Fill in the contents, the following code,
?
| 1234567891011 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ staticNSString *simpleIdentify = @"SimpleIdentify"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentify]; if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentify]; } cell.textLabel.text = [_infoArray objectAtIndex:indexPath.row]; returncell;} |
The above code is all my handwriting input, because I already remember memorized, is the right one can be born skillfully, so iOS development, for some key code, in order to not duplicate access to data, or to carefully write down. So how to remember the above simple but easy to forget code? Let me say my memory process, first static nsstring *simpleidentify = @ "Simpleidentify"; is the reuse identifier of the set UITableViewCell, We know that UITableView's reuse mechanism is a bit of a good technology, I will write a blog later to explain, there is no more to say. Next I enter the code uitableviewcell *cell = [TableView de]; when Xcode automatically prompts the method list, for example,
Remember, here Please select the first Dequeuereusablecellwithidentifier method, the method has only one NSString * parameter, I have chosen the second method many times before, then the program inexplicably Cursh.
Then, if the cell is empty by using if (cell = = nil) {}, if the cell is empty, the cell object is instantiated, and the code is cell = [[UITableViewCell alloc] Initwithstyle: Uitableviewcellstyledefault Reuseidentifier:simpleidentify];
then through Cell.textLabel.text = [_infoarray ObjectAtIndex:indexPath.row]; To set the contents of the corresponding function in the data source displayed by the cell, and finally through the return cell;
After writing the code, we run the program, as below,
Well, this is UITableView's series of tutorials, mainly on the key steps of coding through Xib, and dozens of how to quickly remember the proxy method of UITableView.
iOS deep Learning (UITableView: Series 1-the most basic thing)