隱藏多余的 cell
self.tableView.tableFooterView = [[UIView alloc] init];
分割線相關(guān)
// 去掉整個(gè) tableView 的分割線:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 去掉某一個(gè)行 cell 的分割線:
cell.separatorInset = UIEdgeInsetsMake(0, ScreenWidth, 0, 0);
// 分割線頂?shù)筋^部
self.tableView.separatorInset = UIEdgeInsetsZero;
self.tableView.layoutMargins = UIEdgeInsetsZero;
// 分割線顏色
self.tableView.separatorColor = [UIColor redColor];
隱藏滾動(dòng)條
// 垂直方向
self.tableView.showsVerticalScrollIndicator = NO;
// 豎直方向
self.tableView.showsHorizontalScrollIndicator = NO;
cell點(diǎn)擊效果
// 點(diǎn)擊無(wú)樣式
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// 自定義點(diǎn)擊樣式 - view
cell.selectedBackgroundView = [[UIView alloc] init];
cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor];
// 自定義點(diǎn)擊樣式 - image
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xxx"]];
// 右邊輔助按鈕樣式
cell.accessoryType = UITableViewCellAccessoryDetailButton;
// 類似 button 點(diǎn)擊閃爍效果
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
TableView 自動(dòng)布局
// TableviewCell 使用SB約束好, 根據(jù)大小自動(dòng)布局
// 使用SB布局的Cell ,直接使用下面代碼達(dá)到自動(dòng)布局目的
self.tableView.estimatedRowHeight = 44;
self.tableView.rowHeight = UITableViewAutomaticDimension;
自定義分區(qū)頭
注意:自定義分區(qū)頭,tableView 的樣式使用Plain
就可以。
- 自定義視圖,繼承自
UITableViewHeaderFooterView
- 設(shè)置headerView 的行高:
self.orderTableView.sectionHeaderHeight = 42;
- 注冊(cè) headerView:
[self.tablView registerNib:[UINib nibWithNibName:NSStringFromClass([CustomHeaderView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:@"header"];
- 實(shí)現(xiàn)代理方法:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CustomHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
headerView.titleL.text = self.orders[section][@"date"];
return headerView;
}
獲取 TableView 的可視區(qū)域
// 方式一
// 直接返回一個(gè) UITableViewCell 的數(shù)組,對(duì)于自定義 cell 處理起來(lái)比較繁瑣
self.tableView.visibleCells;
// 方式二
// 返回一個(gè) NSIndexPath 的數(shù)組,可以使用 indexPath.row 去獲取數(shù)據(jù)、獲取 cell
self.tableView.indexPathsForVisibleRows;
// 方式三
// 改方法可使用在代理回調(diào)比較多的設(shè)計(jì)中
NSIndexPath *index = [[NSIndexPath alloc] init];
CGRect cellR = [self.tableView rectForRowAtIndexPath:index];
if ((self.tableView.contentOffset.y - cellR.origin.y) < self.tableView.lc_height ||
(cellR.origin.y - self.tableView.contentOffset.y) > self.tableView.lc_height) {
NSLog(@"此時(shí)的 cell不在 tableview 的可視區(qū)域來(lái)了");
}
// 注意:1和2 在自動(dòng)根據(jù)數(shù)據(jù)伸長(zhǎng)的 cell 好像不太好用。
禁止分區(qū)頭跟隨 TableView 滾動(dòng)
// 滾動(dòng)視圖代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.tableView) {
CGFloat headerHeight = 42;
if ((scrollView.contentOffset.y <= headerHeight) && (scrollView.contentOffset.y >= 0)) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (scrollView.contentOffset.y >= headerHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-headerHeight, 0, 0, 0);
}
}
}
程序不執(zhí)行代理方法
當(dāng)網(wǎng)絡(luò)請(qǐng)求后,設(shè)置reloadData
來(lái)刷新表格時(shí),有時(shí)會(huì)不執(zhí)行代理方法。
如果設(shè)置-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
代理方法,并且返回值是根據(jù)請(qǐng)求結(jié)果來(lái)設(shè)置分區(qū)個(gè)數(shù)的話,數(shù)值有可能為0。
原因:
當(dāng)返回的分區(qū)頭個(gè)數(shù)0時(shí),tableView 的其他代理方法都不會(huì)執(zhí)行。
開發(fā)中要注意:分區(qū)個(gè)數(shù)為0的情況。
GitHub: https://github.com/LiCheng244/LCUtils
個(gè)人博客: http://www.licheng244.com/