iOS分段控制器+表格

在这里插入图片描述

myTableViewCell.h 继承UItableviewcell

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface myTableViewCell : UITableViewCell
@property(nonatomic,strong)UIImageView * imgv;
@property(nonatomic,strong)UILabel * zhu;
@property(nonatomic,strong)UILabel * fu;
@property(nonatomic,strong)UILabel * look;
@end

NS_ASSUME_NONNULL_END

myTableViewCell.m

#import “myTableViewCell.h”

@implementation myTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if(self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
[self addSubview:self.imgv];
[self addSubview:self.zhu];
[self addSubview:self.fu];
[self addSubview:self.look];
}
return self;
}

-(UIImageView * )imgv
{
if(!_imgv)
{
_imgv=[[UIImageView alloc]initWithFrame:CGRectMake(3, 10, 200, 150)];

}
return _imgv;

}

-(UILabel * )zhu
{
if(!_zhu)
{
_zhu=[[UILabel alloc]initWithFrame:CGRectMake(210, 10, 210, 40)];
_zhu.font=[UIFont systemFontOfSize:20];
}
return _zhu;
}

-(UILabel * )fu
{
if(!_fu)
{
_fu=[[UILabel alloc]initWithFrame:CGRectMake(210, 60, 210, 40)];
_fu.font=[UIFont systemFontOfSize:16];
_fu.textColor=[UIColor lightGrayColor];
_fu.numberOfLines=0;

}
return _fu;

}

-(UILabel * )look
{
if(!_look)
{
_look=[[UILabel alloc]initWithFrame:CGRectMake(350, 120, 50, 30)];

    _look.textColor=[UIColor lightGrayColor];
    
    _look.textAlignment=NSTextAlignmentRight;
}
return _look;

}

  • (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    }

  • (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    }

@end

AppDelegate.h。

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface AppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong) NSPersistentContainer *persistentContainer;

  • (void)saveContext;

@end

AppDelegate.m

#import “AppDelegate.h”
#import “ViewController.h”
#import “findViewController.h”
#import “myViewController.h”
@interface AppDelegate ()

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UINavigationController * vcNav=[[UINavigationController alloc]initWithRootViewController:[ViewController new]];

    vcNav.tabBarItem=[[UITabBarItem alloc]initWithTitle:@“报告” image:[UIImage imageNamed:@“消息灰.png”] selectedImage:[UIImage imageNamed:@“消息蓝.png”]];

    UINavigationController * findNav=[[UINavigationController alloc]initWithRootViewController:[findViewController new]];

    findNav.tabBarItem=[[UITabBarItem alloc]initWithTitle:@“发现” image:[UIImage imageNamed:@“喜欢灰.png”] selectedImage:[UIImage imageNamed:@“喜欢蓝.png”]];

    UINavigationController * myNav=[[UINavigationController alloc]initWithRootViewController:[myViewController new]];

    myNav.tabBarItem=[[UITabBarItem alloc]initWithTitle:@“我” image:[UIImage imageNamed:@“我的灰.png”] selectedImage:[UIImage imageNamed:@“我的蓝.png”]];

    UITabBarController * tbc=[[UITabBarController alloc]init];

    tbc.viewControllers=@[vcNav,findNav,myNav];

    self.window.rootViewController=tbc;

    return YES;
    }

  • (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

  • (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

  • (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

  • (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

  • (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application’s managed object context before the application terminates.
    [self saveContext];
    }

#pragma mark - Core Data stack

@synthesize persistentContainer = _persistentContainer;

  • (NSPersistentContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
    if (persistentContainer == nil) {
    persistentContainer = [[NSPersistentContainer alloc] initWithName:@"
    __"];
    [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
    if (error != nil) {
    // Replace this implementation with code to handle the error appropriately.
    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                  /*
                   Typical reasons for an error here include:
                   * The parent directory does not exist, cannot be created, or disallows writing.
                   * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                   * The device is out of space.
                   * The store could not be migrated to the current model version.
                   Check the error message to determine what the actual problem was.
                  */
                  NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                  abort();
              }
          }];
      }
    

    }

    return _persistentContainer;
    }

#pragma mark - Core Data Saving support

  • (void)saveContext {
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    NSError *error = nil;
    if ([context hasChanges] && ![context save:&error]) {
    // Replace this implementation with code to handle the error appropriately.
    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    NSLog(@“Unresolved error %@, %@”, error, error.userInfo);
    abort();
    }
    }

@end

ViewController.m

#import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor redColor];
    }

@end

//创建文件
findViewController.h

findViewController.m

#define FenLei @“enumeration/list?type=StrategyType”
#define GongGong @“http://124.65.238.30:3300/bingyun/api/
#define XiangDui @“strategy/page?pageNo=1&pageSize=10&strategyType=POSTURE”
#import “findViewController.h”
#import “myTableViewCell.h”
#import “AFNetworking.h”
#import “UIImageView+WebCache.h”
#import “MJExtension.h”
#import “FeiLeiModel.h”
#import “SuDuModel.h”
@interface findViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UISegmentedControl * segment;
@property(nonatomic,strong)UITableView * tbv;
@property(nonatomic,strong)NSMutableArray * dataArr;
@property(nonatomic,strong)NSMutableArray * feileiArr;
@property(nonatomic,strong)UIButton * btn;
@property(nonatomic,strong)UIButton * twoBtn;
@property(nonatomic,strong)UIButton * threeBtn;
@property(nonatomic,strong)UIButton * fourBtn;

@end

@implementation findViewController

//初始化分段控制器
-(UISegmentedControl * )segment
{
if(!_segment)
{
//设置分段控制器的内容
_segment=[[UISegmentedControl alloc]initWithItems:@[@“攻略”,@“训练营”,@“资讯”]];

    //设置分段控制器的位置
    _segment.frame=CGRectMake(20, 0, self.view.frame.size.width-40, 40);
    
    [_segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];

}
return _segment;

}

//初始化表格
-(UITableView * )tbv
{
if(!_tbv)
{
_tbv=[[UITableView alloc]initWithFrame:CGRectMake(3, 100, self.view.frame.size.width-6, self.view.frame.size.height-80) style:UITableViewStylePlain];

    _tbv.delegate=self;
    
    _tbv.dataSource=self;
    
}
return _tbv;

}

  • (void)viewDidLoad {
    [super viewDidLoad];
    //设置导航不透明
    self.navigationController.navigationBar.translucent=NO;

    //设置主视图背景颜色
    self.view.backgroundColor=[UIColor whiteColor];

    //设置分段控制器的位置,添加到导航条
    self.navigationItem.titleView=self.segment;

    //初始化数组
    self.dataArr=[NSMutableArray new];

    self.feileiArr=[NSMutableArray new];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * string=@“string”;

myTableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:string];

if(!cell)
{
    cell=[[myTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
}
SuDuModel * sd=self.dataArr[indexPath.row];

[cell.imgv sd_setImageWithURL:[NSURL URLWithString:sd.photo]];

cell.zhu.text=sd.title;

cell.fu.text=sd.suitIntro;

cell.look.text=sd.readNum;

UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(340, 115, 50, 40)];
label.text=@"?";

[cell addSubview:label];
return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 170;
}

//实现分段控制器的方法
-(void)change:(UISegmentedControl * )sender
{
if(sender.selectedSegmentIndex==0)
{
NSLog(@“1”);
//设置全部攻略的label
UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(3, 10, 150, 40)];
label.text=@“全部攻略”;

    label.font=[UIFont systemFontOfSize:20];
    
    [self.view addSubview:label];
    
    //拼接字符串网址
    NSString * onestring=[NSString stringWithFormat:@"%@%@",GongGong,FenLei];
    
    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    
    [manager GET:onestring parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"--------%@",responseObject);
        
        //打印totalProperty判断分类按钮的数量
        id shuliang=responseObject[@"totalProperty"];
        NSLog(@"+++++++++%@",shuliang);
        
        //获取data里面的元素
        NSArray * arr=responseObject[@"data"];
        
        //遍历
        for (NSDictionary * dic in arr) {
            FeiLeiModel * feilei=[FeiLeiModel mj_objectWithKeyValues:dic];
            
            
            [self.feileiArr addObject:feilei];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error=%@",error);
    }];
    NSLog(@"\\\\\\\\\\\\\\\\%@",self.feileiArr);
    
    
   
    //数量为4,根据内容创建四个按钮
    //初始化速度按钮
    self.btn=[[UIButton alloc]initWithFrame:CGRectMake(2, 60, self.view.frame.size.width/4-6, 40)];
    
    [self.btn addTarget:self action:@selector(sudu) forControlEvents:UIControlEventTouchUpInside];
    
    [self.btn setTitle:@"速度" forState:UIControlStateNormal];
    
    [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    self.btn.backgroundColor=[UIColor lightGrayColor];
    
    self.btn.layer.masksToBounds=YES;
    
    self.btn.layer.cornerRadius=20;
    
    [self.view addSubview:self.btn];
    
    
    //初始化技巧按钮
    self.twoBtn=[[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/4, 60, self.view.frame.size.width/4-6, 40)];
    
    [self.twoBtn setTitle:@"技巧" forState:UIControlStateNormal];
    
     [self.twoBtn addTarget:self action:@selector(jiqiao) forControlEvents:UIControlEventTouchUpInside];
    
    [self.twoBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    self.twoBtn.backgroundColor=[UIColor lightGrayColor];
    
    self.twoBtn.layer.masksToBounds=YES;
    
    self.twoBtn.layer.cornerRadius=20;
    
    [self.view addSubview:self.twoBtn];
    
    
    //初始化姿势按钮
    self.threeBtn=[[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/4*2, 60, self.view.frame.size.width/4-6, 40)];
    
    [self.threeBtn setTitle:@"姿势" forState:UIControlStateNormal];
    
     [self.threeBtn addTarget:self action:@selector(zishi) forControlEvents:UIControlEventTouchUpInside];
    
    [self.threeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    self.threeBtn.backgroundColor=[UIColor lightGrayColor];
    
    self.threeBtn.layer.masksToBounds=YES;
    
    self.threeBtn.layer.cornerRadius=20;
    
    [self.view addSubview:self.threeBtn];
    
    
    //初始化进球按钮
    self.fourBtn=[[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/4*3, 60, self.view.frame.size.width/4-6, 40)];
    
    [self.fourBtn setTitle:@"进球" forState:UIControlStateNormal];
    
     [self.fourBtn addTarget:self action:@selector(jinqiu) forControlEvents:UIControlEventTouchUpInside];
    
    [self.fourBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    self.fourBtn.backgroundColor=[UIColor lightGrayColor];
    
    self.fourBtn.layer.masksToBounds=YES;
    
    self.fourBtn.layer.cornerRadius=20;
    
    [self.view addSubview:self.fourBtn];
    
}
else if(sender.selectedSegmentIndex==1)
{
    NSLog(@"2");
    
    UIView * view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    view.backgroundColor=[UIColor whiteColor];
    
    [self.view addSubview:view];
}
else if(sender.selectedSegmentIndex==2)
{
    NSLog(@"3");
    
    UIView * oneview=[[UIView alloc]initWithFrame:self.view.frame];
    
    oneview.backgroundColor=[UIColor whiteColor];
    
    [self.view addSubview:oneview];
}

}

//实现速度按钮的点击方法
-(void)sudu
{
self.btn.backgroundColor=[UIColor blueColor];
self.twoBtn.backgroundColor=[UIColor lightGrayColor];
self.threeBtn.backgroundColor=[UIColor lightGrayColor];
self.fourBtn.backgroundColor=[UIColor lightGrayColor];

//添加表格到主视图
[self.view addSubview:self.tbv];


//拼接网址字符串
NSString * string=[NSString stringWithFormat:@"%@%@",GongGong,XiangDui];

//封装请求参数为字典
NSDictionary * dic=@{@"strategyType":@"攻略分类"};

AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];

[manager GET:string parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"00000000000000%@",responseObject);
    
    NSArray * arr=responseObject[@"data"];
    
    for (NSDictionary * dic in arr) {
        SuDuModel * sudu=[SuDuModel mj_objectWithKeyValues:dic];
        
        [self.dataArr addObject:sudu];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
         [self.tbv reloadData];
    });
   
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"%@",error);
}];

}

//实现技巧按钮的点击方法
-(void)jiqiao
{
self.btn.backgroundColor=[UIColor lightGrayColor];
self.twoBtn.backgroundColor=[UIColor blueColor];
self.threeBtn.backgroundColor=[UIColor lightGrayColor];
self.fourBtn.backgroundColor=[UIColor lightGrayColor];

UIView * view=[[UIView alloc]initWithFrame:CGRectMake(3, 100, self.view.frame.size.width-6, self.view.frame.size.height-80)];

view.backgroundColor=[UIColor whiteColor];

[self.view addSubview:view];

}

//实现姿势按钮的点击方法
-(void)zishi
{
self.btn.backgroundColor=[UIColor lightGrayColor];
self.twoBtn.backgroundColor=[UIColor lightGrayColor];
self.threeBtn.backgroundColor=[UIColor blueColor];
self.fourBtn.backgroundColor=[UIColor lightGrayColor];

UIView * view=[[UIView alloc]initWithFrame:CGRectMake(3, 100, self.view.frame.size.width-6, self.view.frame.size.height-80)];

view.backgroundColor=[UIColor whiteColor];

[self.view addSubview:view];

}

//实现进球按钮的点击方法
-(void)jinqiu
{
self.btn.backgroundColor=[UIColor lightGrayColor];
self.twoBtn.backgroundColor=[UIColor lightGrayColor];
self.threeBtn.backgroundColor=[UIColor lightGrayColor];
self.fourBtn.backgroundColor=[UIColor blueColor];

UIView * view=[[UIView alloc]initWithFrame:CGRectMake(3, 100, self.view.frame.size.width-6, self.view.frame.size.height-80)];

view.backgroundColor=[UIColor whiteColor];

[self.view addSubview:view];

}

@end

创建myViewController.h

☆☆☆ “MJCSegmentInterface分段控制器” ☆☆☆ 一款简单的类似百思不得姐主页导航栏下方的那个分段界面的控件,类似王者荣耀助手的聊天模块的导航栏下方的分段界面控件等等,简单的说,这是一款分段界面选择器 ☆☆ 如何导入使用框架方法 ☆☆ 可以打开 https://github.com/MJCIOS/MJCSegmentInterface 将项目下载下来,将MJCSlideInterface文件夹放入自己的项目中 项目已支持pod导入,pod 'MJCSegmentInterface',如果没法导入,可以先[pod repo update]更新整个.cocoapods下的所有库... 有啥问题联系我QQ292251588,希望大神们和我多多交流,和大神们一起学习..... 技术交流群612845323,希望和大神们一起学习多多交流,互相学习些技术..... 希望大家多多支持,如果觉得好用,多多帮忙推荐,谢谢大家,谢谢大神,有啥问题,可以提给我... ☆☆ MJCSegmentInterface分段控制器创建(几行代码设置即可使用) ☆☆ NSArray *titlesArr = @[@"啦啦",@"么么",@"啪啪",@"啪啪",@"啪啪",@"啪啪",@"啪啪"]; MJCSegmentFaceControl *segmentsface = [[MJCSegmentFaceControl alloc]init]; segmentsface.frame = CGRectMake(0,64, MJCScreenWidth,MJCScreenHeight); [lala intoTitlesArray:titlesArr hostController:self]; [self.view addSubview:segmentsface]; MJCTestViewController *vc1 = [[MJCTestViewController alloc]init]; vc1.titlesCount = 1; MJCTestTableViewController *vc2 = [[MJCTestTableViewController alloc]init]; vc2.titlesCount = 2; MJCTestViewController1 *vc3 = [[MJCTestViewController1 alloc]init]; vc3.titlesCount = 3; MJCTestViewController *vc4 = [[MJCTestViewController alloc]init]; vc4.titlesCount = 4; MJCTestViewController *vc5 = [[MJCTestViewController alloc]init]; vc5.titlesCount = 5; NSArray *vcarrr = @[vc1,vc2,vc3,vc4,vc5]; [lala intoChildControllerArray:vcarrr]; 可自行修改各个属性,具体属性接口可进入项目查看.. /** 添加控制器的方法(添加控制器按照控制器添加的先后顺序与按钮对应的 */ -(void)intoChildControllerArray:(NSArray *)childControllerArray; /** 添加标题栏的方法 */ -(void)intoTitlesArray:(NSArray *)titlesArray hostController:(UIViewController *)hostController;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值