美文网首页
iOS两个TableView的复用

iOS两个TableView的复用

作者: c42b9af86268 | 来源:发表于2016-12-28 19:40 被阅读0次

    两个TableView的复用

    代码

    1. TableView的代理方法实现TableView的复用,同理,推两个TableView的复用。

    实现:

    .h文件

    #import

    @classHomeDropdown;

    @protocolHomeDropdownDataSource

    -(NSInteger)numberOfRowsInMainTable:(HomeDropdown*)homeDropdown;

    -(NSString*)homeDropdown:(HomeDropdown*)homeDropdown

    titleOfRowsInMainTable:(NSInteger)row;

    -(NSArray*)homeDropdown:(HomeDropdown*)homeDropdown

    subdataForRowInMainTable:(NSInteger)row;

    //@optional

    //可选方法

    //-(NSString

    *)titleOfRowsInMainTable:(UITableView *)mainTable;

    @end

    @interfaceHomeDropdown :UIView

    +(instancetype)dropdowm;

    @property(nonatomic,strong)NSArray*categories;

    @property(nonatomic,strong)id dataSource;

    @end

    .m文件

    #import"HomeDropdown.h"

    #import"CategoryModel.h"

    #import"HomeDropdownMainCell.h"

    #import"HomeDropdownSubCell.h"

    #import"MTRegion.h"

    @interfaceHomeDropdown()

    @property(weak,nonatomic)IBOutletUITableView*mainTable;

    @property(weak,nonatomic)IBOutletUITableView*allowTable;

    //@property

    (nonatomic,strong) CategoryModel *selectedCategory;

    @property(nonatomic,assign)NSIntegerselectedMainRow;

    @property(nonatomic,strong)NSArray*regionArray;

    @end

    @implementationHomeDropdown

    +(instancetype)dropdowm{

    return[[[NSBundlemainBundle]loadNibNamed:@"HomeDropdown"owner:niloptions:nil]firstObject];

    }

    -(void)awakeFromNib{

    //不需要跟随父空间的尺寸变化而伸缩

    self.autoresizingMask=UIViewAutoresizingNone;

    self.backgroundColor=[UIColorredColor];

    }

    -(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    if(tableView==self.mainTable) {

    NSLog(@"%ld",(long)[self.dataSourcenumberOfRowsInMainTable:self]);

    return[self.dataSourcenumberOfRowsInMainTable:self];

    }else{

    //return [self.dataSource homeDropdown:selfsubdataForRowInMainTable:self.selectedMainRow].count;

    returnself.regionArray.count;

    }

    }

    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    //NSLog(@"datasource %@",self.dataSource);

    UITableViewCell*cell=nil;

    if(tableView==self.mainTable) {

    //主表

    cell=[HomeDropdownMainCellcellWithTableView:tableView];

    cell.textLabel.text=[self.dataSourcehomeDropdown:selftitleOfRowsInMainTable:indexPath.row];

    if([self.dataSourcehomeDropdown:selfsubdataForRowInMainTable:indexPath.row]) {

    //有一个圆形button按钮

    cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;

    }else{

    cell.accessoryType=UITableViewCellAccessoryNone;

    }

    }else{

    //从表

    if(self.regionArray.count!=0){

    cell=[HomeDropdownSubCellcellWithTable:tableView];

    NSString*region=self.regionArray[indexPath.row];

    cell.textLabel.text=region;

    }

    }

    returncell;

    }

    -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

    if(tableView==self.mainTable) {

    //self.selectedCategory=self.categories[indexPath.row];

    self.selectedMainRow=indexPath.row;

    self.regionArray=[self.dataSourcehomeDropdown:selfsubdataForRowInMainTable:indexPath.row];

    //刷新表格

    [self.allowTablereloadData];

    }else{

    NSLog(@"%@",self.regionArray[indexPath.row]);

    }

    }

    实现代理方法:

    -(NSInteger)numberOfRowsInMainTable:(HomeDropdown*)homeDropdown{

    return[MTMetaToolcategories].count;

    }

    -(NSString*)homeDropdown:(HomeDropdown*)homeDropdown

    titleOfRowsInMainTable:(NSInteger)row{

    CategoryModel*categoryModel=[MTMetaToolcategories][row];

    returncategoryModel.name;

    }

    -(NSArray*)homeDropdown:(HomeDropdown*)homeDropdown

    subdataForRowInMainTable:(NSInteger)row{

    CategoryModel*categoryModel=[MTMetaToolcategories][row];

    returncategoryModel.subcategories;

    }

    相关文章

      网友评论

          本文标题:iOS两个TableView的复用

          本文链接:https://www.haomeiwen.com/subject/umtuvttx.html