IOS---UITableViewCell自適應行高(非AutoLayout)

來源:互聯網
上載者:User

IOS---UITableViewCell自適應行高(非AutoLayout)

如題所示,本程式是使用非AutoLayout寫的UITableView自適應行高,之後筆者將會寫一個基於AutoLayout的自適應行高的小demo。
PS:此小程式只適用於剛接觸IOS的小朋友,只用做參考,毫無技術性,大神勿噴。

上代碼:

//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;    //使用者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];    //時間表徵圖    //時間    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)];}//返回Label的Size-(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;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.