自定義高等Cell之frame


  • 1.創(chuàng)建一個繼承自UITableViewCell的子類,比如IZGrouponCell

    • 1.1 在IZGrouponCell.m中重寫initWithStyle:reuseIdentifier:方法
      • 添加子控件
      • 設(shè)置子控件的初始化屬性(比如文字顏色、字)
    //在這個方法中添加所有的子控件
        -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
            {
                if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
                    // ......
                }
                return self;
            }
- **1.2 重寫`-layoutSubviews`方法**
    >- 一定要調(diào)用`[super layoutSubviews]`
    >- 在這個方法中計算和設(shè)置所有子控件的frame
```objc
//在這個方法中計算所有子控件的frame
    -(void)layoutSubviews
    {
        [super layoutSubviews];
        // ......
    }
```
- **1.3 需要提供一個模型屬性,重寫模型的set方法,在這個方法中設(shè)置模型數(shù)據(jù)到子控件**
    >- 在IZGrouponCell.h文件中提供一個模型屬性,比如IZGroupon模型
@class IZGroupon;
@interface IZGrouponCell : UITableViewCell
/** 團購模型數(shù)據(jù) */
@property (nonatomic, strong) IZGroupon *groupon;
@end
    >-  在IZGrouponCell.m中重寫模型屬性的set方法
-(void)setGroupon:(IZGroupon *)groupon
{
    _groupon = groupon;
        // .......
}
  • 2.在控制器中

    • 利用registerClass...方法注冊Cell類

[self.tableView registerClass:[IZGrouponCell class] forCellReuseIdentifier:ID];

    - **給cell傳遞模型數(shù)據(jù)**

    ```objc
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            // 訪問緩存池
            IZGrouponCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

            // 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
            cell.groupon = self.groupon[indexPath.row];

            return cell;
        }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容