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