IOS --- UITableViewCell adaptive Row Height (non-AutoLayout)

Source: Internet
Author: User

IOS --- UITableViewCell adaptive Row Height (non-AutoLayout)

As shown in the question, this program uses the UITableView self-adaptive Row Height written by non-AutoLayout, and then I will write a small demo of self-adaptive Row Height Based on AutoLayout.
PS: This applet is only applicable to children who are new to IOS. It is only used for reference and is untechnical.

Code:

// UITableViewCell # import
  
   
@ Interface commentaryCell: UITableViewCell @ property (retain, nonatomic) UILabel * userID; @ property (retain, nonatomic) UILabel * date; @ property (retain, nonatomic) UILabel * commentary; -(void) setContent :( NSString *) userid_dic :( NSString *) date_dic :( NSString *) comment_dic; @ end # import "commentaryCell. h "float width; float height; NSString * commentaryStr; @ implementation commentaryCell @ synthesize userID; @ synthesize date; @ synthesize commentary;-(id) initWithStyle :( UITableViewCellStyle) style reuseIdentifier :( NSString *) reuseIdentifier {self = [super initWithStyle: style reuseIdentifier: reuseIdentifier]; if (self) {self. contentView. backgroundColor = [UIColor whiteColor]; [self createView];} return self;}-(void) createView {width = self. contentView. frame. size. width; height = self. contentView. frame. size. height; // user ID userID = [[UILabel alloc] initWithFrame: CGRectMake (width * 0.05, height * 0.3, width * 0.3, height * 0.3)]; userID. font = [UIFont systemFontOfSize: 14]; [userID setTextColor: [UIColor colorWithRed: 85.0/255.0 green: 85.0/255.0 blue: 85.0/255.0 alpha: 0.6]; [self. contentView addSubview: userID]; // time icon // time date = [[UILabel alloc] initWithFrame: CGRectMake (width * 0.4, height * 0.3, width * 0.5, height * 0.3)]; date. font = [UIFont systemFontOfSize: 14]; [date setTextColor: [UIColor colorWithRed: 85.0/255.0 green: 85.0/255.0 blue: 85.0/255.0 alpha: 0.6]; [self. contentView addSubview: date]; // commentary = [[UILabel alloc] init]; [commentary setNumberOfLines: 0]; commentary. font = [UIFont systemFontOfSize: 16]; [commentary setTextColor: [UIColor colorWithRed: 85.0/255.0 green: 85.0/255.0 blue: 85.0/255.0 alpha: 1]; [self. contentView addSubview: commentary];}-(void) setContent :( NSString *) userid_dic :( NSString *) date_dic :( NSString *) comment_dic {userID. text = userid_dic; date. text = date_dic; [commentary setNumberOfLines: 0]; commentary. text = comment_dic; commentary. font = [UIFont systemFontOfSize: 16]; CGSize commentSize = [self returnSize: commentary. text font: commentary. font]; [commentary setFrame: CGRectMake (width * 0.05, 41, commentSize. width, commentSize. height)];} // return the Size of the Label-(CGSize) returnSize :( NSString *) text font :( UIFont *) font {float width = [UIScreen mainScreen]. bounds. size. width; float height = [UIScreen mainScreen]. bounds. size. height; CGSize _ Size = CGSizeMake (width * 0.9, height); CGSize Size = [text sizeWithFont: font constrainedToSize: _ Size lineBreakMode: NSLineBreakByWordWrapping]; return Size ;}
  

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.