標籤:
一個頁面顯示兩個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 [email protected]"我的關注清單"; //設定標題
_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;
}
iOS中一個頁面顯示兩個tableview的情況