设置界面的搭建和封装
来源:互联网 发布:江阴市网络教研平台 编辑:程序博客网 时间:2024/05/15 11:53
设置:
添加bar button item
添加TableView,,push。
推送和提醒:
用代码:
创建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
- 设置界面的搭建和封装
- TabBar和Navigation搭建的界面
- 节日 table和button的界面设置
- 用IOS做一个界面切换的效果(登录界面和注册界面和找回密码界面的切换)(用封装好的lable和textf创建界面)
- ios8 xcode6 下的启动界面设置和图标设置
- 搭建vue环境(http://localhost:8080/ 显示vue设置好的界面 基本搭建成功)
- 登陆界面的搭建
- 09设置界面的布局和设置界面逻辑的完成
- 一般情况的界面搭建和数据的填充
- ui界面搭建,自己的理解和总结
- 侧滑和主界面的Fragment搭建
- vuejs 和 element 搭建的一个后台管理界面
- Android的设置界面
- Android的设置界面:SharedPreferences和PreferenceActivity的使用
- MVC、JSP实现mysql的增删改查功能的封装和简陋的界面交互
- 游戏中的音乐和音效的设置界面
- 兼容MIUI5和MIUI6的开启悬浮窗设置界面
- ubuntu启动到字符界面和图形界面的设置
- 关于SqLite的研究
- 好的深度学习——学习地址
- spring mvc 结合jackson返回的日期格式问题
- POJ 1389 (扫描线)
- Android 文件的上传
- 设置界面的搭建和封装
- libvirt的安装流程
- 九度oj-1088-剩下的树
- JDK-Eclipse-Android_SDK-ADT详细图文安装教程(2)
- 设置界面-更新
- Bitmap的相关方法汇总
- int 和Integer创建对象在内存中的初始值不同
- java设计模式(一)—策略模式
- C语言段与内存的理解