第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

設(shè)置自定義UITableViewCells高度

設(shè)置自定義UITableViewCells高度

iOS
慕姐8265434 2019-12-07 16:03:01
我使用的是自定義UITableViewCell,其中包含一些要顯示的標(biāo)簽,按鈕和圖像視圖。單元格中有一個(gè)標(biāo)簽,其文本是一個(gè)NSString對象,字符串的長度可以是可變的。因此,我無法使用UITableView的heightForCellAtIndex方法為單元格設(shè)置恒定的高度。單元格的高度取決于標(biāo)簽的高度,可以使用NSString's sizeWithFont方法確定。我嘗試使用它,但好像我在某處出錯(cuò)。如何解決?這是用于初始化單元格的代碼。if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]){    self.selectionStyle = UITableViewCellSelectionStyleNone;    UIImage *image = [UIImage imageNamed:@"dot.png"];    imageView = [[UIImageView alloc] initWithImage:image];    imageView.frame = CGRectMake(45.0,10.0,10,10);    headingTxt = [[UILabel alloc] initWithFrame:   CGRectMake(60.0,0.0,150.0,post_hdg_ht)];    [headingTxt setContentMode: UIViewContentModeCenter];    headingTxt.text = postData.user_f_name;    headingTxt.font = [UIFont boldSystemFontOfSize:13];    headingTxt.textAlignment = UITextAlignmentLeft;    headingTxt.textColor = [UIColor blackColor];    dateTxt = [[UILabel alloc] initWithFrame:CGRectMake(55.0,23.0,150.0,post_date_ht)];    dateTxt.text = postData.created_dtm;    dateTxt.font = [UIFont italicSystemFontOfSize:11];    dateTxt.textAlignment = UITextAlignmentLeft;    dateTxt.textColor = [UIColor grayColor];    NSString * text1 = postData.post_body;    NSLog(@"text length = %d",[text1 length]);    CGRect bounds = [UIScreen mainScreen].bounds;    CGFloat tableViewWidth;    CGFloat width = 0;    tableViewWidth = bounds.size.width/2;    width = tableViewWidth - 40; //fudge factor    //CGSize textSize = {width, 20000.0f}; //width and height of text area    CGSize textSize = {245.0, 20000.0f}; //width and height of text area    CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:11.0f]                        constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];這是高度和寬度有問題的標(biāo)簽:textView = [[UILabel alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,ht)];
查看完整描述

3 回答

?
慕桂英4014372

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊

您UITableViewDelegate應(yīng)該實(shí)施tableView:heightForRowAtIndexPath:


目標(biāo)C


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return [indexPath row] * 20;

}

迅捷5


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return indexPath.row * 20

}

您可能需要使用NSString的sizeWithFont:constrainedToSize:lineBreakMode:方法來計(jì)算行高,而不是僅對indexPath進(jìn)行一些愚蠢的數(shù)學(xué)運(yùn)算:)


查看完整回答
反對 回復(fù) 2019-12-07
?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

如果所有行的高度都相同,則只需設(shè)置rowHeightUITableView 的屬性,而不要實(shí)現(xiàn)heightForRowAtIndexPath。蘋果文檔:


使用tableView:heightForRowAtIndexPath:而不是rowHeight會(huì)對性能產(chǎn)生影響。每次顯示表視圖時(shí),它都會(huì)在委托上為其表的每一行調(diào)用tableView:heightForRowAtIndexPath:,這可能導(dǎo)致表視圖具有大量行(大約1000或更多)而導(dǎo)致嚴(yán)重的性能問題。


查看完整回答
反對 回復(fù) 2019-12-07
  • 3 回答
  • 0 關(guān)注
  • 627 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號