1. UITableView的作用
以垂直滾動方式顯示數(shù)據(jù)列表。
UITableView 的兩種內(nèi)置樣式:UITableViewStylePlain 和UITableViewStyleGrouped 。
tableView只是一個(gè)愚蠢的容器,只負(fù)責(zé)顯示。顯示的是什么自己完全不知道。
數(shù)據(jù)都是由dataSource提供。
要滿足快速滾動,性能很重要
2. UITableView的常用屬性
2.1 分割線屬性
屬性名稱
作用
separatorStyle
分隔線樣式
separatorColor
分隔線顏色
2.2 選中屬性
屬性名稱
作用
allowsSelection
允許選中
allowsMultipleSelection
允許多選
2.3 行數(shù)
屬性名稱
作用
indexPathsForSelectedRows
當(dāng)前選中行數(shù)
indexPathsForVisibleRows
當(dāng)前可見行數(shù)
2.4 背景
屬性名稱
作用
backgroundView
背景視圖
selectedBackgroundView
選中時(shí)的背景視圖
2.5 UITableViewCell的selectionStyle屬性可設(shè)置被選中時(shí)的背景顏色
屬性名稱
作用
UITableViewCellSelectionStyleNone
沒有顏色
UITableViewCellSelectionStyleBlue
藍(lán)色(默認(rèn))
UITableViewCellSelectionStyleGray
灰色
3. tableView展示數(shù)據(jù)三部曲
遵守?cái)?shù)據(jù)源協(xié)議;
設(shè)置數(shù)據(jù)源
實(shí)現(xiàn)相應(yīng)數(shù)據(jù)源方法
cell的默認(rèn)高度是44,寬度和tableView等寬。
3.1 遵守?cái)?shù)據(jù)源
@interface ViewController ()<UITableViewDataSource>
3.2 設(shè)置數(shù)據(jù)源
self.tableView.dataSource = self;
3.3 實(shí)現(xiàn)數(shù)據(jù)源方法
總共多少組
每組多少行
每組中每行的內(nèi)容
//返回有多少組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
//返回有多少行,section 組的索引
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
//返回每一組的每一行顯示什么內(nèi)容
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//創(chuàng)建UITableViewCell
UITableViewCell *tableViewCell = [[UITableViewCell alloc]init];
return tableViewCell;
}
4. UITableViewStyleGrouped樣式
4.1 使用storyboard設(shè)置
4.2 使用代碼設(shè)置
UITableViewStyle的style的屬性是一個(gè)只讀屬性,所以修改不了。
默認(rèn)創(chuàng)建出來就是UITableViewStylePlain。
但是可以在初始化的時(shí)候直接定義。
UITableView *haha = [UITableView alloc]initWithFrame:<#(CGRect)#> style:(UITableViewStyle)];
5. UITableViewCell
5.1 四種默認(rèn)樣式
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
UITableViewCellStyleValue1, // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
UITableViewCellStyleValue2, // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
UITableViewCellStyleSubtitle // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
}; // available in iPhone OS 3.0
滿足不了需求,就需要自定義樣式。
最后編輯于 :2017.12.04 08:03:57
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者 平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。