高度封装tableView

   可以解决一种一组,一种多组,多种多组,多种一组的tableView

public enum  LSTableViewCellType
{
    case oneType_oneGroup            //一种  一组    WPBaseCell.Self                           [data]
    case oneType_multipleGroup       //一种  多组    [WPBaseCell.Self]                         [data]   //  [[data1,data2],[data1,data2]]
    case multipleType_oneGroup       //多种  一组    [WPBaseCell.Self,WPBaseCell2.Self]        [data1,data2]   cell.cout = data.cout
    case multipleType_multipleGroup  //多种  多组    [[WPBaseCell1.Self,WPBaseCell2.Self],[]]  [[data1,data2],[data1,data2]]
}

extension UIViewController
{
    public  func cellBlock(_ cell:UITableViewCell,data:AnyObject,index:NSInteger)
    {
    
    }
}

extension UITableViewCell
{
    /**
     
     Cell刷新数据
     - parameter data: 数据源
     */
    public func cellRefreshData(_ data:(AnyObject!))
    {
    
    }
    
    /**
     计算Cell的高度
     - parameter data: 数据源
     - returns: 高度
     */
    public  class func heightForCell(_ data:(AnyObject!)) -> CGFloat
    {
        return 44;
    }
  
}

class LSTableViewDataKit: NSObject,UITableViewDataSource {
    var tableView:UITableView!
    var data:Array<AnyObject>!
    var cellIDs:AnyObject!
    var cellType:LSTableViewCellType = .oneType_oneGroup
    
    weak var viewController:UIViewController!
    /**
     初始化
     
     - parameter data:      数据源     一组  多组
     - parameter cellID:    cellID    一种  多种(一组多种,多组多种)
     - parameter tableView: tableView
     
     - returns:
     */
    init(data:Array<AnyObject> = [AnyObject](),cellID:AnyObject,tableView:UITableView = UITableView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight), style: .grouped),viewController:UIViewController) {
       super.init()
       self.data = data
       self.tableView = tableView
       self.cellIDs = cellID
       self.viewController = viewController
       self.tableView.backgroundColor = COLOR_BG
       //注册Cell
       self.registerCell()
       tableView.dataSource = self
    }
    
 
  
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //分组
        switch self.cellType {
        case .oneType_oneGroup:
            return  self.data.count
        case .oneType_multipleGroup:
            if let item = self.data[section] as? [AnyObject] {
                return item.count
            }else
            {
              return 1
            }
        case .multipleType_oneGroup:
            return  self.data.count
        case .multipleType_multipleGroup:
           if let item  = self.data[section] as? [AnyObject]
           {
              return item.count
           }
        }
        return 1
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        if self.cellType == .oneType_oneGroup || self.cellType == .multipleType_oneGroup {
         return 1
        }else
        {
           return self.data.count
        }
   
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cellType  =   self.cellWithIdentifier(indexPath).0
        let cell =    tableView.dequeueReusableCell(withIdentifier: cellType, for:indexPath)
        let itemData = self.cellWithIdentifier(indexPath).1
      
        
        if (self.viewController.responds(to: #selector(UIViewController.cellBlock(_:data:index:))))
        {
          if let itemCell = cell as? WPBaseCell
          {
             itemCell.delegateVC = viewController
            switch self.cellType {
            case .oneType_oneGroup:
                 itemCell.baselineView.isHidden  = self.data.count - 1 == indexPath.row
            case .oneType_multipleGroup:
                if let item = self.data[indexPath.section] as? [AnyObject] {
                    itemCell.baselineView.isHidden = item.count - 1 == indexPath.row
                }else
                {
                    itemCell.baselineView.isHidden = self.data.count - 1 == indexPath.section
                    
                }
            case .multipleType_oneGroup:
                itemCell.baselineView.isHidden  =  self.data.count - 1 == indexPath.row
            case .multipleType_multipleGroup:
                if let item  = self.data[indexPath.section] as? [AnyObject]
                {
                    itemCell.baselineView.isHidden =  item.count - 1 == indexPath.row
                }
            }
          }
        }
        
        //刷新数据
        cell.cellRefreshData(itemData)
        
        return  cell
    }
    
    
    //MARK: - 获取Cell和数据源
    func cellWithIdentifier(_ indexPath: IndexPath) -> (String,AnyObject)
    {
        var aa:(cell:String,data:AnyObject) = ("","" as AnyObject)
        switch self.cellType {
        case .oneType_oneGroup:       //一组一种
            if let cellID = cellIDs as? AnyClass{
                aa.cell = String(describing: cellID)
            }
            aa.data = self.data[indexPath.row]
            
        case .oneType_multipleGroup:   //多组一种
            if let cellID = cellIDs as? [AnyClass]{
                aa.cell = String(describing: cellID[0])
            }
            //2种数据源
            if let item = self.data[indexPath.section] as? [AnyObject] {
                aa.data = item[indexPath.row]
            }else{
                aa.data = self.data[indexPath.section]
            }
            
        case .multipleType_oneGroup:   //一组多种
            if let cellID = cellIDs as? [AnyClass]{
                if self.data.count ==  cellID.count{
                    aa.cell = String(describing: cellID[indexPath.row])
                }else
                {
                    aa.cell = String(describing: cellID[0])
                }
                aa.data = self.data[indexPath.row]
            }
        case .multipleType_multipleGroup:   //多组多种
            if let itemData = self.data[indexPath.section] as? [AnyObject] {
                aa.data = itemData[indexPath.row]
            }
            if let cellID = cellIDs as? [[AnyClass]]{
                aa.cell = String(describing: cellID[indexPath.section][indexPath.row])
            }
        }
        return aa
    }

    //MARK: - 注册Cell
    func  registerCell()
    {
         if let cellID = cellIDs as? AnyClass
         {  //一种
            tableView.register(cellID, forCellReuseIdentifier:String(describing: cellID))
            self.cellType = .oneType_oneGroup
            
         }else if let cellID = cellIDs as? [AnyClass]
         {
             //多种一组   一种多组
             for  item in cellID
             {
                tableView.register(item, forCellReuseIdentifier: String(describing: item))
             }
             if cellID.count == 1{
                self.cellType = .oneType_multipleGroup
             }else{
                self.cellType = .multipleType_oneGroup
             }
        }else if let cellID = cellIDs as? [Array<AnyClass>]
         {
            //多种多组
            for  index1 in  cellID
            {
                 for index2 in index1
                 {
                   tableView.register(index2, forCellReuseIdentifier: String(describing: index2))
                 }
            }
            self.cellType = .multipleType_multipleGroup
         }else
         {
             debugPrint("注册异常,参数CellID错误")
         }
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#import "LHDBaseTableViewCell.h" @interface LHDBaseTableView : UITableView <UITableViewDataSource,UITableViewDelegate> @property (nonatomic, assign) CGFloat cellHeight; @property (nonatomic, assign) BOOL fixed; //是否固定高度 @property (nonatomic, copy) NSInteger(^tableViewNumberOfRowInSection)(UITableView *,NSInteger); @property (nonatomic, copy) UITableViewCell *(^tableViewCellForRowAtIndexPath)(UITableView *, NSIndexPath *); @property (nonatomic, copy) NSInteger(^numberOfSectionInTabelView)(UITableView *); @property (nonatomic, copy) CGFloat(^tableViewHeightForRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) BOOL(^tableViewCanEditRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) void(^tableViewCommitEditingStyleforRowAtIndexPath)(UITableView *,UITableViewCellEditingStyle,NSIndexPath *); @property (nonatomic, copy) UITableViewCellEditingStyle (^tableViewEditingStyleForRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) void(^tableViewDidSelectRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) void(^tableViewDidDeselectRowAtIndexPath)(UITableView *,NSIndexPath *); @property (nonatomic, copy) UIView *(^tableViewViewForHeaderInSection)(UITableView *,NSInteger); @property (nonatomic, copy) CGFloat(^tableViewHeightForHeaderInSection)(NSInteger); @property (nonatomic, strong) LHDBaseTableViewCell *myTableViewCell; @property (nonatomic, strong) NSArray *dataArray; - (void)resetDelegate;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值