1. 程式人生 > >iOS:UITableView相關

iOS:UITableView相關

lar 對齊 options bject 技巧 pear uitable col rec

UITableView用得較多,遇到的情況也較多,單獨記錄一篇。

一、零散的技巧

二、取cell

三、導航欄、TableView常見問題相關

一、零散的技巧

1、 cell的選中效果是cell的屬性,可以有的有,無的無。

// 自定義cell
self.selectionStyle = UITableViewCellSelectionStyleNone;
// 取cell
cell.selectionStyle = UITableViewCellSelectionStyleNone;

2、cell的下劃線是Table的屬性,全部有,或全部無。

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

3、cell下劃線左邊頂住屏幕左邊。

cell.preservesSuperviewLayoutMargins = NO;
cell.layoutMargins = UIEdgeInsetsZero;
cell.separatorInset = UIEdgeInsetsZero;

  後續補充:也可以隱藏掉系統的下劃線,自定義LineView,要多寬就多寬,且可以實現不同cell不同下劃線樣式。

4、cell的重用ID,可以用類名

NSStringFromClass([tableCell class])

二、取cell

1、cell初始化的一些區別

1)、TableViewCell

1-1)、沒註冊

沒註冊的(一開始會取不到):
cell  = 從隊列取
if(cell取不到)
{
	創建cell
	創建子視圖,加tag
}
cell從tag取子視圖,刷新 tag 或 屬性

1-2)、註冊

註冊的(100%取得到):
cell  = 從隊列取(有indexPath的方法)
刷新 tag 或 屬性

(
	系統取不到,會走自定義的initWithStyle:reuseIdentifier:
	if(cell創建成功)
	{
		創建子視圖,加tag
	}
)

2)、CollectionViewCell

2-1)、沒註冊

2-2)、註冊

註冊的(100%取得到):
cell  = 從隊列取(有indexPath的方法)
if(cell取得到)
{
	(判斷是否有子視圖)創建子視圖
}
刷新 tag 或 屬性


collectionViewCell 流程有點不同
	1、沒 TableViewCell 的 initWithStyle:reuseIdentifier:
	2、但 每次都能從隊列取到
	3、所以 需要判斷取到的cell是否有子視圖,不然會不斷創建

2、加載XIB

1)、從多個cell樣式的XIB加載。只有1個cell樣式,可直接lastObject加載。(先根據不同的ID取,取不到再加載。)

  1-1)、獲取XIB裏的所有對象

NSArray *cellArry = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MyTableCell class]) owner:self options:nil];

  1-2)、讀取對應的Cell樣式,此時的參數type為枚舉,或基本數據類型。

cell = [cellArry objectAtIndex:type];

2)、在 UIView + xxx 的類別文件裏,可以添加這個類。方便加載單種Cell樣式的XIB。

+ (instancetype)viewFromXib
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

三、導航欄、TableView常見問題相關

1、導航欄、TableView

//調整contentInset。
//NO:不調整,按設定的frame、contentInset的顯示
//YES:會調整contentInset.top的高,讓顯示的頂在導航欄下面,【有滑過半透明效果】
self.automaticallyAdjustsScrollViewInsets =NO;

//調整frame
//    UIRectEdgeNone   //會頂在導航欄下面【沒有滑過半透明效果】
//    UIRectEdgeTop    //對齊原點
//    UIRectEdgeLeft   //對齊左邊
//    UIRectEdgeBottom //對齊頂部
//    UIRectEdgeRight  //對齊右邊
//    UIRectEdgeAll    //對齊所有
self.edgesForExtendedLayout = UIRectEdgeNone;

//導航欄半透明
self.navigationController.navigationBar.translucent = YES;

//隱藏navigationBar(1、它推過的所有的VC共用1個Bar;2、用繼承View的hidden屬性,隱藏不了!)
self.navigationController.navigationBarHidden=YES;

  後續補充:iOS11後 automaticallyAdjustsScrollViewInsets 廢棄,不過還需要做版本判斷。

        詳見“2、iOS11”

2、iOS11(此處參考簡書 “iOS 11 安全區域適配總結”--sonialiu)

1)、TableView 默認開啟Cell高度估算,關掉。

[UITableView appearance].estimatedRowHeight = 0;
[UITableView appearance].estimatedSectionHeaderHeight = 0;
[UITableView appearance].estimatedSectionFooterHeight = 0;

2)、ScrollView新增安全區域。

  2-1)、如果之前讓TabelView頂住屏幕,然後設置頂部內邊距 = 20+44,剛好在導航欄下面的話,

        會被系統向下偏移64的 SafeAreaInsets,再加上自己設置的64,就出現下移64問題。

  2-2)、同理,沒導航欄的時候,也會下移20 -> 狀態欄的高度。

  2-3)、以前若設置 automaticallyAdjustsScrollViewInsets = YES 讓系統自動調整,不會有問題

解決方案:添加下面,相當於 automaticallyAdjustsScrollViewInsets = NO

#ifdef __IPHONE_11_0   
if ([tableView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
    [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
#endif

  2-4)、contentInsetAdjustmentBehavior 其他類型

UIScrollViewContentInsetAdjustmentScrollableAxes:  adjustedContentInset = ( 可滾動方向 ? safeAreaInset + contentInset : contentInset );

UIScrollViewContentInsetAdjustmentNever:       adjustedContentInset = contentInset;

UIScrollViewContentInsetAdjustmentAlways:       adjustedContentInset = safeAreaInset + contentInset;

UIScrollViewContentInsetAdjustmentAutomatic:    (controller裏automaticallyAdjustsScrollViewInsets = YES) && (controller被navigation包含) == Always,否則 == Axes

iOS:UITableView相關