本章讲解常用控件UISegmentedControl在storyBoard下经常使用的两种方法。(在此,展示的数据用tableView)
1.首先创建个工程,我习惯性是要带导航的,便于返回,然后加两个按钮,一个用于测试展示多个tableviewcontroller,既变数据源也直接换了tableView,一个用于只添加一个tableViewController变的是数据源。
2.先以多个tableView为例讲解。
在MoreTableViewController上添加两个控件:UISegmented和Container View,添加约束条件,拉线到MoreTableViewController里面,然后添加三个tableViewcontroller,以上图形式拖拽,我分别取名为FirstTableViewController,SecondTableViewController,ThirdTableViewController。MoreTableViewController.h里面内容大概如下:
#import "FirstTableViewController.h"
#import "SecondTableViewController.h"
#import "ThirdTableViewController.h"
@interface MoreTableViewController : UIViewController
{
intcurrentSegmentIndex;
}
@property (nonatomic,strong) FirstTableViewController*firstVC;
@property (nonatomic,strong) SecondTableViewController*secondVC;
@property (nonatomic,strong) ThirdTableViewController*thirdVC;
@property(nonatomic) int startupSegmentIndex;
@property (strong, nonatomic) IBOutlet UISegmentedControl*segmentedController;
- (IBAction)changeView:(UISegmentedControl *)sender;
@property (strong, nonatomic) IBOutlet UIView*bottonview;//contaiinerView
//根据点击的index设置要显示的controller
-(void) setBottomViewByIndex:(NSInteger)viewIndex;
// 是底部View自适应
- (void)fullfillToBottomView:(UIView *)subView ;
MoreTableViewController.m文件 详见代码。
然后我下面截一下三个控制器里面的内容:
first:
second:
third:
横评:
3.主要讲解一个tableview时,切换index时数据的变化,不用n多个控制器就可解决。
在OneTableViewController里面,放一个UISegmented和tableView,
然后拉线到OneTableViewController.h,.h内容大致如下:
@property (nonatomic,assign) NSInteger currentIndex;
@property (strong, nonatomic) IBOutlet UISegmentedControl*segmented;
- (IBAction)segmentedClick:(UISegmentedControl *)sender;
@property (strong, nonatomic) IBOutlet UITableView*table;
下面我主要把.m文件的主要代码粘贴一下
-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(currentIndex == 0) {
table.rowHeight= 50;
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
if(indexPath.row%3 == 1) {
cell.imageView.image = [UIImage imageNamed:@"4.jpg"];
cell.textLabel.text = @"图片四";
}else if(indexPath.row%3 == 2){
cell.imageView.image = [UIImage imageNamed:@"5.jpg"];
cell.textLabel.text = @"图片五";
}else{
cell.imageView.image = [UIImage imageNamed:@"6.jpg"];
cell.textLabel.text = @"图片六";
}
// //图片设置圆角
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 15;
returncell;
}else if(currentIndex == 1){
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell2"];
if(indexPath.row%3 == 0) {
cell.textLabel.text = @"想要了解更多内容";
cell.detailTextLabel.text = @">";
}else if(indexPath.row%3 == 1){
cell.textLabel.text = @"详情关注博客";
cell.detailTextLabel.text = @">";
}else{
cell.textLabel.text = @"博客地址:http://blog.csdn.net/dandelion9";
cell.detailTextLabel.text = @">";
}
returncell;
}else{
//预计高度为88
table.estimatedRowHeight = 88.0f;
//自适应高度
table.rowHeight = UITableViewAutomaticDimension;
ThirdTWOCell *cell = [tabledequeueReusableCellWithIdentifier:@"cell3"];
if (cell ==nil) {
cell = [[[NSBundlemainBundle]loadNibNamed:@"ThirdTWOCell" owner:selfoptions:nil]lastObject];
}
cell.title.text = titleArray[indexPath.row];
cell.title.textColor = [UIColor purpleColor];
cell.content.text = contentArray[indexPath.row];
cell.content.textColor = [UIColor blueColor];
returncell;
}
}
实现效果,当然这里我对UISegment稍微设置了一些属性.
好了到这里结束,详情请下载demo。
https://github.com/LuoLuoZhou/UISegmentedControl-In-UIStoryBoard.git