PYTableMainViewgit
因爲開發中常常須要修改tableView的組(cell)的順序,或者控制組(cell)的顯示與隱藏,github
尷尬的是判斷順序的邏輯每次都至少要在如下地方修改objective-c
/// 獲得 cell、header、footer - (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; - (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; /// cell、header、footer 計算高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; /// cell 點擊事件 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 複製代碼
代碼及其冗餘,並且不聚合。很差維護緩存
因而封裝了
PYTableMainView
ide
PYTableMainView
位於PYKit
庫中的PYBaseView
庫spa
你能夠這樣引入 pod PYBaseView
代理
demo在這裏code
高聚合代碼。component
拋棄了組的限制,用類型或者key
來區分cell
、header
、footer
cdn
懶加載形式自動緩存每一個cell
、header
、footer
的frame
若是你發現緩存的frame
不許確:
沒有調用PYTableMainView
的reloadData
。你能夠調用- (void) reloadIndexPathFrameCatch
強制刷新緩存。
設置了tableView
的 預估行高屬性或實現了相應的代理,請設置成0
estimatedSectionHeaderHeight
estimatedSectionFooterHeight
estimatedRowHeight
動態註冊 header
、cell
、footer
SBaseTabelViewData
header
、cell
、footer
的基本屬性:高度、類型、區分的key
(是否爲IXB)SBaseTabelViewData
,而且調用相應的自定義代理。header
、cell
、footer
類型/identifire
來進行處理。struct SBaseTabelViewData {
/// 擴展key
NSString * key;
NSInteger sectionCount;
NSInteger rowCount;
Class rowType;
Class headerType;
Class footerType;
NSString *rowIdentifier;
NSString *headerIdentifier;
NSString *footerIdentifier;
CGFloat rowHeight;
CGFloat rowWidth;
CGFloat headerHeight;
CGFloat headerWidth;
CGFloat footerHeight;
CGFloat footerWidth;
BOOL isXibCell;
BOOL isXibFooter;
BOOL isXibHeader;
/// 若是 (isXibCell && length<= 0) 那麼cellNibName = NSStringFromClass(rowType)
NSString *cellNibName;
};
typedef struct SBaseTabelViewData SBaseTabelViewData;
複製代碼
你能夠實現下面的方法,來建立一個對應IndexPath
的SBaseTabelViewData
。
- (SBaseTabelViewData) getTableViewData:(PYTableMainView *)baseTableView andCurrentSection:(NSInteger)section andCurrentRow:(NSInteger)row
複製代碼
全部的關於組的判斷都在這個方法中統一處理。
- (SBaseTabelViewData) getTableViewData:(PYTableMainView *)baseTableView andCurrentSection:(NSInteger)section andCurrentRow:(NSInteger)row {
SBaseTabelViewData data = SBaseTabelViewDataMakeDefault();
data.sectionCount = 6;
if (section == 0) {
data.rowCount = self.data1.count;
data.rowHeight = 60;
data.rowType = BaseTableTestCell1.class;
data.rowIdentifier = KBaseTableTestCell1;
}
if (section == 1) {
data.rowCount = self.data2.count;
data.rowHeight = 30;
data.rowType = BaseTableTestCell2.class;
data.rowIdentifier = KBaseTableTestCell2;
data.headerHeight = 40;
data.headerIdentifier = KBasetableTestHeserFooterView1;
data.headerType = BasetableTestHeserFooterView1.class;
}
if (section == 2) {
data.rowCount = self.data3.count;
data.rowHeight = 112;
data.rowType = BaseTableTestCell3.class;
data.rowIdentifier = KBaseTableTestCell3;
data.headerHeight = 50;
data.headerIdentifier = KBasetableTestHeserFooterView2;
data.headerType = BasetableTestHeserFooterView2.class;
data.isXibCell = true;
data.key = KBaseTableTestCell3_data3;///根據key區分是第二組數據
}
if (section == 3) {
data.rowCount = self.data4.count;
data.rowHeight = 120;
data.rowType = BaseTableTestCell3.class;
data.rowIdentifier = KBaseTableTestCell3;
data.headerHeight = 60;
data.headerIdentifier = KBasetableTestHeserFooterView2;
data.headerType = BasetableTestHeserFooterView2.class;
data.isXibCell = true;
data.key = KBaseTableTestCell3_data4;///根據key區分是第三組數據
}
return data;
}
複製代碼
cell數據的傳遞
- (void)baseTableView:(PYTableMainView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath andData:(SBaseTabelViewData)data{
if ([BaseTableTestCell1.class isEqual: cell.class]) {
}
if ([BaseTableTestCell2.class isEqual: cell.class]) {
BaseTableTestCell2 *cell2 = (BaseTableTestCell2 *)cell;
cell2.titleLabel.text = self.data2[indexPath.row];
cell2.subTitleLabel.text = self.data2[indexPath.row];
__weak typeof(self)weakSelf = self;
[cell2 setClickCallBack:^{
[weakSelf.tableView reloadData];
}];
}
if ([BaseTableTestCell3.class isEqual: cell.class]) {
BaseTableTestCell3 *cell3 = (BaseTableTestCell3 *)cell;
NSString *str = @"";
if ([KBaseTableTestCell3_data3 isEqualToString:data.key]) {
str = self.data3[indexPath.row];
}
if ([KBaseTableTestCell3_data4 isEqualToString:data.key]) {
str = self.data4[indexPath.row];
}
NSArray <NSString *>*array = [str componentsSeparatedByString:@"<EB>"];
cell3.nameLabel.text = array.firstObject;
cell3.subTitleLabel.text = array.lastObject;
cell3.delegate = self;
[cell3 addGestureRecognizer: cell3.longPressGesture];
}
}
- (void)tableView:(PYTableMainView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section andData: (SBaseTabelViewData)data {
if ([BasetableTestHeserFooterView1.class isEqual:view.class]) {
BasetableTestHeserFooterView1 *header = (BasetableTestHeserFooterView1 *)view;
header.titleLabel.text = @"設置";
}
/// 第二種headerView
if ([BasetableTestHeserFooterView2.class isEqual:view.class]) {
BasetableTestHeserFooterView2 *header = (BasetableTestHeserFooterView2 *)view;
/// 用key區分header的類型
if ([data.key isEqualToString:KBaseTableTestCell3_data3]) {
header.titleLabel.text = @"小的綠蘿";
header.rightPointView.backgroundColor = UIColor.redColor;
}
if ([data.key isEqualToString:KBaseTableTestCell3_data4]) {
header.titleLabel.text = @"大的鵝鵝鵝掌柴";
header.rightPointView.backgroundColor = UIColor.blueColor;
}
}
}
複製代碼
PYTableMainView
位於PYKit
庫中的PYBaseView
庫
你能夠這樣引入 pod PYBaseView