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

隱藏多余的 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)擊無樣式
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就可以。

  1. 自定義視圖,繼承自UITableViewHeaderFooterView
  2. 設(shè)置headerView 的行高:
self.orderTableView.sectionHeaderHeight = 42;
  1. 注冊(cè) headerView:
[self.tablView registerNib:[UINib nibWithNibName:NSStringFromClass([CustomHeaderView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:@"header"];
  1. 實(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 處理起來比較繁瑣
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ū)域來了");
}

// 注意: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來刷新表格時(shí),有時(shí)會(huì)不執(zhí)行代理方法。

如果設(shè)置-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView代理方法,并且返回值是根據(jù)請(qǐng)求結(jié)果來設(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/


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 一、簡(jiǎn)介 <<UITableView(或簡(jiǎn)單地說,表視圖)的一個(gè)實(shí)例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無邪8閱讀 10,706評(píng)論 3 3
  • 版權(quán)聲明:未經(jīng)本人允許,禁止轉(zhuǎn)載. 1. TableView初始化 1.UITableView有兩種風(fēng)格:UITa...
    蕭雪痕閱讀 2,919評(píng)論 2 10
  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明AGI閱讀 16,018評(píng)論 3 119
  • 題記:我人生中犯下的最大錯(cuò)誤都起源于把我的力量拱手送人——相信別人給予的愛比我給自己的愛更重要。 ——《我堅(jiān)信》 ...
    傾城你好閱讀 2,576評(píng)論 0 3
  • 周圍的小伙伴兒好像都挺忙的,忙著找工作,找對(duì)象,找賺錢的門路。 物質(zhì)資料日漸豐富的當(dāng)下,我們對(duì)金錢利益的追...
    暮鼓晨鐘安之若素閱讀 395評(píng)論 0 0