您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 非 AutoLayout 實作 UITableViewCell 自適應行高

編輯:iOS 開發綜合

本文示範如何在不使用 Auto Layout 的前提下,讓 UITableViewCell 依內容自動撐開高度。核心思路很單純:先把文字放進 UILabel 並關閉行數限制,再用 sizeWithFont:constrainedToSize:lineBreakMode: 量出實際佔用的尺寸,最後把這個高度回報給 tableView。

(下一篇會補上基於 Auto Layout 的版本。)

自訂 Cell 實作

#import <UIKit/UIKit.h>

@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];   // 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 實際需要的尺寸
-(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;
}

@end

使用時機要注意:在 tableView:heightForRowAtIndexPath: 中同樣呼叫一次量測方法回傳高度,並建議把結果快取起來(例如存進陣列或字典),否則每次滾動都要重新計算文字尺寸,列表一長就會明顯掉幀。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved