PYTableMainView

Because the development of tableView often need to modify the order of the group (cell), or control group (cell) show and hide,

The awkwardness is that the logic of determining the order has to be modified in at least the following places every time

// get 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 calculate height - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; // cell click event - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;Copy the code

The code is extremely redundant and does not aggregate. Poor maintenance

So it wraps PYTableMainView

PYTableMainView is located in the PYBaseView library of the PYKit library

You can introduce pod PYBaseView like this

The demo here

The characteristics of

  1. High aggregation code.

  2. Discard group restrictions and use type or key to distinguish between cell, header, and footer

  3. Lazy loading automatically caches the frame of each cell, header, and footer

    1. If you find that the cached frame is inaccurate:

      1. ReloadData is not called to PYTableMainView. You can call the – (void) reloadIndexPathFrameCatch forced to refresh the cache.

      2. Set the estimated row height property of tableView or implement the corresponding proxy, please set to 0

        estimatedSectionHeaderHeight

        estimatedSectionFooterHeight

        estimatedRowHeight

  4. Dynamically register headers, cells, and footers

Train of thought

  1. Encapsulate a structureSBaseTabelViewData
    1. Which encapsulatesheader,cell,footerBasic attributes of: height, type, distinguishingkey(Whether it is IXB)
  2. Create groups where you need to determine themSBaseTabelViewDataAnd invoke the corresponding custom proxy.
  3. Is used in all custom proxy implementation methodsheader,cell,footerType /identifireTo process it.
Struct SBaseTabelViewData {// 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; // if (isXibCell && length<= 0) then cellNibName = NSStringFromClass(rowType) NSString *cellNibName; }; typedef struct SBaseTabelViewData SBaseTabelViewData;Copy the code

You can implement the following method to create an SBaseTabelViewData corresponding to IndexPath.

- (SBaseTabelViewData) getTableViewData:(PYTableMainView *)baseTableView andCurrentSection:(NSInteger)section andCurrentRow:(NSInteger)row
Copy the code

All decisions about groups are handled in this way.

example

- (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; } 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; } return data; }Copy the code

Transfer of cell data

- (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 = @" set "; } / / / the second headerView if ([BasetableTestHeserFooterView2. Class isEqual: the class]) {BasetableTestHeserFooterView2 *header = (BasetableTestHeserFooterView2 *)view; / / / the key to distinguish the type of the header if ([data. The key isEqualToString: KBaseTableTestCell3_data3]) {header. The titleLabel. Text = @ "small money plant"; header.rightPointView.backgroundColor = UIColor.redColor; } the if ([data. Key isEqualToString: KBaseTableTestCell3_data4]) {header. The titleLabel. Text = @ "big goose goose schefflera"; header.rightPointView.backgroundColor = UIColor.blueColor; }}}Copy the code

PYTableMainView is located in the PYBaseView library of the PYKit library

You can introduce pod PYBaseView like this

The demo here