ios-單元格複用

1、創建單元格的幾種方式

a)  通過UITableViewCell固定格式設置,其屬性是imageView, textLabel、detailLabel,但它們的樣式固定,且通常來說不易改變它們的位置,不夠靈活

b)  通過UITableViewCell的contentView屬性添加子視圖

c)  使用xib自定義子視圖,開發較爲迅速

d) 子類化UITableViewCell,更加面向對象 


2. 通過複用id的方法創建

   

    //1) 定義id

    static NSString *cellID = @"cellID";

    //2) 通過id創建

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    //3) 判斷

    if (cell == nil) {

        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle

                                 reuseIdentifier:cellID];

    }


3、通過xib方式複用創建

    // 定義id

    NSString *cellID = @"cellID";

    // 通過id獲取單元格

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    // 爲空的情況

    if (cell == nil) {

        // 通過文件引入單元格

        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCell"

                                             owner:self

                                            options:nil]lastObject];

    }


4、註冊

 //a) 獲取nib文件

    UINib *nib = [UINib nibWithNibName:@"MyTableCell" bundle:[NSBundle mainBundle]];

 //b) 通過nib註冊

    [tableView registerNib:nib forCellReuseIdentifier:cellID];


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID

                                                            forIndexPath:indexPath];



發佈了47 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章