Ios & ndash; Use UINib to load xib files to implement UITableViewCell and uinibxib

Source: Internet
Author: User

Ios-use UINib to load xib files to implement UITableViewCell and uinibxib

 

The essence of the xib file is xml, which describes interface objects. Each object has a very important attribute. The class attribute in the identity inspector panel, when loading the xib file, the class corresponding to the interface object is actually instantiated.

Xib file loading process:

1. Load xib files from disk to memory. There are two ways to load xib files: NSBundle and UINib.

2. Execute unarchive and initialize operations. This process is mainly completed by initWithCoder :( NSCoder *) decoder in NSCoding Protocol.

3. create connections: Outlets and Actions. Outlets uses the IBOutlet keyword and setValue: forKey: To create each Outlet. Therefore, KVO notifications are sent for each Outlet creation. Actions uses the IBAction keyword to mark and replace the void return value. It uses the addTarget: action: forControlEvents: method to establish each Action connection. Note: The Outlets and Actions are constructed sequentially. The Outlets connection is established first, and the Actions connection is established later. Because Actions depends on the Outlets created earlier.

4. To call the awakeFromNib method, you must first call the awakeFromNib method of super, then you can set some personalized operations and some operations that cannot be set during design. Note that awakeFromNib messages are sent only to the specified M Class in Interface Builder and not to placeholder objects such as Files's Owner and First Responder.

After the object is loaded, you can perform various operations.

Use NSBundle to load the xib file:

 

[[NSBundle mainBundle]loadNibNamed:<(NSString *)> owner:<(id)> options:<(NSDictionary *)>];

This is the most common one. The loadNibNamed: owner: options: method returns an NSArray *, which contains the interface object (class) contained in the loaded xib file ).

NSBundle loads the xib file from the disk each time, while UINib loads the xib file from the disk for the first time, and then caches the xib file in the memory. Every time a new object is generated, directly access the xib file in the memory and execute the 2-4 steps described above, so the performance will be greatly improved. In addition, the developer documentation also recommends that you use the UINib loading technology for the xib files that are used repeatedly, when a low memory warning is received, the xib file is detached from the disk and loaded from the disk when the file is accessed again. Let's take a look at the definition of UINib:

NS_CLASS_AVAILABLE_IOS(4_0) @interface UINib : NSObject {  @private    id storage;}// If the bundle parameter is nil, the main bundle is used.// Releases resources in response to memory pressure (e.g. memory warning), reloading from the bundle when necessary.+ (UINib *)nibWithNibName:(NSString *)name bundle:(NSBundle *)bundleOrNil;// If the bundle parameter is nil, the main bundle is used.+ (UINib *)nibWithData:(NSData *)data bundle:(NSBundle *)bundleOrNil;// Returns an array containing the top-level objects from the NIB.// The owner and options parameters may both be nil.// If the owner parameter is nil, connections to File's Owner are not permitted.// Options are identical to the options specified with -[NSBundle loadNibNamed:owner:options:]- (NSArray *)instantiateWithOwner:(id)ownerOrNil options:(NSDictionary *)optionsOrNil;@end

The first two methods are clear, loading them in different ways, and the third method is instantiation (Object creation)

Table view instance:

No more details

Create an xib file named Empty

@ Interface TableViewCell: UITableViewCell @ property (weak, nonatomic) IBOutlet UILabel * lb1; @ property (weak, nonatomic) IBOutlet UILabel * lb2;-(IBAction) bt :( id) sender; @ end

The three attributes are linked to the xib file.

Then, perform the following operations in the UITableViewDataSource Proxy:

// Declare UINib * nib in the header file; // instantiate self-> nib = [UINib nibWithNibName: @ "Empty" bundle: nil];

Then let's take a look at tableView: cellForRowAtIndexPath: method. The standard implementation method of this method is as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  static NSString *CellIdentifier = [NSString stringWithFormat:@"Cell"];  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  if (cell == nil) {    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  }  //config the cell  return cell;}

This is how to create a cell using code. The following describes how to use UINib:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *identifier = @"Cell";    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];    if (cell == nil) {        cell = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];    }        switch (indexPath.section) {        case redSection:            cell.lb1.text = @"lb1";            cell.lb2.text = @"lb2";            break;        case blueSection:            break;        default:            [[cell textLabel] setText:@"Unknow"];    }    return cell;}

As follows:

The above is my personal understanding. If you have any mistakes, please correct them.

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.