標籤:name tag 還需要 nsstring 添加 end ios1 nil cti
UITableView用得較多,遇到的情況也較多,單獨記錄一篇。
一、零散的技巧
二、取cell
三、導覽列、TableView常見問題相關
一、零散的技巧
1、 cell的選中效果是cell的屬性,可以有的有,無的無。
// 自訂cellself.selectionStyle = UITableViewCellSelectionStyleNone;// 取cellcell.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為枚舉,或基礎資料型別 (Elementary Data 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相關