初始化
UITableView * tableView = [[UITableView alloc]initWithFrame:frame style:UITableViewStyleGrouped];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
常見(jiàn)屬性
// 索引條內(nèi)部文字顏色
self.tableView.sectionIndexColor
// 設(shè)置索引條背景顏色
self.tableView.sectionIndexBackgroundColor
// 行高
self.tableView.rowHeight = 100;
// 組頭組尾的高
self.tableView.sectionHeaderHeight
self.tableView.sectionFooterHeight
// 設(shè)置整個(gè)頭部/尾部視圖
self.tableView.tableHeaderView
self.tableView.tableFooterView
// 分割線顏色(clearColor相當(dāng)于取消系統(tǒng)分割線)
self.tableView.separatorColor;
// 設(shè)置分割線樣式
self.tableView.separatorStyle;
// 告訴tableView的真實(shí)高度是自動(dòng)計(jì)算的,根據(jù)你的約束來(lái)計(jì)算
self.tableView.rowHeight = UITableViewAutomaticDimension;
// 告訴tableView所有cell的估計(jì)行高
self.tableView.estimatedRowHeight = 44
// 返回估算告訴,作用:在tablView顯示時(shí)候,先根據(jù)估算高度得到整個(gè)tablView高,而不必知道每個(gè)cell的高度,從而達(dá)到高度方法的懶加載調(diào)用
常用數(shù)據(jù)源方法
// 返回行數(shù)
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// 設(shè)置cell
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
static NSString *CMainCell = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CMainCell];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CMainCell];
//cell = [[[UINib nibWithNibName:@"CellName" bundle:nil]instantiateWithOwner:self options:nil]lastObject];
//cell = [[[NSBundle mainBundle] loadNibNamed:@"CellName" owner:nil options:nil] lastObject];
}
return cell;
}
#pragma mark - 代理方法
//設(shè)置行高
- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
// 添加每組的組頭
- (UIView *)tableView:(nonnull UITableView *)tableView viewForHeaderInSection:(NSInteger)section
// 返回每組的組尾
- (UIView *)tableView:(nonnull UITableView *)tableView viewForFooterInSection:(NSInteger)section
// 選中某行cell時(shí)會(huì)調(diào)用
- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
// 取消選中某行cell會(huì)調(diào)用 (當(dāng)我選中第0行的時(shí)候扇救,如果現(xiàn)在要改為選中第1行 - 》會(huì)先取消選中第0行,然后調(diào)用選中第1行的操作)
- (void)tableView:(nonnull UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath;
// 設(shè)置UITableView的索引條杰刽,返回?cái)?shù)組字符串集
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者