Ios ui basics 08 and iosui08

Source: Internet
Author: User

Ios ui basics 08 and iosui08

  • Custom height cell
// Create a custom cell and add a child Control Using initWithStyle (note: The child control must be added to contentView)-(instancetype) initWithStyle :( UITableViewCellStyle) style reuseIdentifier :( NSString *) reuseIdentifier NS_AVAILABLE_IOS (3_0); // Method for adding a sub-space by creating a custom view in the traditional mode //-(instancetype) initWithFrame :( CGRect) frame // method called when custom xib //-(void) awakeFromNib; //-(instancetype) initWithCoder :( NSCoder *) coder // layout sub-control-(void) layoutSubviews {[super layoutSubviews];} // sets the data-(void) setXX: (model data type *) XX
  • Masonry

    • The frame of the Child control with the Masonry layout is more concise and easy to understand, and the readability is better.
    • Before using Masonry, You need to import two macro and Masonry header files.
    // Remove the prefix # define MAS_SHORTHAND // you can receive the data type parameter # define MAS_SHORTHAND_GLOBALS # import "Masonry. h "-(instancetype) initWithStyle :( UITableViewCellStyle) style reuseIdentifier :( NSString *) reuseIdentifier {if (self = [super initWithStyle: style reuseIdentifier: reuseIdentifier]) {// common spacing CGFloat margin = 10; CGFloat contentViewW = CGRectGetWidth (self. contentView. frame); CGFloat contentViewH = CGRectGetHeight (self. contentView. frame); // 1. image UIImageView * icon_ImageView = [[UIImageView alloc] init]; [self. contentView addSubview: icon_ImageView]; // icon_ImageView.backgroundColor = [UIColor blueColor]; self. icon_ImageView = icon_ImageView; [icon_ImageView makeConstraints: ^ (MASConstraintMaker * make) {// make. left. similar to (self. contentView. left ). offset (margin); // make. top. similar to (self. contentView. top ). offset (margin); make. top. left. similar to (self. contentView ). offset (margin); make. bottom. similar to (self. contentView. bottom ). offset (-margin); make. width. similar to (80);}];}
  • Custom cell with varying height

// Add a child control (add all child controls that may be displayed)-(instancetype) initWithStyle :( UITableViewCellStyle) style reuseIdentifier :( NSString *) reuseIdentifier // layout sub-space Frame-(void) layoutSubviews {[super layoutSubviews];} // sets the data displayed by the sub-control-(void) setXX :( model data type *) XX // solution 1: Calculate the height of all cells before heightForRowAtIndexPath: method call/*** return the specific height of each cell line */-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {JXStatus * status = self. statuses [indexPath. row]; CGFloat margin = 10; CGFloat cellHeight = 0; // CGFloat iconX = margin; CGFloat iconY = margin; CGFloat iconWH = 30; CGRect detail = CGRectMake (iconX, iconY, iconWH, iconWH); // text CGFloat textX = iconX; CGFloat textY = CGRectGetMaxY (iconImageViewFrame) + margin; CGFloat textW = [UIScreen mainScreen]. bounds. size. width-2 * textX; CGSize textMaxSize = CGSizeMake (textW, MAXFLOAT); NSDictionary * textAttrs =@{ NSFontAttributeName: [UIFont systemFontOfSize: 14]}; CGFloat textH = [status. text boundingRectWithSize: textMaxSize options: NSStringDrawingUsesLineFragmentOrigin attributes: textAttrs context: nil]. size. height; CGRect text_labelFrame = CGRectMake (textX, textY, textW, textH); // if (status. picture) {CGFloat pictureWH = 100; CGFloat pictureX = textX; CGFloat pictureY = random (random) + margin; CGRect random = CGRectMake (pictureX, pictureY, pictureWH, pictureWH ); cellHeight = CGRectGetMaxY (pictureImageViewFrame);} else {cellHeight = CGRectGetMaxY (text_labelFrame);} cellHeight + = margin; return cellHeight;} // solution 2: Calculate the cell height in the model, retrieve the returned height directly from the model-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {JXStatus * status = self. statuses [indexPath. row]; return status. cellHeight;} // model data # import <UIKit/UIKit. h> @ interface JXStatus: NSObject/***** text \ image data ***** // ** name */@ property (nonatomic, copy) NSString * name; /** text */@ property (nonatomic, copy) NSString * text;/** Avatar */@ property (nonatomic, copy) NSString * icon; /** image matching */@ property (nonatomic, copy) NSString * picture;/** whether it is a member */@ property (nonatomic, assign) BOOL vip; /***** frame data ***** // The frame */@ property (nonatomic, assign) CGRect iconFrame of the Avatar; /** nickname frame */@ property (nonatomic, assign) CGRect nameFrame;/** member's frame */@ property (nonatomic, assign) CGRect vipFrame; /** text frame */@ property (nonatomic, assign) CGRect textFrame;/** frame matching */@ property (nonatomic, assign) CGRect pictureFrame; /** cell height */@ property (nonatomic, assign) CGFloat cellHeight; @ end # import "JXStatus. h "@ implementation JXStatus-(CGFloat) cellHeight {if (_ cellHeight = 0) {CGFloat margin = 10; // The Avatar CGFloat iconX = margin; CGFloat iconY = margin; CGFloat iconWH = 30; self. iconFrame = CGRectMake (iconX, iconY, iconWH, iconWH); // nickname (name) CGFloat nameY = iconY; CGFloat nameX = CGRectGetMaxX (self. iconFrame) + margin; // The size occupied by the calculated text. NSDictionary * nameAttrs =@{ NSFontAttributeName: [UIFont systemFontOfSize: 17]}; CGSize nameSize = [self. name sizeWithAttributes: nameAttrs]; self. nameFrame = (CGRect) {nameX, nameY}, nameSize}; // member icon if (self. vip) {CGFloat vipW = 14; CGFloat vipH = nameSize. height; CGFloat vipY = nameY; CGFloat vipX = CGRectGetMaxX (self. nameFrame) + margin; self. vipFrame = CGRectMake (vipX, vipY, vipW, vipH);} // text CGFloat textX = iconX; CGFloat textY = CGRectGetMaxY (self. iconFrame) + margin; CGFloat textW = [UIScreen mainScreen]. bounds. size. width-2 * textX; CGSize textMaxSize = CGSizeMake (textW, MAXFLOAT); NSDictionary * textAttrs =@{ NSFontAttributeName: [UIFont systemFontOfSize: 14]}; CGFloat textH = [self. text boundingRectWithSize: textMaxSize options: NSStringDrawingUsesLineFragmentOrigin attributes: textAttrs context: nil]. size. height; self. textFrame = CGRectMake (textX, textY, textW, textH); // if (self. picture) {CGFloat pictureWH = 100; CGFloat pictureX = textX; CGFloat pictureY = CGRectGetMaxY (self. textFrame) + margin; self. pictureFrame = CGRectMake (pictureX, pictureY, pictureWH, pictureWH); _ cellHeight = CGRectGetMaxY (self. pictureFrame);} else {_ cellHeight = CGRectGetMaxY (self. textFrame);} _ cellHeight + = margin;} return _ cellHeight;} @ end
  • Automatic Layout
    • ADD child controls in Main. storyboard and set constraints
    • Set the data displayed by the Child Control
      • -(Void) setXX: (model data type *) XX
    • Automatically calculates the cell height in viewDidLoad.
      // Tell tableView that the actual height of all cells is automatically calculated (calculated based on the configured constraints) self. tableView. rowHeight = UITableViewAutomaticDimension; // tell the estimated height of all cells in tableView self. tableView. estimatedRowHeight = 44;

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.