iOS開發(fā) - UITableView 使用細節(jié)

隱藏多余的 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就可以。

  1. 自定義視圖,繼承自UITableViewHeaderFooterView
  2. 設置headerView 的行高:
self.orderTableView.sectionHeaderHeight = 42;
  1. 注冊 headerView:
[self.tablView registerNib:[UINib nibWithNibName:NSStringFromClass([CustomHeaderView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:@"header"];
  1. 實現(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/


最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 一、簡介 <<UITableView(或簡單地說,表視圖)的一個實例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無邪8閱讀 10,958評論 3 3
  • 版權聲明:未經(jīng)本人允許,禁止轉載. 1. TableView初始化 1.UITableView有兩種風格:UITa...
    蕭雪痕閱讀 2,981評論 2 10
  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,171評論 3 119
  • 周圍的小伙伴兒好像都挺忙的,忙著找工作,找對象,找賺錢的門路。 物質(zhì)資料日漸豐富的當下,我們對金錢利益的追...
    暮鼓晨鐘安之若素閱讀 450評論 0 0
  • 2018年8月18日星期六 晴 那一年,我們家的小狗俏俏不幸車禍喪命,連續(xù)多日家里都是陰云籠罩,缺失了很多平日的快...
    笑雨珍惜每一天閱讀 458評論 0 0

友情鏈接更多精彩內(nèi)容