Ios-use UINib to load xib files and implement UITableViewCell
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. Next let's take a look at the definition of UINib: copy the code 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 *) instantiateWithOw Ner :( id) ownerOrNil options :( NSDictionary *) optionsOrNil; @ end copy the first two methods of the Code are clear and loaded in different ways, respectively, the third method is to instantiate (create an object) The table view instance: the specific details do not mention the creation of an xib file named Empty 9abc0d43-dd1_401a-8dca-301_f5c2cdb. Note that the class attribute of Table View Cell is TableViewCell, not the default UITableViewCell, TableViewCell. h: @ interface TableViewCell: UITableViewCell @ property (weak, nonatomic) IBOutlet UILabel * lb1; @ property (weak, nonatomic) IBOutlet UILabel * lb2; -(IBAction) bt :( id) sender; @ end three attributes are linked to the xib file and can be seen. 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: copy the code-(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;} copy the code to create a cell using code, the following describes how to use UINib: Copy code-(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 ;}