UITableView

1.UITableView

在iOS中,要實現表格數據展示,最常用的做法就是使用UITableView
UITableView繼承自UIScrollView,因此支持垂直滾動,而且性能極佳
UITableView的兩種樣式

UITableViewStylePlain
UITableViewStyleGrouped

如何展示數據 <UITableViewDataSource>:

# 1>UITableView需要一個數據源(dataSource)來顯示數據
# 2>UITableView會向數據源查詢一共有多少行數據以及每一行顯示什么數據等
# 3>沒有設置數據源的UITableView只是個空殼
# 4>凡是遵守 <UITableViewDataSource> 協議的OC對象,都可以是UITableView的數據源

2.UITableView 數據源 <UITableViewDataSource>

1-<UITableViewDataSource>.png

tableView展示數據的過程

調用數據源的下面方法得知一共有多少組數據
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

調用數據源的下面方法得知每一組有多少行數據
- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section;

調用數據源的下面方法得知每一行顯示什么內容
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath;

3.UITableViewCell

UITableView的每一行都是一個UITableViewCell,通過dataSource的tableView:cellForRowAtIndexPath:方法來初始化每一行

UITableViewCell內部有個默認的子視圖:contentView,contentView是UITableViewCell所顯示內容的父視圖,可顯示一些輔助指示視圖

輔助指示視圖的作用是顯示一個表示動作的圖標,可以通過設置UITableViewCell的accessoryType來顯示,默認是

UITableViewCellAccessoryNone(不顯示輔助指示視圖),其他值如下:
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryCheckmark

還可以通過cell的accessoryView屬性來自定義輔助指示視圖(比如往右邊放一個開關)

UITableViewCell的contentView

contentView下默認有3個子視圖
其中2個是UILabel(通過UITableViewCell的textLabel和detailTextLabel屬性訪問)
第3個是UIImageView(通過UITableViewCell的imageView屬性訪問)

UITableViewCell還有一個UITableViewCellStyle屬性,用于決定使用contentView的哪些子視圖,以及這些子視圖在contentView中的位置

2-UITableViewCell.png
3-UITableViewCell結構.png

4.UITableViewCell的重用原理

iOS設備的內存有限,如果用UITableView顯示成千上萬條數據,就需要成千上萬個UITableViewCell對象的話,那將會耗盡iOS設備的內存。要解決該問題,需要重用UITableViewCell對象

重用原理:

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

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

解決方案:

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

Cell的重用代碼

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

5.UITableViewCell自定義 Cell的高度不一致

1.新建一個繼承自UITableViewCell的類
2.重寫initWithStyle:reuseIdentifier:方法
添加所有需要顯示的子控件(不需要設置子控件的數據和frame, 子控件要添加到contentView中)
進行子控件一次性的屬性設置(有些屬性只需要設置一次, 比如字體\固定的圖片)
3.提供2個模型
數據模型: 存放文字數據\圖片數據
frame模型: 存放數據模型\所有子控件的frame\cell的高度
4.cell擁有一個frame模型(不要直接擁有數據模型)
5.重寫frame模型屬性的setter方法: 在這個方法中設置子控件的顯示數據和frame
6.frame模型數據的初始化已經采取懶加載的方式(每一個cell對應的frame模型數據只加載一次)

4-自定義不同高度的UITableViewCell.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容