国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOSUI基礎08

2019-11-14 18:33:56
字體:
來源:轉載
供稿:網友
  • 自定義等高cell
    // 創建自定義cell添加子控件的方法initWithStyle(note:子控件要添加到contentView上)    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier NS_AVAILABLE_IOS(3_0);    // 傳統創建自定義view添加子空間的方法    //- (instancetype)initWithFrame:(CGRect)frame    // 自定義xib時調用的方法    //- (void)awakeFromNib;    //- (instancetype)initWithCoder:(NSCoder *)coder    // 布局子控件    - (void)layoutSubviews   {      [super layoutSubviews];   }   //設置數據   - (void)setXX:(模型數據類型 *)XX
  • Masonry

    • 用Masonry布局子控件frame更簡潔易懂,可讀性更好。
    • 使用Masonry之前需要導入2個宏和Masonry頭文件
     //除掉前綴  #define MAS_SHORTHAND //可接收數據類型參數  #define MAS_SHORTHAND_GLOBALS  #import "Masonry.h"  - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{  if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {      // 常用的間距      CGFloat margin = 10;      CGFloat contentViewW = CGRectGetWidth(self.contentView.frame);      CGFloat contentViewH = CGRectGetHeight(self.contentView.frame);      // 1.圖片      UIImageView *icon_ImageView = [[UIImageView alloc] init];      [self.contentView addSubview:icon_ImageView];      //icon_ImageView.backgroundColor = [UIColor blueColor];      self.icon_ImageView = icon_ImageView;      [icon_ImageView makeConstraints:^(MASConstraintMaker *make) {//            make.left.equalTo(self.contentView.left).offset(margin);//            make.top.equalTo(self.contentView.top).offset(margin);          make.top.left.equalTo(self.contentView).offset(margin);          make.bottom.equalTo(self.contentView.bottom).offset(-margin);          make.width.equalTo(80);      }];}
  • 自定義不等高cell

     // 添加子控件(把有可能顯示的子控件都加進去)     - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier     //布局子空間Frame     - (void)layoutSubviews    {        [super layoutSubviews];    }    // 設置子控件顯示的數據    - (void)setXX:(模型數據類型 *)XX    //方案1:在heightForRowAtIndexPath:方法調用之前將所有cell的高度計算清楚    /** *  返回每一行cell的具體高度 */- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    JXStatus *status = self.statuses[indexPath.row];    CGFloat margin = 10;    CGFloat cellHeight = 0;    // 頭像    CGFloat iconX = margin;    CGFloat iconY = margin;    CGFloat iconWH = 30;    CGRect iconImageViewFrame = CGRectMake(iconX, iconY, iconWH, iconWH);    // 文字    CGFloat textX = iconX;    CGFloat textY = CGRectGetMaxY(iconImageViewFrame) + margin;    CGFloat textW = [UIScreen mainScreen].bounds.size.width - 2 * textX;    CGSize textMaxSize = CGSizeMake(textW, MAXFLOAT);    NSDictionary *textAttrs = @{NSFontAttributeName : [UIFont systemFontOfSize:14]};    CGFloat textH = [status.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttrs context:nil].size.height;    CGRect text_labelFrame = CGRectMake(textX, textY, textW, textH);    // 配圖    if (status.picture) {        CGFloat pictureWH = 100;        CGFloat pictureX = textX;        CGFloat pictureY = CGRectGetMaxY(text_labelFrame) + margin;        CGRect pictureImageViewFrame = CGRectMake(pictureX, pictureY, pictureWH, pictureWH);        cellHeight = CGRectGetMaxY(pictureImageViewFrame);    } else {        cellHeight = CGRectGetMaxY(text_labelFrame);    }    cellHeight += margin;    return cellHeight;}// 方案2:在模型中計算cell高度,返回高度直接從模型中取出- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    JXStatus *status = self.statuses[indexPath.row];    return status.cellHeight;}//模型數據#import <UIKit/UIKit.h>@interface JXStatus : NSObject/**** 文字/圖片數據 ****//** 姓名 */@PRoperty (nonatomic, copy) NSString *name;/** 文本 */@property (nonatomic, copy) NSString *text;/** 頭像 */@property (nonatomic, copy) NSString *icon;/** 配圖 */@property (nonatomic, copy) NSString *picture;/** 是否為會員 */@property (nonatomic, assign) BOOL vip;/**** frame數據 ****//** 頭像的frame */@property (nonatomic, assign) CGRect iconFrame;/** 昵稱的frame */@property (nonatomic, assign) CGRect nameFrame;/** 會員的frame */@property (nonatomic, assign) CGRect vipFrame;/** 文字的frame */@property (nonatomic, assign) CGRect textFrame;/** 配圖的frame */@property (nonatomic, assign) CGRect pictureFrame;/** cell的高度 */@property (nonatomic, assign) CGFloat cellHeight;@end#import "JXStatus.h"@implementation JXStatus- (CGFloat)cellHeight{    if (_cellHeight == 0) {        CGFloat margin = 10;        // 頭像        CGFloat iconX = margin;        CGFloat iconY = margin;        CGFloat iconWH = 30;        self.iconFrame = CGRectMake(iconX, iconY, iconWH, iconWH);        // 昵稱(姓名)        CGFloat nameY = iconY;        CGFloat nameX = CGRectGetMaxX(self.iconFrame) + margin;        // 計算文字所占據的尺寸        NSDictionary *nameAttrs = @{NSFontAttributeName : [UIFont systemFontOfSize:17]};        CGSize nameSize = [self.name sizeWithAttributes:nameAttrs];        self.nameFrame = (CGRect){{nameX, nameY}, nameSize};        // 會員圖標        if (self.vip) {            CGFloat vipW = 14;            CGFloat vipH = nameSize.height;            CGFloat vipY = nameY;            CGFloat vipX = CGRectGetMaxX(self.nameFrame) + margin;            self.vipFrame = CGRectMake(vipX, vipY, vipW, vipH);        }        // 文字        CGFloat textX = iconX;        CGFloat textY = CGRectGetMaxY(self.iconFrame) + margin;        CGFloat textW = [UIScreen mainScreen].bounds.size.width - 2 * textX;        CGSize textMaxSize = CGSizeMake(textW, MAXFLOAT);        NSDictionary *textAttrs = @{NSFontAttributeName : [UIFont systemFontOfSize:14]};        CGFloat textH = [self.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttrs context:nil].size.height;        self.textFrame = CGRectMake(textX, textY, textW, textH);        // 配圖        if (self.picture) {            CGFloat pictureWH = 100;            CGFloat pictureX = textX;            CGFloat pictureY = CGRectGetMaxY(self.textFrame) + margin;            self.pictureFrame = CGRectMake(pictureX, pictureY, pictureWH, pictureWH);            _cellHeight = CGRectGetMaxY(self.pictureFrame);        } else {            _cellHeight = CGRectGetMaxY(self.textFrame);        }        _cellHeight += margin;    }    return _cellHeight;}@end
  • 自動布局
    • 在Main.storyboard添加好子控件,設置好約束
    • 設置子控件顯示的數據
      • -(void)setXX:(模型數據類型 *)XX
    • 在viewDidLoad中自動計算cell高度
      // 告訴tableView所有cell的真實高度是自動計算(根據設置的約束來計算)self.tableView.rowHeight = UITableViewAutomaticDimension;// 告訴tableView所有cell的估算高度self.tableView.estimatedRowHeight = 44;

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新安县| 临安市| 本溪| 大方县| 崇礼县| 临汾市| 克拉玛依市| 定日县| 湖口县| 新余市| 长治市| 林西县| 辽中县| 衡南县| 麻栗坡县| 嘉峪关市| 儋州市| 海伦市| 萨迦县| 吉林省| 刚察县| 香河县| 十堰市| 新竹县| 桓台县| 丹寨县| 深水埗区| 繁昌县| 靖西县| 武隆县| 龙泉市| 平和县| 蓬莱市| 荣成市| 松阳县| 凯里市| 门头沟区| 建宁县| 新安县| 福安市| 凌源市|