IOS_14_tableViewController_xib: Create and encapsulate custom cells. ios custom viewxib
Finally:
Standard steps for xib + custom cell encapsulation: (follow these steps to get twice the result with half the effort)
Standard steps for xib + custom cell encapsulation: (follow these steps to get twice the result with half the effort) 1. Create a GirlCell. xib: drag a tableViewCell to it, drag various controls in cell 2, create a GirlCell class, inherit from uitableViewCell 3, and set GirlCell. in the xib interface, change the cell class name to GirlCell and specify the reused identifer. Generally, write class name 4 to change GirlCell. all the controls on the xib interface are connected to the GirlCell class so that they all become members of the GirlCell class. 5. Create a data model Girl. The method list is as follows // weak is used for the UI control, copy is used for the string, and strong // specifies the name of the Avatar image @ property (nonatomic, copy) NSString * headImgName; // name @ property (nonatomic, copy) NSString * name; // discriminant @ p Roperty (nonatomic, copy) NSString * verdict; // class method. The dictionary conversion object is similar to javaBean one-time filling + (Girl *) girlWithDict :( NSDictionary *) dict; // object method, after setting the attributes of an object, the returned object-(Girl *) initWithDict :( NSDictionary *) dict; 6. encapsulated GirlCell. The method list is as follows: @ property (weak, nonatomic) IBOutlet UIImageView * headImg; @ property (weak, nonatomic) IBOutlet UILabel * name; @ property (weak, nonatomic) IBOutlet UILabel * verdict; // return the cell height in the xib interface + (CGFloa T) cellHeight; // return the reusable cellID + (NSString *) cellID written on the xib interface; // load and instantiate a girlCell object + (GirlCell *) girlCell from xib; // The parameter is the data model girl object. It fills in the values of each control in the xib and returns the cellWithGirl (Girl *) girl object (girlCell *) after encapsulated data; 7. The cellForRow method in the controller is as follows-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// after the cell is encapsulated, the controller knows very little about it ~ // Retrieve your cell class GirlCell * girlCell = [tableView dequeueReusableCellWithIdentifier: [GirlCell cellID] From the cache pool first; if (girlCell = nil) {// if the pool is not obtained, generate a new girlCell using the class method. The xib interface specifies reuse cellID girlCell = [GirlCell girlCell];} // set the unique content Girl * girl = [_ girls objectAtIndex: indexPath. row]; // The parameter is the girl object. Fill in each member value of the object to each control in the xib, and return the filled cell girlCell = [girlCell cellWithGirl: girl]; // return cell return girlCell ;}
Follow these steps to create a tableViewController:
Standard steps for creating tableViewController: 1. Create a project and then delete viewController. h and. m File 2, new File select OC class, inherit from UITableViewController 3, to main. delete the default viewController 4 in the storyboard, and drag a yellow tableViewController to main. storyboard, and change the file's owner to BeyondTableViewController 5, still to main. the automatically generated cell control 6 is deleted from the storyboard.
Girl. h
/// Girl. h // add, delete, modify, and delete 12_tableView /// Created by beyond on 14-7-27. // Copyright (c) 2014 com. beyond. all rights reserved. // # import <Foundation/Foundation. h> @ interface Girl: NSObject // weak is used for the UI control, and copy is used for the string. For other objects, use strong // picture name @ property (nonatomic, copy) NSString * headImgName; // name @ property (nonatomic, copy) NSString * name; // discriminant @ property (nonatomic, copy) NSString * verdict; // class method, the dictionary conversion object is similar to the one-time filling + (Girl *) girlWithDict :( NSDictionary *) dict; // object method. After setting the object attributes, the system returns the object-(Girl *) initWithDict :( NSDictionary *) dict; @ end
Girl. m
/// Girl. m // 12_tableView addition, deletion, modification, // Created by beyond on 14-7-27. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "Girl. h "@ implementation Girl // class method. The dictionary conversion object is similar to javaBean one-time filling + (Girl *) girlWithDict :( NSDictionary *) dict {// only calls the initWithDict method of the object, self is used to ensure compatibility with the subclass. return [[self alloc] initWithDict: dict];} // object method. After setting the attributes of an object, the object-(Girl *) is returned *) initWithDict :( NSDictionary *) dict {// you must first call the init method of the parent NSObject if (self = [super init]) {// you can set the attributes of the object itself. name = dict [@ "name"]; self. headImgName = dict [@ "headImg"]; self. verdict = dict [@ "verdict"] ;}// return the filled object return self ;}@ end
GirlCell. xib
GirlCell. h
//// GirlCell. h // 14_tableViewController_xib custom cell /// Created by beyond on 14-7-28. // Copyright (c) 2014 com. beyond. all rights reserved. /* standard steps for xib + custom cell encapsulation: (follow these steps to get twice the result with half the effort) 1. Create a GirlCell. xib: drag a tableViewCell to it, drag various controls in cell 2, create a GirlCell class, inherit from uitableViewCell 3, and set GirlCell. in the xib interface, change the cell class name to GirlCell and specify the reused identifer. Generally, write class name 4 to change GirlCell. all the controls on the xib interface are connected to the GirlCell class so that they all become members of the GirlCell class. 5. Create a data model Girl. The method list is as follows // UI control uses weak, string uses copy, and other objects use strong // Avatar image name @ property (nonatomic, copy) NSString * headImgName; // name @ property (nonatomic, copy) NSString * name; // discriminant @ property (nonatomic, copy) NSString * verdict; // class method. The dictionary conversion object is similar to one-time filling of javaBean + (Girl *) girlWithDict :( NSDictionary *) dict; // object method. After setting the object attributes, the returned object-(Girl *) initWithDict :( NSDictionary *) dict; 6, encapsulated GirlCell, the method list is as follows: @ property (weak, nonatomic) IBOutlet UIImageView * h EadImg; @ property (weak, nonatomic) IBOutlet UILabel * name; @ property (weak, nonatomic) IBOutlet UILabel * verdict; // return the height of the cell in the xib interface + (CGFloat) cellHeight; // return the reusable cellID + (NSString *) cellID written on the xib interface; // load and instantiate a girlCell object + (GirlCell *) girlCell from xib; // The parameter is the data model girl object. It fills in the values of each control in the xib and returns the cellWithGirl (Girl *) girl object (girlCell *) after encapsulated data; 7. The cellForRow method in the controller is as follows-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// after the cell is encapsulated, the controller knows very little about it ~ // Retrieve your cell class GirlCell * girlCell = [tableView dequeueReusableCellWithIdentifier: [GirlCell cellID] From the cache pool first; if (girlCell = nil) {// if the pool is not obtained, generate a new girlCell using the class method. The xib interface specifies reuse cellID girlCell = [GirlCell girlCell];} // set the unique content Girl * girl = [_ girls objectAtIndex: indexPath. row]; // The parameter is the girl object. Fill in each member value of the object to each control in the xib, and return the filled cell girlCell = [girlCell cellWithGirl: girl]; // return cell return girlCell;} */# import <UIKit/UIKit. h> @ class Girl; @ interface GirlCell: UITableViewCell @ property (weak, nonatomic) IBOutlet UIImageView * headImg; @ property (weak, nonatomic) IBOutlet UILabel * name; @ property (weak, nonatomic) IBOutlet UILabel * verdict; // return the cell height + (CGFloat) cellHeight in the xib interface; // return the reusable cellID + (NSString *) cellID written on the xib interface; // load and instantiate a girlCell object + (GirlCell *) girlCell from xib; // The parameter is the data model girl object, which is filled with the values of each control in xib, and return the encapsulated data, girlCell object-(GirlCell *) cellWithGirl :( Girl *) girl; @ end
GirlCell. m
//// GirlCell. m // 14_tableViewController_xib custom cell /// Created by beyond on 14-7-28. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "GirlCell. h "# import" Girl. h "@ implementation GirlCell // return the cell height + (CGFloat) cellHeight in the xib interface {// check the cell height in xib and return 80 ;} // return the reusable cellID + (NSString *) cellID written on the xib interface {// it must be consistent with the return @ "GirlCell ";} // load and instantiate a girlCell object from the xib + (GirlCell *) girlCell {// mainBundel loads the xib object without writing the extension. xib NSArray * arrayXibObjects = [[NSBundle mainBundle] loadNibNamed: @ "GirlCell" owner: nil options: nil]; return arrayXibObjects [0];} // After the encapsulated data is returned, girlCell object-(GirlCell *) cellWithGirl :( Girl *) girl {// front, connect each control in xib to the GirlCell class through a line, and become its member attribute, in this way, the _ name of each control in xib is not obtained through tag. text = girl. name; _ headImg. image = [UIImage imageNamed: girl. headImgName]; _ verdict. text = girl. verdict; // return the girlCell object return self;} @ end after encapsulated data
Main. storyboard
BeyondTableViewController. h
/// BeyondTableViewController. h // 14_tableViewController_xib custom cell /// Created by beyond on 14-7-28. // Copyright (c) 2014 com. beyond. all rights reserved. // # import <UIKit/UIKit. h> // tableViewController inherits from UIViewController and complies with the data source and proxy. Its internal view is tableView, and the data source and proxy have been connected by itself./* standard step 1 for tableViewController creation, create a project, and then delete viewController. h and. m File 2, new File select OC class, inherit from UITableViewController 3, to main. delete the default viewController 4 in the storyboard, and drag a yellow tableViewController to main. storyboard, and change the file's owner to BeyondTableViewController 5, still to main. delete the automatically generated cell control 6 from the storyboard and run the */@ interface BeyondTableViewController: UITableViewController @ end command.
BeyondTableViewController. m
/// BeyondTableViewController. m // 14_tableViewController_xib custom cell /// Created by beyond on 14-7-28. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "BeyondTableViewController. h "# import" Girl. h "# import" GirlCell. h "@ interface BeyondTableViewController () {// All girls loaded from the plist file, returns an array of all objects NSMutableArray * _ girls ;} @ end @ implementation BeyondTableViewController // hide the top Status Bar-(BOOL) pref ErsStatusBarHidden {return YES;}-(void) viewDidLoad {[super viewDidLoad]; // initialize the object array _ girls = [NSMutableArray array]; // plist to object array [self plistToObjects];} // plist to object array-(void) plistToObjects {// sg_bundle template code, 1, get. MAIN package of the app; 2. Return the full fullPath of a file in the main package. NSBundle * mainBundle = [NSBundle mainBundle]; NSString * fullPath = [mainBundle pathForResource: @ "girls. plist "ofType: nil]; // returns a dictionary array from the plist file based on the full path NSArray * arrayWithDict = [NSArray arrayWithContentsOfFile: fullPath]; // class method of the model to return the object. You only need a dictionary array for the parameter (NSDictionary * dict in arrayWithDict) {// The parameter only needs a dictionary. As a result, the controller does not need to know much about it. Girl * girl = [Girl girlWithDict: dict]; // Add it to the object array [_ girls addObject: girl] ;}# pragma mark-data source method-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {// return the number of rows return _ girls. count;}-(UITableViewC Ell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// after the cell is encapsulated, the controller knows very little about it ~ // Retrieve your cell class GirlCell * girlCell = [tableView dequeueReusableCellWithIdentifier: [GirlCell cellID] From the cache pool first; if (girlCell = nil) {// if the pool is not obtained, generate a new girlCell using the class method. The xib interface specifies reuse cellID girlCell = [GirlCell girlCell];} // set the unique content Girl * girl = [_ girls objectAtIndex: indexPath. row]; // The parameter is the girl object. Fill in each member value of the object to each control in the xib, and return the filled cell girlCell = [girlCell cellWithGirl: girl]; // return cell return girlCell;} # pragma mark-proxy method // The height of each row-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {// call the class method to obtain the high return [GirlCell cellHeight] of the cell line in the xib;} // After the default click is canceled, the blue background is highlighted-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {[self. tableView deselectRowAtIndexPath: indexPath animated: YES];} @ end
Girls. plist
TableViewController hierarchy
How to put multiple custom cells in different rows of tableview
1. Create the TableViewCell class and inherit the parent class as UITableViewCell.
1.1 "TableCell. h"
# Import <UIKit/UIKit. h>
/**
* Room table display structure
*/
@ Interface TableCell: UITableViewCell {
UILabel * tableNoLable; // table number
UIImageView * tableImageView; // table Image
UIImageView * tableStateImageView; // Table Status picture
}
@ Property (nonatomic, retain) IBOutlet UILabel * tableNoLable; // table number
@ Property (nonatomic, retain) IBOutlet UIImageView * tableImageView; // table Image
@ Property (nonatomic, retain) IBOutlet UIImageView * tableStateImageView; // Table Status picture
1.2 TableCell. m
# Import "TableCell. h"
@ Implementation TableCell
@ Synthesize tableNoLable; // table number
@ Synthesize tableImageView; // table Image
@ Synthesize tableStateImageView; // Table Status picture
-(Id) initWithStyle :( UITableViewCellStyle) style reuseIdentifier :( NSString *) reuseIdentifier {
If (self = [super initWithStyle: style reuseIdentifier: reuseIdentifier]) {
// Initialization code
}
Return self;
}
-(Void) setSelected :( BOOL) selected animated :( BOOL) animated {
[Super setSelected: selected animated: animated];
// Configure the view for the selected state
}
-(Void) dealloc {
[TableNoLable release];
[TableImageView release];
[Hand3ImageView release];
[Super dealloc];
}
@ End
1.3 layout the layout file (xib file) indicates that the class is your customized tabelcellview
A new Empty Xib file that does not contain views. Drag a UITableCell in libriary to the boxes of the two icons, double-click cellview, specify the class as the cell. m file, and start editing.
2. Load your c... the remaining full text in tableView on the page>
For ios development, similar to the layout of the chat interface, add a long-press event to the cell in tableview to display custom
Custom method:
UIMenuItem * share = [[UIMenuItem alloc] initWithTitle: @ "share" action: @ selector (shareClick :)];
UIMenuItem * report = [[UIMenuItem alloc] initWithTitle: @ "report" action: @ selector (report :)];
UIMenuController * menu = [UIMenuController sharedMenuController];
[Menu setMenuItems: [NSArray arrayWithObjects: share, report, nil];
[Menu setTargetRect: cell. c_post_content.frame inView: cell. c_post_content.superview];
[Menu setMenuVisible: YES animated: YES];