设置:
添加bar button item
添加TableView,,push。
推送和提醒:
用代码:
创建ILSettingTableViewController : UITableViewController放在setting文件夹。
设置按钮,如何跳转:
ILLoginViewController.m:
- (IBAction)setting:(id)sender {
// 创建设置口控制器
ILSettingTableViewController *settingVc = [[ILSettingTableViewController alloc] init];
// 调整到设置界面
[self.navigationController pushViewController:settingVc animated:YES];
}
//group模式
ILSettingTableViewController.m:
- (id)init
{
return [super initWithStyle: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 = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
//group模式
- (id)init
{
return [super initWithStyle:UITableViewStyleGrouped];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 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
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//懒加载
- (NSMutableArray *)dataList
{
if (_dataList == nil) {
_dataList = [NSMutableArray array];
//创建模型:
// 0组
LISettingArrowItem *pushNotice = [LISettingArrowItem itemWithIcon:@"MorePush" title:@"推送和提醒"];
pushNotice.destVcClass = [LITestViewController class];
pushNotice.destVcName = @"LITestViewController";
LISettingItem *yaoyiyao = [LISettingSwitchItem itemWithIcon:@"handShake" title:@"摇一摇机选"];
LISettingGroup *group0 = [[LISettingGroup alloc] init];
group0.items = @[pushNotice,yaoyiyao];
group0.header = @"asdas";
group0.footer = @"asdasd";
// 1组
LISettingItem *newVersion = [LISettingArrowItem itemWithIcon:@"MoreUpdate" title:@"检查新版本"];
LISettingItem *help = [LISettingArrowItem itemWithIcon:@"MoreHelp" title:@"帮助"];
LISettingGroup *group1 = [[LISettingGroup alloc] init];
group1.header = @"帮助";
group1.items = @[newVersion,help];
[_dataList addObject:group0];
[_dataList addObject: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 = [[LISettingCell class] 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:[LISettingArrowItem class]]) { // 需要跳转控制器
LISettingArrowItem *arrowItem = (LISettingArrowItem *)item;
Class vcClass = NSClassFromString(arrowItem.destVcName);
// 创建跳转的控制器
UIViewController *vc = [[arrowItem.destVcClass alloc] init];
vc.title = item.title; //跳转界面的标题
[self.navigationController pushViewController: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 = [[self alloc] 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 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellArrow"]];
}
return _imgView;
}
- (UISwitch *)switchView
{
if (_switchView == nil) {
_switchView = [[UISwitch alloc] init];
}
return _switchView;
}
- (void)setItem:(LISettingItem *)item
{
_item = item;
// 1.设置cell的子控件的数据
[self setUpData];
// 2.设置右边视图
[self setUpAccessoryView];
}
// 设置cell的子控件的数据
- (void)setUpData
{
self.imageView.image = [UIImage imageNamed:_item.icon];
self.textLabel.text = _item.title;
}
// 设置右边视图
- (void)setUpAccessoryView
{
if ([_item isKindOfClass:[LISettingArrowItem class]]) { // 箭头
self.accessoryView = self.imgView;
self.selectionStyle = UITableViewCellSelectionStyleDefault;//小细节,防止一次设置之后无法改变
}else if ([_item isKindOfClass:[LISettingSwitchItem class]]){ // 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 = [tableView dequeueReusableCellWithIdentifier:ID ];
if (cell == nil) {
cell = [[LISettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
return cell;
}
@end