1、tableView層次 結構
Snip20151026_21.png
2、cell
-
cell結構
Snip20151026_22.png - contentView下默認有3個子視圖
- 2個是UILabel(textLabel、detailTextLabel)
- 1個UIImageView(imageView)
- UITableViewCellStyle屬性
-
用于決定使用contentView的哪些子視圖,以及這些子視圖在contentView中的位置
cellStyle.png
-
- cell重用原理
當滾動列表時,部分UITableViewCell會移出窗口,
UITableView會將窗口外的UITableViewCell放入一個對象池中,等待重用。
當UITableView要求dataSource返回UITableViewCell時,
dataSource會先查看這個對象池,
如果池中有未使用的UITableViewCell,
dataSource會用新的數據配置這個UITableViewCell,
然后返回給UITableView,
重新顯示到窗口中,從而避免創建新對象
- 不同類型的Cell重用
- 解決方案:指定不同類型對應的重用標識來區分
UITableViewCell有個NSString *reuseIdentifier屬性,
可以在初始化UITableViewCell的時候傳入一個特定的字符串標識來設置reuseIdentifier(一般用UITableViewCell的類名)。
當UITableView要求dataSource返回UITableViewCell時,
先通過一個字符串標識到對象池中查找對應類型的UITableViewCell對象,
如果有,就重用,
如果沒有,就傳入這個字符串標識來初始化一個UITableViewCell對象
+ 注冊Cell
```objc
[self.tableView registerClass:[TagCell class] forCellReuseIdentifier:@"tgID"];
- 快速常見Cell : xib / storyboard
- (instancetype)initWithTableView:(UITableView *)tableView{
static NSString *ideitifier = @"tgCell";
TgCell *cell = [tableViewdequeueReusableCellWithIdentifier:ideitifier];
if (!cell) {
// 1. xib 緩存池中沒有通過 loadNibNamed...方法加載重寫創建
// cell = [[[NSBundle mainBundle] loadNibNamed:@"TgCell" owner:nil options:nil] lastObject];
// 2. storyboard 緩存池中沒有通過 initWithStyle...方法加載重寫創建
cell = [[TgCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ideitifier];
}
return cell;
}
- iOS8 , 自動計算Cell高度
// 告訴tableView的真實高度是自動計算的,根據你的約束來計算
self.tableView.rowHeight = UITableViewAutomaticDimension;
// 告訴tableView所有cell的估計行高
self.tableView.estimatedRowHeight = 44
// 返回估算告訴,作用:在tablView顯示時候,先根據估算高度得到整個tablView高,而不必知道每個cell的高度,從而達到高度方法的懶加載調用
3、常見屬性
// UITableView的兩種樣式
UITableViewStylePlain / UITableViewStyleGrouped
self.tableView.backgroundColor = [UIColor purpleColor];
// 設置索引條內部文字顏色
self.tableView.sectionIndexColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
// 設置索引條背景顏色
self.tableView.sectionIndexBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
//1. 修改tableView的行高
self.tableView.rowHeight = 100;
// 2.組頭組尾的高
self.tableView.sectionHeaderHeight = 55;
self.tableView.sectionFooterHeight = 22;
// 3.設置整個tablView的頭部/尾部視圖
self.tableView.tableHeaderView = [[UISwitch alloc] init];
self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeInfoDark];
// 4.設置我們分割線顏色(clearColor相當于取消系統分割線)
self.tableView.separatorColor = [UIColor clearColor];
// 5.設置分割線樣式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
4、方法
#pragma mark - 數據源方法
// 返回行數
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
// 設置cell
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
}
#pragma mark - 代理方法
/**
* 設置行高
*/
- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
return 100;
}
// 添加每組的組頭
- (UIView *)tableView:(nonnull UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
}
// 返回每組的組尾
- (UIView *)tableView:(nonnull UITableView *)tableView viewForFooterInSection:(NSInteger)section{
}
// 選中某行cell時會調用
- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
NSLog(@"選中didSelectRowAtIndexPath row = %ld", indexPath.row);
}
// 取消選中某行cell會調用 (當我選中第0行的時候,如果現在要改為選中第1行 - 》會先取消選中第0行,然后調用選中第1行的操作)
- (void)tableView:(nonnull UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
NSLog(@"取消選中 didDeselectRowAtIndexPath row = %ld ", indexPath.row);
}
// 設置UITableView的索引條,返回數組字符串集
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;
5、功能實現
5.1 代碼自定義不等高cell
1.新建一個繼承自UITableViewCell的類
2.重寫initWithStyle:reuseIdentifier:方法
添加所有需要顯示的子控件(不需要設置子控件的數據和frame, 子控件要添加到contentView中)
進行子控件一次性的屬性設置(有些屬性只需要設置一次, 比如字體\固定的圖片)
3.提供2個模型
數據模型: 存放文字數據\圖片數據
frame模型: 存放數據模型\所有子控件的frame\cell的高度
4.cell擁有一個frame模型(不要直接擁有數據模型)
5.重寫frame模型屬性的setter方法: 在這個方法中設置子控件的顯示數據和frame
6.frame模型數據的初始化已經采取懶加載的方式(每一個cell對應的frame模型數據只加載一次)
5.2 批量刪除、全局刷新、局部刷新
- 1、 全局刷新: [self.tableView reloadData];
- 2、 修改局部刷新, reloadRowsAtIndexPaths
- 修改Cell的模型數據可以使用reloadRowsAtIndexPaths...進行局部刷新
- 該方法使用的前提是模型數據的個數不變,因為修改并未修改模型數據的個數,所以可以使用該方法進行局部刷新
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
+ 刪除局部刷新,刪除調用 deleteRowsAtIndexPaths...方法
- 注意:這里注意,和添加操作一樣我們不能使用UITableView提供的reloadRowsAtIndexPaths...方法 -> 使用的前提是模型數據數量不變
```objc
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle];
左滑功能
Snip20151026_23.png
- 1、 讓UITableView進入編輯狀態,會出現左滑效果
// self.tableView.editing = YES;
// self.tableView.editing = !self.tableView.editing;
[self.tableView setEditing:!self.tableView.editing animated:YES];
- 2、 代理方法處理左滑效果時編輯事件處理
/**
* 只要實現了這個方法,左滑出現刪除按鈕的功能就有了
* 點擊了左滑出現的“刪除”按鈕就會調用
typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
};
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
- 3、返回左滑右邊執行刪除操作按鈕文字。
- (NSString *)tableView:(nonnull UITableView *)tableViewtitleForDeleteConfirmationButtonForRowAtIndexPath:(nonnull NSIndexPath*)indexPath;
- 4、 設置左滑顯示對應按鈕及其處理事件
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView*)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(8_0);
5.3 tableView的內容能穿透導航欄與TabBar,而且內容都不被它們擋住
Snip20150914_48.png
Snip20150914_35.png
5.4 如何實現水平滾動的UITableView
Snip20150914_26.png