IPhone Development Study Notes 002 -- Xib design UITableViewCell and then dynamically load

Source: Internet
Author: User
Tags button attributes

(Note: Environment Mac OS X Lion 10.7.3 + Xcode 4.2.1 + iOS SDK 5.0 .)
1. Create an iOS Application project and select Single View Application. Do not select Use Storyboard. If both the product name and class prefix are specified as one, the Code View is automatically generated after completion, for example:




By default, this application loads the view of oneViewController. Double-click oneViewController. xib and add the control UILabel, UIButton, and UITableView on the default view of this xib, as shown in:



In the header file of oneViewController and *. the m file defines three attributes: label, button, and tableViewCell. The first two correspond to labels and buttons on the xib interface, and the last one corresponds to tableViewCell in the xib file to be created, in addition, the label and button attributes are associated with the controls on the interface by dragging, for example:
OneViewController. h:



OneViewController. m:




2. Create the xib file Cell. xib whose root view is UITableViewCell:
You cannot directly generate an xib file whose root view is UITableViewCell. You can first create an xib file whose root view is UIView, and then delete the Root view of the xib file, drag a Table View Cell from the Library window to the xib as the Root view of the xib. for example:



At the time of generation:



Delete the view above, drag a table view cell instead, and add a label and a button on the table view cell, such:



Set the Cell Label and Cell Button View Tag under Table view Cell to 1 and 2 respectively:



Then, select the File's owner and select the corresponding loading Class oneViewController in the identify inspector view Custom Class:



In addition, the UITableViewCell object tableViewCell in Outlets under connections inspector is dragged and connected to the Table View Cell under Objects in the intermediate View window: (Figure XXX)



In Attributes inspector, set the identifier of Table View Cell to CellIdentifier:



Save.

3. dynamically load Table View Cell XIB in the Code:
Enable oneViewController. h to enable oneViewController to implement the Protocol UITableViewDataSource and UITableViewDelegate, that is:

 

The following interface method is implemented in oneViewController. m:


-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
Return20;
}

// Row display. Implementers shoshould * always * try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells
DequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
StaticNSString * CustomCellIdentifier = @ "CellIdentifier ";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: mcmcellidentifier];
If (cell = nil ){
NSArray * nib = [[NSBundlemainBundle] loadNibNamed: @ "Cell" owner: selfoptions: nil];
// If ([nib count]> 0 ){
// Cell = self. tableViewCell;
//} Else {
// NSLog (@ "failed to load CustomCell nib file! ");
//}
Cell = [nib objectAtIndex: 0]; // either of the two methods can be used. However, if there is no drag-and-drop connection process in the red area (Figure XXX) above, you can only use the nib objectAtIndex method here.
}
NSUInteger row = [indexPathrow];

NSLog (@ "++ jonesduan % s, cell row: % d", _ func __, row );

UILabel * cellLabel = (UILabel *) [cellviewWithTag: 1];
CellLabel. text = [NSStringstringWithFormat: @ "cell index: % d", row];

UIButton * cellButton = (UIButton *) [cellviewWithTag: 2];
[CellButton setTitleColor: [UIColorredColor] forState: UIControlStateNormal];
[CellButton setTitleColor: [UIColororangeColor] forState: UIControlStateHighlighted];
[CellButton setTitle: @ "Press me! "ForState: UIControlStateNormal];
[CellButton setTitle: @ "Pressed! "ForState: UIControlStateHighlighted];
Return cell;
}

-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {
// Modified by jonesduan.
Return80;
}

-(Void) didReceiveMemoryWarning
{
[SuperdidReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

# Pragma mark-View lifecycle
-(Void) viewDidLoad
{
[SuperviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[Self. labelsetBackgroundColor: [UIColorgrayColor];
[Self. buttonsetTitleColor: [UIColorgreenColor] forState: UIControlStateNormal];
[Self. buttonsetTitleColor: [UIColororangeColor] forState: UIControlStateHighlighted]; // here, the label and button in oneViewController. xib are controlled, which have been dragged and connected.
}
After this is done. Run CMD + R to see if the interface is displayed, as shown in:

 


No Cell is displayed. cell Label and Cell Button in xib. The following is the most important thing that a person has learned for a long time: Select oneViewController. switch to connections inspector, and drag and drop the dataSource and delegate of the Table view to the File's owner. For example:





After the connection, run CMD + R to see if the result is as shown in,




OK, success!

Conclusion: the most important thing to use a Custom view through xib is the view controller Used by this view. For example, in xxxViewController, you must specify the corresponding loading Class as xxxViewController in the identity M Class of identity inspector, select the File's owner (under Placeholders in the large window in the middle), and switch to connections inspector to switch the Objects under Outlets to the Objects in the large window in the middle (at the same level as Placeholders, under Placeholders) the Root View under is connected. Finally, do not forget to drag and drop the dataSource and delegate of Table View to connect to the File's owner.
The least habit of APPLE development is to drag and drop the connection. Here we record it as a personal reference, so that you do not forget it later.

Demo: http://download.csdn.net/detail/duanyipeng/4063778


From Code Heaven
 



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.