设置界面的搭建和封装

来源:互联网 发布:江阴市网络教研平台 编辑:程序博客网 时间:2024/05/15 11:53

设置:


添加bar button item


添加TableView,push


Pasted Graphic 1.tiff


推送和提醒:Pasted Graphic 1_1.tiff






用代码:



创建ILSettingTableViewController :UITableViewController放在setting文件夹。


设置按钮,如何跳转:

 ILLoginViewController.m

- (IBAction)setting:(id)sender {

    

    //创建设置口控制器

    ILSettingTableViewController *settingVc = [[ILSettingTableViewControlleralloc] init];

    

    // 调整到设置界面

    [self.navigationControllerpushViewController:settingVc animated:YES];

    

    

}

//group模式

ILSettingTableViewController.m

- (id)init

{

    return [superinitWithStyle:UITableViewStyleGrouped];

}



显示数据:

plist







 ILSettingTableViewController.m

@interface ILSettingTableViewController ()


@property (nonatomic,strong) NSMutableArray *dataList; //存放group


@end

@property (nonatomic,strong) NSMutableArray *dataList;

ILSettingTableViewController.m:  



#import "LISettingTableViewController.h"

#import "LISettingGroup.h"

#import "LISettingItem.h"

#import "LISettingSwitchItem.h"

#import "LISettingArrowItem.h"

#import "LITestViewController.h"

#import "LISettingCell.h"


@interface LISettingTableViewController ()


@property (nonatomic,strong) NSMutableArray *dataList;//存放group


@end


@implementation LISettingTableViewController


- (id)initWithStyle:(UITableViewStyle)style

{

   self = [superinitWithStyle:style];

   if (self) {

        // Custom initialization

    }

    return self;

}

//group模式

- (id)init

{

    return [superinitWithStyle:UITableViewStyleGrouped];

}

- (void)viewDidLoad

{

    [superviewDidLoad];

    

    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

    

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

//懒加载

- (NSMutableArray *)dataList

{

   if (_dataList ==nil) {

       _dataList = [NSMutableArrayarray];

       //创建模型:

       // 0

        LISettingArrowItem *pushNotice = [LISettingArrowItemitemWithIcon:@"MorePush"title:@"推送和提醒"];

        pushNotice.destVcClass = [LITestViewControllerclass];

        pushNotice.destVcName =@"LITestViewController";

        LISettingItem *yaoyiyao = [LISettingSwitchItemitemWithIcon:@"handShake"title:@"摇一摇机选"];

        

        LISettingGroup *group0 = [[LISettingGroupalloc] init];

        group0.items =@[pushNotice,yaoyiyao];

        group0.header =@"asdas";

        group0.footer =@"asdasd";

        

       // 1

        LISettingItem *newVersion = [LISettingArrowItemitemWithIcon:@"MoreUpdate"title:@"检查新版本"];

        LISettingItem *help = [LISettingArrowItemitemWithIcon:@"MoreHelp"title:@"帮助"];

        

        

        

        LISettingGroup *group1 = [[LISettingGroupalloc] init];

        group1.header =@"帮助";

        group1.items =@[newVersion,help];

        

        [_dataListaddObject:group0];

        [_dataListaddObject:group1];

        

    }

    

    return_dataList;

}


//告知系统每一行显示什么内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //  forIndexPath:indexPath  只能是srotryboard加载时才有

    //  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath] 

   // 改为 

    // 创建cell

    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]  ;

    LISettingCell *cell = [[LISettingCellclass] cellWithTableView:tableView];

    

    // 取出模型

   LISettingGroup *group = self.dataList[indexPath.section];

   LISettingItem *item = group.items[indexPath.row];

    cell.item =item;

   return cell;

}


//点击每个cell的跳转

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    // 取出模型

   LISettingGroup *group = self.dataList[indexPath.section];

   LISettingItem *item = group.items[indexPath.row];

    

   if ([item isKindOfClass:[LISettingArrowItemclass]]) { // 需要跳转控制器

        LISettingArrowItem *arrowItem = (LISettingArrowItem *)item;

        

        Class vcClass =NSClassFromString(arrowItem.destVcName);

        

        // 创建跳转的控制器

       UIViewController *vc = [[arrowItem.destVcClassalloc] init];

        vc.title = item.title;//跳转界面的标题

        [self.navigationControllerpushViewController:vc animated:YES];

        

    }

    

    

    

}


#pragma mark - Table view data source

//告诉tableview一共有多少组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

#warning Potentially incomplete method implementation.

    // Return the number of sections.

    return self.dataList.count;

}

//section组有多少行


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

#warning Incomplete method implementation.

    // Return the number of rows in the section.

   LISettingGroup *group = self.dataList[section];

   return group.items.count;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    LISettingGroup *group =self.dataList[section];

   return group.header;

}


- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

{

    LISettingGroup *group =self.dataList[section];

   return group.footer;

}


@end





模型:




创建group模型:

ILSettingGroup :NSObject

@property (nonatomic,copy) NSString *header;  //头部标题


@property (nonatomic,strong) NSArray *items;  //strong,存放cell


@property (nonatomic,copy) NSString *footer;  //尾标用copy






创建ILSettingItem :NSObject 放在model文件夹里  ,管理每个cell


@property (nonatomic,copy) NSString *title;

@property (nonatomic,copy) NSString *icon;  //注意都是copy


+ (instancetype)itemWithIcon:(NSString *)icon title:(NSString *)title; //工厂方法,创建对象


+ (instancetype)itemWithIcon:(NSString *)icon title:(NSString *)title

{

   ILSettingItem *item = [[selfalloc] init];//为什么用self,不写ILSettingItem防止子类调用

    

    item.icon = icon;

    item.title = title;

    

   return item;

}


两种cell样式。新建cell模型继承ILSettingItem


LISettingArrowItem

其中,LISettingArrowItem.h

  #import "ILSettingItem.h"


  @interface ILSettingArrowItem :ILSettingItem


  // 调整的控制器的类名

  @property (nonatomic,assign) Class destVcClass;


  @property (nonatomic,copy) NSString *destVcName;


  @end

ILSettingSwitchItem







需要根据模型来给每个cell设置数据和类型,因而,通过自定义cell来实现。分别指定显示的数据和右边显示的内容类型。(箭头  switch 或者默认)

新建ILSettingCell 封装cell view模型继承UITableViewCell放到view   里面封装ILSettingItem

LISettingCell.h

#import <UIKit/UIKit.h>


@class LISettingItem;


@interface LISettingCell :UITableViewCell


@property (nonatomic,strong) LISettingItem *item;


+ (instancetype)cellWithTableView:(UITableView *)tableView;//自定义的方法快速创建


@end

LISettingCell.m



#import "LISettingCell.h"

#import "LISettingSwitchItem.h"

#import "LISettingArrowItem.h"

@interface LISettingCell()



@property (nonatomic,strong) UIImageView *imgView;

@property (nonatomic,strong) UISwitch *switchView;


@end


@implementation LISettingCell

- (UIImageView *)imgView

{

   if (_imgView ==nil) {

        _imgView = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"CellArrow"]];

    }

    return_imgView;

}


- (UISwitch *)switchView

{

   if (_switchView ==nil) {

        

       _switchView = [[UISwitchalloc] init];

    }

    return_switchView;

}




- (void)setItem:(LISettingItem *)item

{

   _item = item;

    

    

    // 1.设置cell的子控件的数据

    [selfsetUpData];

    

    // 2.设置右边视图

    [selfsetUpAccessoryView];

    

}


// 设置cell的子控件的数据

- (void)setUpData

{

    self.imageView.image = [UIImageimageNamed:_item.icon];

    self.textLabel.text =_item.title;

}


// 设置右边视图

- (void)setUpAccessoryView

{

    if ([_itemisKindOfClass:[LISettingArrowItemclass]]) { // 箭头

       self.accessoryView =self.imgView;

        self.selectionStyle =UITableViewCellSelectionStyleDefault;//小细节,防止一次设置之后无法改变

    }elseif ([_itemisKindOfClass:[LISettingSwitchItemclass]]){ // Switch

       self.accessoryView =self.switchView;

        

        self.selectionStyle =UITableViewCellSelectionStyleNone;

    }else{

       self.accessoryView =nil;

        self.selectionStyle =UITableViewCellSelectionStyleDefault;

    }

}




+ (instancetype)cellWithTableView:(UITableView *)tableView

{

   static NSString *ID =@"cell";

    LISettingCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID ];

    

   if (cell == nil) {

        cell = [[LISettingCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];

    }

    

   return cell;

}



@end

0 0