iOS UITableViewCell自適應高度

來源:互聯網
上載者:User

標籤:

  在cell.m檔案中

  1)初始化方法中:

 

 self.lalName=[[UILabel alloc] initWithFrame:CGRectMake(71, 5, 250, 40)];    [self addSubview:self.lalName];

 

 2)建立方法:

 

//給使用者介紹賦值並且實現自動換行-(void)setIntroductionText:(NSString*)text{    //獲得當前cell的高度    CGRect frame=[self frame];    //文本賦值    self.lalName.text=text;    CGSize size=CGSizeMake(300, 1000);    //設定label的最大行數    self.lalName.numberOfLines=10;    CGSize labelSize=[self.lalName.text sizeWithFont:self.lalName.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];    self.lalName.frame=CGRectMake(self.lalName.frame.origin.x, self.lalName.frame.origin.y, labelSize.width, labelSize.height) ;        //計算出自適應的高度    frame.size.height=labelSize.height+20;    self.frame =frame;    }

  

  在ViewController中:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *[email protected]"acell";    TextKitTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:acell];    if (!cell) {        cell=[[[NSBundle mainBundle] loadNibNamed:@"TextKitTableViewCell" owner:self options:nil]lastObject];    }         [cell setIntroductionText:@"where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you?where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you?"];    return cell;    }

  當然別忘記了返回給cell高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell=[self tableView:self.tableViewTextKit cellForRowAtIndexPath:indexPath];    NSLog(@"height===%f",cell.frame.size.height);    return cell.frame.size.height;}

  

iOS UITableViewCell自適應高度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.