先上圖:
我們經常在專案中使用到UISegmentControl進行UITableView的切換 下面我說說我實現的過程。
這個UISegmentControl我是放在導航欄的中間的,實現程式碼如下:
//建立導航欄分欄控制元件
-(void)createSegMentController{
NSArray *segmentedArray = [NSArray arrayWithObjects:@"問答",@"傾述",nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
segmentedControl.frame = CGRectMake(0, 0, 180, 30);
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.tintColor = [UIColor colorWithRed:252/255.0 green:245/255.0 blue:248/255.0 alpha:1];
[segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
[self.navigationItem setTitleView:segmentedControl];
}
複製程式碼
下面講講如何實現UITableView的切換:
建立兩個UITableView
UITableView *leftTable;
UITableView *rightTable;
複製程式碼
leftTable = [[UITableView alloc]initWithFrame:CGRectMake(0,64,WIDTH,HEIGHT-64-44) style:UITableViewStylePlain];
leftTable.delegate = self;
leftTable.dataSource = self;
[leftTable registerNib:[UINib nibWithNibName:@"AnswerCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:AnswerReuseId];
leftTable.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:leftTable];
rightTable = [[UITableView alloc]initWithFrame:CGRectMake(0,64,WIDTH,HEIGHT-64-44) style:UITableViewStylePlain];
rightTable.delegate = self;
rightTable.dataSource = self;
[rightTable registerNib:[UINib nibWithNibName:@"AnswerCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:AnswerReuseId];
rightTable.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:rightTable];
複製程式碼
- 接下來我們實現切換:
-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender
{
//我定義了一個 NSInteger tag,是為了記錄我當前選擇的是分段控制元件的左邊還是右邊。
NSInteger selecIndex = sender.selectedSegmentIndex;
switch(selecIndex){
case 0:
leftTable.hidden = NO;
rightTable.hidden = YES;
sender.selectedSegmentIndex=0;
tag = 0;
[leftTable reloadData];
break;
case 1:
leftTable.hidden = YES;
rightTable.hidden = NO;
sender.selectedSegmentIndex = 1;
tag=1;
[rightTable reloadData];
break;
default:
break;
}
}
複製程式碼
接下來實現UITableView的代理和資料來源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(tag==0){
return array.count;
}
else if (tag==1){
return array.count;
}
return 0;
}
複製程式碼
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(tag == 0){
AnswerCell *cell = [leftTable dequeueReusableCellWithIdentifier:AnswerReuseId ];
if (!cell) {
cell = (AnswerCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: AnswerReuseId];
}
//cell.model = array[indexPath.row];
cell.content.text = array[indexPath.row];
cell.userImg.image = [UIImage imageNamed:@"tabbar_home_selected@2x"];
cell.userName.text = @"一個演員的自我修養";
cell.title.text = @"演員的自我修養";
cell.time.text = @"4小時前";
cell.star.text = @"1344";
cell.counts.text = @"445";
// 獲取不確定文字的高度
NSString *str = array[indexPath.row];
CGFloat testHeight = [self sizeWithFont:[UIFont systemFontOfSize:14.0] maxW:cell.frame.size.width withContent:str] + 10;
// 重新設定cell的frame.
CGRect frame = cell.frame;
frame.size.height = testHeight + self.originCellMaxY;
cell.frame = frame;
return cell;
}
else if (tag==1){
AnswerCell *cell = [rightTable dequeueReusableCellWithIdentifier:AnswerReuseId ];
if (!cell) {
cell = (AnswerCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: AnswerReuseId];
}
//cell.model = array[indexPath.row];
cell.content.text = array[indexPath.row];
cell.userImg.image = [UIImage imageNamed:@"tabbar_home_selected@2x"];
cell.userName.text = @"一個演員的自我修養";
cell.title.text = @"演員的自我修養";
cell.time.text = @"4小時前";
cell.star.text = @"1344";
cell.counts.text = @"445";
// 獲取不確定文字的高度
NSString *str = array[indexPath.row];
CGFloat testHeight = [self sizeWithFont:[UIFont systemFontOfSize:14.0] maxW:cell.frame.size.width withContent:str] + 10;
// 重新設定cell的frame.
CGRect frame = cell.frame;
frame.size.height = testHeight + self.originCellMaxY;
cell.frame = frame;
return cell;
}
return nil;
}
複製程式碼
效果圖是這樣的:
對了說一下,每個單元格之間那段距離 我是在自定義的cell中放了一個高度為10的UIView。哈哈
複製程式碼
就是這個樣子: