IOS development-setting interface Extraction

Source: Internet
Author: User

IOS development-setting interface Extraction

So how can we write the settings interface to make it easier to use? Next I will talk about my ideas. 1. observe the prototype Diagram 2. the purpose of finding out the same thing is to extract a base class module. As long as I write this controller, all other interfaces are written. 3. determine whether to use pure code or storyboard. If the control position on the interface is fixed, use storyboard. When to use static cells: If the Interface cell is similar to the built-in cell, you can select static cells if they are not similar: 1. determine if all other cells are similar. If all cells are similar, you can use static Cells 2. if the interface is very different and no static cells are used, use the code. Next we will extract the setting interface: first, inherit the UITableViewController and write a BasicSettingViewController. h // The number of rows in the cell group <SettingItem> @ property (nonatomic, strong) NSMutableArray * groups ;. m # import "BasicTableViewController. h "# import" GroupItem. h "# import" SettingItem. h "# import" BasicCell. h "@ interface BasicTableViewController () @ End @ implementation BasicTableViewController // Let this class initialize as a group style-(instancetype) init {return [self initWithStyle: UITableViewStyleGrouped];}-(void) viewDidLoad {[super viewDidLoad]; // you can specify the basic information of tableView. tableView. backgroundColor = [UIColor colorWithRed: 211 green: 211 blue: 211 alpha: 1]; self. tableView. sectionHeaderHeight = 10; self. tableView. sectionFooterHeight = 0; self. tableView. contentInset = UIEdgeInsetsMake (-25, 0, 0, 0); self. tableView. separatorStyle = UITableViewCellSeparatorStyleNone;} // You must load the group lazily. Otherwise, the subclass Cannot initialize its-(NSMutableArray *) groups {if (! _ Groups) {_ groups = [NSMutableArray array];} return _ groups;} # pragma mark-Table view data source // number of returned groups-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {# warning Incomplete implementation, return the number of sections return self. groups. count;} // number of returned rows-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {# warning Incomplete implementation, retu Rn the number of rows GroupItem * group = self. groups [section]; return group. items. count;} // initialize the cell and assign a value to the cell-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {BasicCell * cell = [BasicCell cellWithTableView: tableView]; groupItem * group = self. groups [indexPath. section]; SettingItem * item = group. items [indexPath. row]; cell. item = item; [cell setInde XPath: indexPath rowCount: group. items. count]; return cell;} // return the title of the foot-(NSString *) tableView :( UITableView *) tableView titleForFooterInSection :( NSInteger) section {GroupItem * group = self. groups [section]; return group. footTitle;} // return the header title-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {GroupItem * group = self. groups [section]; return group. headTitle;} // select when cell Run this method-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {[tableView deselectRowAtIndexPath: indexPath animated: YES]; GroupItem * group = self. groups [indexPath. section]; SettingItem * item = group. items [indexPath. row]; if (item. destVc) {// if there is a jump, execute the jump UIViewController * destVc = [item. destVc alloc] init]; [self. navigationController pushViewController: destVc Animated: YES];} if (item. option) {// the cell that does things (do your own things) item. option () ;}} copy the code to set the group model (base class): copy the code @ interface GroupItem: NSObject // The number of rows in each group @ property (nonatomic, strong) NSArray * items; // header title @ property (nonatomic, copy) NSString * headTitle; // footer title @ property (nonatomic, copy) NSString * footTitle; @ end typedef void (^ SettingItemOption) (); @ interface SettingItem: NSObject // God map on the left @ property (nonatomic, strong) UII Mage * image; // title @ property (nonatomic, copy) NSString * title; // subTitle @ property (nonatomic, copy) NSString * subTitle; @ property (nonatomic, assign) class destVc; // jump to the Target Controller (it is best not to use NSString) @ property (nonatomic, copy) SettingItemOption option; // you can only use copy in the ARC model, MRC can use copy or strong. In order to put it in the heap memory, if it is put in the stack memory, it will be released // provide two methods for creating items + (instancetype) itemWithTitle :( NSString *) title; + (instancetype) itemWithTitle :( NSString *) Title image :( UIImage *) image; @ end because the content changes according to the model, we will create several models (inherited from SettingItem ): if each base class has its own unique attributes, you can also set @ interface ArrowItem: SettingItem @ end @ interface BadgeItem: SettingItem @ property (nonatomic, copy) NSString * badgeValue; @ end @ interface SwitchItem: SettingItem @ property (nonatomic, assign) BOOL isOn; // whether to enable @ end @ interface LabelItem: SettingItem @ property (nonatomic, copy) NSString * text; // text on the label @ End @ interface CheakItem: SettingItem @ property (nonatomic, assign) BOOL isCheak; // whether to tick @ end cell (base class): copy the code @ class SettingItem; @ interface BasicCell: UITableViewCell // display the content according to the settingItem Model @ property (nonatomic, strong) SettingItem * item; // provides an external method for creating cells + (instancetype) cellWithTableView :( UITableView *) tableView; // method for setting the background image of each Group-(void) setIndexPath :( NSIndexPath *) indexPath rowCount :( NSInteger) rowCoun T; @ end @ interface BasicCell () // you must use strong here, because it is added to the view rather than contentView; // cell @ property (nonatomic, strong) in Arrow format) UIImageView * arrowView; // switch-format cell @ property (nonatomic, strong) UISwitch * switchView; // tick-format cell @ property (nonatomic, strong) UIImageView * cheakView; // cell @ property (nonatomic, weak) UILabel * labelView in label format; @ end // tensile image without deformation (generally, it is best to write a project as category) @ implementation UIImage (Resizable) + (UIIma Ge *) resizableWithImageName :( NSString *) imageName {UIImage * image = [UIImage imageNamed: imageName]; return [image stretchableImageWithLeftCapWidth: image. size. width/2 topCapHeight: image. size. height/2];} @ end @ implementation BasicCell # pragma mark-lazy loading of all controls-(UIImageView *) arrowView {if (_ arrowView = nil) {_ arrowView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "common_icon_arrow"];} Return _ arrowView;}-(UISwitch *) switchView {if (_ switchView = nil) {_ switchView = [[UISwitch alloc] init]; [_ switchView addTarget: self action: @ selector (switchChange :) forControlEvents: UIControlEventValueChanged];} return _ switchView;}-(UIImageView *) cheakView {if (_ cheakView = nil) {_ cheakView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "common_icon_checkmark"];} return _ c HeakView;}-(UILabel *) labelView {if (! _ LabelView) {UILabel * labelView = [[UILabel alloc] initWithFrame: self. bounds]; labelView. textAlignment = NSTextAlignmentCenter; labelView. textColor = [UIColor redColor]; [self. contentView addSubview: labelView]; _ labelView = labelView;} return _ labelView;} # pragma mark-switchChange-(void) switchChange :( UISwitch *) switchView {}-(instancetype) initWithStyle :( UITableViewCellStyle) style reuseIdentif Ier :( NSString *) reuseIdentifier {if (self = [super initWithStyle: style reuseIdentifier: reuseIdentifier]) {// you 'd better put the items you only need to set once in this method for execution, this method is executed only once when the cell is created, instead of self every time the model is assigned a value. detailTextLabel. font = [UIFont systemFontOfSize: 14]; self. backgroundColor = [UIColor clearColor]; self. backgroundView = [[UIImageView alloc] init]; self. selectedBackgroundView = [[UIImageView alloc] init];} return self;} + (Instancetype) cellWithTableView :( UITableView *) tableView {static NSString * ID = @ "cell"; BasicCell * cell = [tableView dequeueReusableCellWithIdentifier: ID]; if (! Cell) {// here you must use self cell = [[self alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: ID];} return cell;}-(void) setItem :( SettingItem *) item {_ item = item; // set data [self setUpData]; // set the model [self setUpRightView];}-(void) setUpData {self. textLabel. text = _ item. title; self. detailTextLabel. text = _ item. subTitle; self. imageView. image = _ item. image;}-(void) setUpRightView {if ([_ item isKi NdOfClass: [ArrowItem class]) {// arrow self. accessoryView = self. arrowView;} else if ([_ item isKindOfClass: [BadgeItem class]) {// badgeView self. accessoryView = self. badgeView;} else if ([_ item isKindOfClass: [SwitchItem class]) {// switch self. accessoryView = self. switchView;} else if ([_ item isKindOfClass: [LabelItem class]) {// Label LabelItem * labelItem = (LabelItem *) _ item; UILabel * label = sel F. labelView; label. text = labelItem. text;} else if ([_ item isKindOfClass: [CheakItem class]) {// tick CheakItem * cheakItem = (CheakItem *) _ item; if (cheakItem. isCheak) {self. accessoryView = self. cheakView;} else {self. accessoryView = nil ;}} else {[_ labelView removeFromSuperview]; _ labelView = nil; self. accessoryView = nil; }}- (void) setIndexPath :( NSIndexPath *) indexPath rowCount :( NSInteger) ro WCount {UIImageView * bgView = (UIImageView *) self. backgroundView; UIImageView * selBgView = (UIImageView *) self. selectedBackgroundView; if (rowCount = 1) {// only one bgView row exists. image = [UIImage resizableWithImageName: @ "common_card_background"]; selBgView. image = [UIImage resizableWithImageName: @ "common_card_background_highlighted"];} else if (indexPath. row = 0) {// The top cell bgView. image = [UIImage resiza BleWithImageName: @ "common_card_top_background"]; selBgView. image = [UIImage resizableWithImageName: @ "common_card_top_background_highlighted"];} else if (indexPath. row = rowCount-1) {// bottom bgView. image = [UIImage resizableWithImageName: @ "common_card_bottom_background"]; selBgView. image = [UIImage resizableWithImageName: @ "common_card_bottom_background_highlighted"];} else {// intermediate bgView. image = [U IImage resizableWithImageName: @ "common_card_middle_background"]; selBgView. image = [UIImage resizableWithImageName: @ "align"] ;}# pragma mark-write here to align-(void) layoutSubviews {[super layoutSubviews]; _ labelView. frame = self. bounds;} so far, all of them have been extracted. You may want to ask what to do if the child class is very long but not the parent class? So we can use the property inherited from OC. The subclass only needs to override the method of the parent class. Write a few controllers at will and inherit from BasicSettingViewController my interface-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. [self setUpNav]; [self setUpGroup0]; [self setUpGroup1]; [self setUpGroup2]; [self setUpGroup3];}-(void) setUpNav {UIBarButtonItem * setting = [[UIBarButtonItem alloc] initWithTitle: @ "set" style: UIBarButtonItemStylePlain target: self action: @ selector (setting)]; NSMutableDictionary * dict = [NSMutableDictionary dictionary]; dict [NSForegroundColorAttributeName] = [UIColor blackColor]; [setting setTitleTextAttributes: dict forState: UIControlStateNormal. navigationItem. rightBarButtonItem = setting;} # pragma mark-settings-(void) setting {SettingViewController * setVc = [[SettingViewController alloc] init]; [self. navigationController pushViewController: setVc animated: YES];}-(void) setUpGroup0 {// new friend ArrowItem * friend = [ArrowItem itemWithTitle: @ "New friend" image: [UIImage imageNamed: @ "new_friend"]; GroupItem * group = [[GroupItem alloc] init]; group. items = @ [friend]; [self. groups addObject: group];}-(void) setUpGroup1 {// my album ArrowItem * album = [ArrowItem itemWithTitle: @ "my album" image: [UIImage imageNamed: @ "album"]; album. subTitle = @ "(12)"; // my favorites ArrowItem * collect = [ArrowItem itemWithTitle: @ "My favorites" image: [UIImage imageNamed: @ "collect"]; collect. subTitle = @ "(0)"; // like ArrowItem * like = [ArrowItem itemWithTitle: @ "like" image: [UIImage imageNamed: @ "like"]; like. subTitle = @ "(0)"; GroupItem * group = [[GroupItem alloc] init]; group. items = @ [album, collect, like]; [self. groups addObject: group];}-(void) setUpGroup2 {// Weibo payment ArrowItem * pay = [ArrowItem itemWithTitle: @ "Weibo payment" image: [UIImage imageNamed: @ "pay"]; // personalized ArrowItem * vip = [ArrowItem itemWithTitle: @ "personalized" image: [UIImage imageNamed: @ "vip"]; vip. subTitle = @ "Weibo source, skin, cover map"; GroupItem * group = [[GroupItem alloc] init]; group. items = @ [pay, vip]; [self. groups addObject: group];}-(void) setUpGroup3 {// my QR code ArrowItem * card = [ArrowItem itemWithTitle: @ "My QR code" image: [UIImage imageNamed: @ "card"]; // draft box ArrowItem * draft = [ArrowItem itemWithTitle: @ "draft box" image: [UIImage imageNamed: @ "draft"]; groupItem * group = [[GroupItem alloc] init]; group. items = @ [card, draft]; [self. groups addObject: group];}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {ProfileCell * cell = [ProfileCell cellWithTableView: tableView]; groupItem * group = self. groups [indexPath. section]; SettingItem * item = group. items [indexPath. row]; cell. item = item; [cell setIndexPath: indexPath rowCount: group. items. count]; return cell;} copy the code because the cell and the parent class are not the same, so the cell should inherit from BasicCell and rewrite the copy code-(instancetype) initWithStyle :( UITableViewCellStyle) style reuseIdentifier :( NSString *) reuseIdentifier {if (self = [super initWithStyle: style reuseIdentifier: reuseIdentifier]) {self. detailTextLabel. font = [UIFont systemFontOfSize: 12];} return self;} // It must be rewritten in this method. Otherwise, it may be useless. This is called when the loading is complete and will be displayed soon, the loaded frame is 100% correct-(void) layoutSubviews {[super layoutSubviews]; CGRect frame = self. detailTextLabel. frame; frame. origin. x = CGRectGetMaxX (self. textLabel. frame) + 5; self. detailTextLabel. frame = frame;} set the page-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. // Add group 0th [self setUpGroup0]; // Add group 1st [self setUpGroup1]; // Add Group 2nd [self setUpGroup2]; // Add group 3rd [self setUpGroup3];}-(void) setUpGroup0 {// account Management ArrowItem * account = [ArrowItem itemWithTitle: @ "account Management"]; // account. badgeValue = @ "8"; GroupItem * group = [[GroupItem alloc] init]; group. items = @ [account]; [self. groups addObject: group];}-(void) setUpGroup1 {// alert and notification ArrowItem * notice = [ArrowItem itemWithTitle: @ "my album"]; // set ArrowItem * setting = [ArrowItem itemWithTitle: @ ""]; setting. destVc = [CommonViewController class]; // privacy and security ArrowItem * secure = [ArrowItem itemWithTitle: @ "privacy and security"]; GroupItem * group = [[GroupItem alloc] init]; group. items = @ [notice, setting, secure]; [self. groups addObject: group];}-(void) setUpGroup2 {// feedback ArrowItem * suggest = [ArrowItem itemWithTitle: @ "feedback"]; // about Weibo ArrowItem * about = [ArrowItem itemWithTitle: @ ""]; GroupItem * group = [[GroupItem alloc] init]; group. items = @ [suggest, about]; [self. groups addObject: group];}-(void) setUpGroup3 {// Account Management LabelItem * layout = [[LabelItem alloc] init]; layout. text = @ "Exit Current Account"; GroupItem * group = [[GroupItem alloc] init]; group. items = @ [layout]; [self. groups addObject: group];} General setting interface-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. // Add group 0th [self setUpGroup0]; // Add group 1st [self setUpGroup1]; // Add Group 2nd [self setUpGroup2]; // Add group 3rd [self setUpGroup3]; [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (refresh :) name: @ "FontSizeChangeNote" object: nil];}-(void) refresh :( NSNotification *) notification {_ fontSize. subTitle = notification. userInfo [FontSizeKey]; [self. tableView reloadData];}-(void) setUpGroup0 {// read mode SettingItem * read = [SettingItem itemWithTitle: @ "read mode"]; read. subTitle = @ "Graph mode"; // font size SettingItem * fontSize = [SettingItem itemWithTitle: @ "font size"]; _ fontSize = fontSize; NSString * fontSizeStr = [[NSUserDefaults standardUserDefaults] objectForKey: FontSizeKey]; if (fontSizeStr = nil) {fontSizeStr = @ "medium";} fontSize. subTitle = fontSizeStr; fontSize. destVc = [FontViewController class]; // display remarks SwitchItem * remark = [SwitchItem itemWithTitle: @ "show remarks"]; GroupItem * group = [[GroupItem alloc] init]; group. items = @ [read, fontSize, remark]; [self. groups addObject: group];}-(void) setUpGroup1 {// image quality ArrowItem * quality = [ArrowItem itemWithTitle: @ "Image quality"]; groupItem * group = [[GroupItem alloc] init]; group. items = @ [quality]; [self. groups addObject: group];}-(void) setUpGroup2 {// sound SwitchItem * sound = [SwitchItem itemWithTitle: @ "sound"]; groupItem * group = [[GroupItem alloc] init]; group. items = @ [sound]; [self. groups addObject: group];}-(void) setUpGroup3 {// multilingual environment SettingItem * language = [SettingItem itemWithTitle: @ "multilingual environment"]; language. subTitle = @ "follow system"; GroupItem * group = [[GroupItem alloc] init]; group. items = @ [language]; [self. groups addObject: group];} font size page-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. [self setUpGroup0];}-(void) setUpGroup0 {# pragma mark-this method can eliminate the circular warning in the block // large _ weak typeof (self) weakSelf = self; cheakItem * _ weak big = [CheakItem itemWithTitle: @ ""]; big. option = ^ {[weakSelf selItem: big] ;}; // medium CheakItem * _ weak middle = [CheakItem itemWithTitle: @ "medium"]; middle. option = ^ {[weakSelf selItem: middle] ;}; _ selCheakItem = middle; // small CheakItem * _ weak small = [CheakItem itemWithTitle: @ "small"]; small. option = ^ {[weakSelf selItem: small] ;}; GroupItem * group = [[GroupItem alloc] init]; group. headTitle = @ "Upload image quality"; group. items = @ [big, middle, small]; [self. groups addObject: group]; // by default, the selected item [self setUpSelItem: middle];}-(void) setUpSelItem :( CheakItem *) item {NSString * fontSizeStr = [[NSUserDefaults standardUserDefaults] objectForKey: FontSizeKey]; if (fontSizeStr = nil) {[self selItem: item]; return ;} for (GroupItem * group in self. groups) {for (CheakItem * item in group. items) {if ([item. title isEqualToString: fontSizeStr]) {[self selItem: item] ;}}}- (void) selItem :( CheakItem *) item {_ selCheakItem. isCheak = NO; item. isCheak = YES; _ selCheakItem = item; [self. tableView reloadData]; // storage [[NSUserDefaults standardUserDefaults] setObject: item. title forKey: FontSizeKey]; [[NSUserDefaults standardUserDefaults] synchronize]; // send a notification [[nsicationicationcenter defacenter center] postNotificationName: @ "FontSizeChangeNote" object: nil userInfo: @ {FontSizeKey: item. title}];}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.