标签:
一个页面显示两个tableview,并且每个tableview上的数据都不一样,一般用以下方法:
首先建一个继承自UIView的类,来表示用来切换tableview的view
//在view的类的.h文件中
#import <UIKit/UIKit.h>
@protocol MyAttentionHeadViewDelegate <NSObject> //建一个叫MyAttentionHeadViewDelegate的一个代理
@optional //两个代理方法(可选择实现optional)
-(void)myAttentionHeadViewLeftBtnClicked:(UIButton*)btn;
-(void)myAttentionHeadViewRightBtnClicked:(UIButton*)btn;
@end
@interface MyAttentionHeadView : UIView
@property (nonatomic,strong) UIButton *leftBtn; //定义左侧按钮
@property (nonatomic,strong) UIButton *rightBtn; //定义右侧按钮
@property (nonatomic,strong) UIView *colorView; //定义按钮下方颜色条
@property (nonatomic,assign) id<MyAttentionHeadViewDelegate>delegate; //代理者
@end
//在view的类的.m文件中
#import "MyAttentionHeadView.h"
@implementation MyAttentionHeadView
//initWithFrame方法用来初始化并返回一个新的视图对象,根据指定的CGRect(尺寸)
-(id)initWithFrame:(CGRect)frame{
self =[super initWithFrame:frame];
if (self) {
self.backgroundColor =[UIColor whiteColor]; //设置view的背景颜色
_colorView =[[UIView alloc]init]; //创建按钮下方颜色条
_colorView.frame =CGRectMake(self.frame.size.width/2.0, self.frame.size.height-1.0, self.frame.size.width/2.0, 1.0);
_colorView.backgroundColor =[UIColor greenColor];
[self addSubview:_colorView]; //将颜色条加入到view中
_leftBtn =[UIButton buttonWithType:UIButtonTypeCustom]; //创建左侧按钮
[_leftBtn setTitle:@"关注的采购商" forState: UIControlStateNormal]; //设置按钮上的文字
[_leftBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; //设置未选择状态下文字颜色
[_leftBtn setTitleColor:[UIColor greenColor] forState:UIControlStateSelected]; //设置选择状态下文字颜色
_leftBtn.titleLabel.font =[UIFont systemFontOfSize:15.0f]; //设置文字大小
_leftBtn.frame =CGRectMake(0, 0, self.frame.size.width/2.0, self.frame.size.height-1.0); //设置按钮的frame
[_leftBtn addTarget:self action:@selector(leftBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
//为按钮添加点击事件
_leftBtn.backgroundColor =[UIColor whiteColor];//设置按钮背景色
[self addSubview:_leftBtn]; //将按钮添加到view中
_rightBtn =[UIButton buttonWithType:UIButtonTypeCustom];
_rightBtn.frame =CGRectMake(self.frame.size.width/2.0, 0, self.frame.size.width/2.0, self.frame.size.height-1.0);
_rightBtn.backgroundColor =[UIColor whiteColor];
[_rightBtn setTitle:@"关注的农场" forState:UIControlStateNormal];
[_rightBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[_rightBtn setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
_rightBtn.titleLabel.font =[UIFont systemFontOfSize:15.0f];
[_rightBtn addTarget:self action:@selector(rightBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_rightBtn];
}
return self;
}
//点击左侧按钮事件
-(void)leftBtnClicked:(UIButton*)btn{
_leftBtn.selected =YES; //左侧按钮选中状态
_rightBtn.selected =NO;//右侧按钮非选中状态
//点击按钮后的动画设置
[UIView animateWithDuration:0.2f animations:^{
_colorView.frame =CGRectMake(0, self.frame.size.height-1.0, self.frame.size.width/2.0, 1.0);
//颜色条在左侧按钮下方
}];
//如果存在叫myAttentionHeadViewLeftBtnClicked:的方法,那么调用这个方法
if ([_delegate respondsToSelector:@selector(myAttentionHeadViewLeftBtnClicked:)]) {
[_delegate myAttentionHeadViewLeftBtnClicked:btn];
}
}
//点击右侧按钮事件
-(void)rightBtnClicked:(UIButton*)btn{
_leftBtn.selected =NO;
_rightBtn.selected =YES;
[UIView animateWithDuration:0.2f animations:^{
_colorView.frame =CGRectMake(self.frame.size.width/2.0, self.frame.size.height-1.0, self.frame.size.width/2.0, 1.0);
}];
if ([_delegate respondsToSelector:@selector(myAttentionHeadViewRightBtnClicked:)]) {
[_delegate myAttentionHeadViewRightBtnClicked:btn];
}
}
@end
//在controller类的.m文件中导入view类的头文件
@interface MyAttentionViewController ()<UITableViewDataSource,UITableViewDelegate,MyAttentionHeadViewDelegate>
//遵从tableview的两个代理和在view类的自定义代理
@property (weak, nonatomic) IBOutlet UITableView *leftTableView; //在xib文件中拖入两个tableview
@property (weak, nonatomic) IBOutlet UITableView *rightTableView;
@property (nonatomic,strong) MyAttentionHeadView *headView; //定义一个view的属性
@property (nonatomic,strong) NSMutableArray *arrays; //数据源
@end
@implementation MyAttentionViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title =@"我的关注"; //设置标题
_headView =[[MyAttentionHeadView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 40)];// 实例化headview
_headView.delegate =self; //设置代理
[self.view addSubview:_headView]; //将headview添加到tableview上
[self refreshData]; //刷新数据
}
//刷新数据方法
-(void)refreshData{
//给数据源建立假数据,用来显示cell的个数
_arrays =[NSMutableArray arrayWithCapacity:2];
NSArray *array1 =[NSMutableArray arrayWithArray:@[@"0"]];
NSArray *array2 =[NSMutableArray arrayWithArray:@[@"0",@"1",@"2",@"3",@"4",@"5"]];
[_arrays addObject:array1];
[_arrays addObject:array2];
[self.leftTableView reloadData]; //tableview刷新
[self.rightTableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _arrays.count; //多少组
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *ary =_arrays[section];
return ary.count; //对应每组多少行
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView isEqual:_leftTableView]) {
//如果显示左侧tableview
if (indexPath.section==0 && indexPath.row==0) {
//第一组第一行加载AddNewPurchaseCell
AddNewPurchaseCell *cell =[tableView dequeueReusableCellWithIdentifier:@"AddNewPurchaseCell"];
if (cell==nil) {
cell =[[[NSBundle mainBundle]loadNibNamed:@"AddNewPurchaseCell" owner:nil options:nil]lastObject];
}
return cell;
}else{
//否则加载 AttentionPurchaseCell
AttentionPurchaseCell *cell1 =[tableView dequeueReusableCellWithIdentifier:@"AttentionPurchaseCell"];
if (cell1==nil) {
cell1 =[[[NSBundle mainBundle]loadNibNamed:@"AttentionPurchaseCell" owner:nil options:nil]lastObject];
//注意:千万别忘记写lastObject
}
return cell1;
}
}else if ([tableView isEqual:_rightTableView]){
//如果显示右侧tableview
if (indexPath.section==0 && indexPath.row ==0) {
AddNewFarmCell *cell2 =[tableView dequeueReusableCellWithIdentifier:@"AddNewFarmCell"];
if (cell2==nil) {
cell2 =[[[NSBundle mainBundle]loadNibNamed:@"AddNewFarmCell" owner:nil options:nil]lastObject];
}
return cell2;
}else{
AttentionFarmCell *cell3 =[tableView dequeueReusableCellWithIdentifier:@"AttentionFarmCell"];
if (cell3==nil) {
cell3 =[[[NSBundle mainBundle]loadNibNamed:@"AttentionFarmCell" owner:nil options:nil]lastObject];
}
return cell3;
}
}
return nil;
}
//判断tableview是否可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return indexPath.section==0 ?NO:YES;
}
//判断编辑类型
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
//把英文变成中文的删除
-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除";
}
//删除方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle ==UITableViewCellEditingStyleDelete) {
//如果编辑的类型是删除,那么选中某行进行删除
NSMutableArray *arrM =_arrays[indexPath.section];
NSObject *obj =arrM[indexPath.row];
if ([arrM containsObject:obj]) {
[arrM removeObject:obj];
}
}
[_leftTableView reloadData]; //刷新两个tableview
[_rightTableView reloadData];
}
//两个自定义的代理方法的实现
-(void)myAttentionHeadViewLeftBtnClicked:(UIButton*)btn{
_leftTableView.hidden=NO;
_rightTableView.hidden=YES;
}
-(void)myAttentionHeadViewRightBtnClicked:(UIButton*)btn{
_leftTableView.hidden =YES;
_rightTableView.hidden =NO;
}
标签:
原文地址:http://www.cnblogs.com/lv14/p/5010666.html