4、iOS 開發之 UITableView

來源:互聯網
上載者:User

標籤:uitableview

一、UITableView的建立
  • 表格控制項在建立時必須指定樣式,只能使用以下執行個體化方法
[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

UITableView的兩種樣式

 1> UITableViewStylePlain 2> UITableViewStyleGrouped

2.UITableView的常見屬性

// 頭部視圖(廣告)@property (nonatomic, retain) UIView *tableHeaderView;// 顯示載入的狀態@property (nonatomic, retain) UIView *tableFooterView;// 每行的高度@property (nonatomic) CGFloat rowHeight;// 組頭部高度(QQ好友名單)@property (nonatomic) CGFloat sectionHeaderHeight;// 組尾部高度@property (nonatomic) CGFloat sectionFooterHeight;

3.常見的方法

// 用於從緩衝池中取出可用的cell- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
二、UITableView展示資料
  • UITableView展示資料需要資料來源datasource
  • 遵守資料來源協議 UITableViewDataSource,並實現資料來源的方法

1.遵守資料來源協議

<UITableViewDataSource>
self.tableView.dataSource = self;

2.實現資料來源方法

// 一共有多少組資料- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;// 每一組有多少行資料- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;// 每一行顯示什麼內容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;// 每行的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath// 選中某一行的資料- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath// 組頭部標題- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section// 組尾部標題- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section// 右值索引列表- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;// 組頭部視圖- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;// 組尾部視圖- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;// 組頭高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;// 組尾部高度- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
三、UITableViewCell的建立

1.cell的常見屬性

// 表徵圖@property (nonatomic, readonly, retain) UIImageView *imageView;// 標題@property (nonatomic, readonly, retain) UILabel     *textLabel;// 詳細標題@property (nonatomic, readonly, retain) UILabel     *detailTextLabel;// 內容視圖,是包含以上3個視圖的@property (nonatomic, readonly, retain) UIView      *contentView;// 右邊的入口視圖@property (nonatomic, retain) UIView                *accessoryView;

2.建立cell的方法
1>

// cell是通過手寫代碼建立的,才會調用這個方法來初始化cell[[UITableViewCell alloc] initWithStyle:<#(UITableViewCellStyle)#> reuseIdentifier:<#(NSString *)#>];

UITableviewCellStyle屬性決定使用contentView的哪些子視圖,以及這些子視圖的位置

UITableViewCellStyleDefault // 標題+映像UITableviewCellStyleSubtitle // 標題+明細+映像UITableViewCellStyleValue1   // 標題+明細+映像UITableviewCellStyleValue2   // 標題+明細reuseIdentifier:用於cell迴圈利用的標示ID

2>

// cell是通過storyboard或則xib實現的,就會調用這個方法來初始化cell- (void)awakeFromNib{}

3>

// 在這方法中設定子控制項的frame- (void)layoutSubViews{}

3.Cell重用的原理

1> 當滾動列表時,部分UITableViewCell會移出視窗,UITableView會將視窗外的UITableViewCell放入一個對象池中,等待重用。當UITableView要求dataSource返回UITableViewCell時,dataSource會先查看這個對象池,如果池中有未使用的UITableViewCell,dataSource會用新的資料配置這個UITableViewCell,然後返回給UITableView,重新顯示到視窗中,從而避免建立新對象

2> 還有一個非常重要的問題:有時候需要自訂UITableViewCell(用一個子類繼承UITableViewCell),而且每一行用的不一定是同一種UITableViewCell,所以一個UITableView可能擁有不同類型的UITableViewCell,對象池中也會有很多不同類型的UITableViewCell,那麼UITableView在重用UITableViewCell時可能會得到錯誤類型的UITableViewCell

解決方案:
UITableViewCell有個NSString *reuseIdentifier屬性,可以在初始化UITableViewCell的時候傳入一個特定的字串標識來設定reuseIdentifier(一般用UITableViewCell的類名)。當UITableView要求dataSource返回UITableViewCell時,先通過一個字串標識到對象池中尋找對應類型的UITableViewCell對象,如果有,就重用,如果沒有,就傳入這個字串標識來初始化一個UITableViewCell對象

4.Cell的重用代碼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 1.定義一個cell的標識    static NSString *ID = @"mjcell";    // 2.從緩衝池中取出cell    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    // 3.如果緩衝池中沒有cell    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];    }    // 4.設定cell的屬性...    return cell;}
四、cell的自訂實現

1.xib實現UITableViewCell (cell的高度一致)

1> 建立一個xib檔案描述一個View的內部結構(假設叫WBTgCell.xib)2> 建立一個自訂的類,類名和xib檔案名稱保持一致 WBTgCell3> 將xib中的控制項和自訂的.m檔案進行連線,並指定可重用的標示符4> 提供一個類方法返回一個建立好的自訂的view(屏蔽xib的載入過程)5> 提供一個模型屬性接受外界傳來的模型資料6> 重寫模型屬性的setter方法,在setter方法中將模型資料展示到對應的子控制項上

2.通過代碼自訂cell(cell的高度不一致)

1> 建立一個繼承自UITableviewCell的類2> 重寫initWithStyle:reuseIdentifier:方法    *添加所有需要顯示的子控制項(不需要設定子控制項的資料和frame,子控制項要添加到contentView中)    *進行子控制項一次性的屬性設定(有些屬性只需要設定一次,比如字型、固定的圖片)3> 提供2個模型    *資料模型:存放文字資料\圖片資料    *frame模型:存放資料模型\所有子控制項的frame\cell的高度4> cell擁有一個frame模型(不要直接擁有資料模型)5> 重寫frame模型屬性的setter方法:在這個方法中設定子控制項的顯示資料和frame6> frame模型資料的初始化已經採取懶載入的方法(每個cell對應的frame模型資料只載入1次)
五、靜態儲存格

1> 通過沒storyboard設定tableview的content屬性,static是靜態儲存格,dynamic是動態添加的
2> 靜態儲存格適用於tableview裡面的cell內容和現實都是固定的,後續不需要更改cell內部的內容
3> 如果tableview中顯示的內容需要改變的就不能用靜態儲存格

六、動態儲存格

應用程式管理

1>cell重用容易出現從緩衝池中取出來的cell 可能是之前更改過狀態的cell,所以在重用的時候必須覆蓋所有cell中子控制項的狀態。2>當cell從storyboard中tableviewcontroller中唯一標示中取出來的時候,控制器可以不做初始化操作,if(cell == nil){};因為storyboard中確實已經存在了cell了3>storyBoard中自訂cell的時候,可以建立多個cell的模型,然後根據唯一標示可以取出相應的cell4>按鈕能不能用取決於當前cell有沒有下載

補充:
1、字串的尺寸

// NSString字串中對象方法:- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize{    NSDictionary *attrs = @{NSFontAttributeName : font};    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;}

2、圖片的隨意不變形(直接Xcode中設定圖片就行)

+ (UIImage *)resizableImage:(NSString *)name{    UIImage *normal = [UIImage imageNamed:name];    CGFloat w = normal.size.width * 0.5;    CGFloat h = normal.size.height * 0.5;    return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w)];}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

4、iOS 開發之 UITableView

聯繫我們

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