iOS開發UITableView 之代碼自訂cell的步驟

來源:互聯網
上載者:User

雖然在UITableView中可以直接拖控制項的方式進行添加cell,但是這種方式有一個致命的缺點,那就是cell是固定的,而且cell的高度難以更改。在實際的開發中並不能滿足我們的需求。比如以下:

*text;@property (nonatomic,copy) NSString *icon;@property (nonatomic,copy) NSString *name;@property (nonatomic,copy) NSString *picture;@property (nonatomic,assign,getter = isVip) BOOL *vip;

- (instancetype) initWithDict: (NSDictionary *)dict;+ (instancetype) weiboWithDict: (NSDictionary *)dict;

方法內部實現就不再贅述了!

之所以建立frame模型,是為了封裝計算cell內各個控制項位置的過程。這體現了OOP的編程思想!

在Frame模型中,各個控制項的Frame會根據傳遞過來的資料模型進行計算設定,所以Frame模型中必須有一個資料模型屬性,且計算各個控制項的frame的過程應該放在模型資料屬性的set方法中,並且在這裡計算出cell的高度

*

 

有frame模型類型的成員屬性,用來對cell內的各個控制項設定frame 以及資料

因為frame模型中有一個資料模型 所以這裡無需再引入資料模型

@property (nonatomic, strong) CLWeiBoFrame *weiboFrame;

並且提供一個類方法,外界調用建立cell的介面,需要傳入參數tableView  。傳入tableView參數是為了能夠取得tableView內的cell隊列迴圈利用cell

+ (instancetype)cellWithTableView:(UITableView* NSString *ID = *cell = (cell ===

-()initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *
=


         
UIImageView *iconView =

=
         
UILabel *nameView =
=

=
         
UIImageView *vipView =
= [UIImage imageNamed:

=
         
UILabel *textView =
= 
=

=
         
UIImageView *pictureView =

=
- () setCLWeiBoFrame:(MJStatusFrame *
=


 

 

 

 

*- (NSArray *
 (_weiboframes ==


         
NSString *path = [[NSBundle mainBundle]pathForResource:
         
NSArray *dictArray =
         
NSMutableArray *weibosFrameArray =
 (NSDictionary *dict 
             
CLWeiBo *weibo =
             
CLWeiBoFrame *weiboFrame =
=


         
_weiboframes =


 

 

- (NSInteger)tableView:(UITableView *
- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*
     
MJStatusCell *cell =
     
cell.statusFrame =
     

 

4.3 實現代理方法

 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *
     
MJStatusFrame *statusFrame =

到此完畢!

聯繫我們

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