[Basic iOS control and basic ios Control

Source: Internet
Author: User

[Basic iOS control and basic ios Control
A. requirement 1. based on the LOL hero list, add the real-time modification feature to it. 2. use UIAlertView3. global reloadData4. partial refresh 1 // 2 // Hero. h 3 // LOLHero 4 // 5 // Created by hellovoidworld on 14/12/1. 6 // Copyright (c) 2014 hellovoidworld. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 @ interface Hero: NSObject12 13 @ property (nonatomic, copy) NSString * icon; 14 @ property (nonatomic, copy) NSString * intro; 15 @ property (nonatomic, copy) NSString * name; 16 17-(instancetype) initWithDictionary :( NSDictionary *) dictionary; 18 + (instancetype) heroWithDictionary :( NSDictionary *) dictionary; 19 + (instancetype) hero; 20 21 @ end 

1 // 2 // Hero. m 3 // LOLHero 4 // 5 // Created by hellovoidworld on 14/12/1. 6 // Copyright (c) 2014 hellovoidworld. all rights reserved. 7 // 8 9 # import "Hero. h "10 11 @ implementation Hero12 13-(instancetype) initWithDictionary :( NSDictionary *) dictionary {14 if (self = [super init]) {15 // self. icon = dictionary [@ "icon"]; 16 // self. intro = dictionary [@ "intro"]; 17 // self. name = dictionary [@ "name"]; 18 // use KVC19 [self setValuesForKeysWithDictionary: dictionary]; 20} 21 22 return self; 23} 24 25 + (instancetype) heroWithDictionary :( NSDictionary *) dictionary {26 return [[self alloc] initWithDictionary: dictionary]; 27} 28 29 + (instancetype) hero {30 return [self heroWithDictionary: nil]; 31} 32 33 @ end
 
1 // 2 // ViewController. m 3 // LOLHero 4 // 5 // Created by hellovoidworld on 14/12/1. 6 // Copyright (c) 2014 hellovoidworld. all rights reserved. 7 // 8 9 # import "ViewController. h "10 # import" Hero. h "11 12 // The row click event can be responded only when UITableViewDelegate is observed. The page Click Event is handled by UIAlertViewDelegate. 13 @ interface ViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate> 14 15 // UITableView 16 @ property (weak, nonatomic) IBOutlet UITableView * tableView; 17 18 // All hero materials 19 @ property (nonatomic, strong) NSArray * heros; 20 21 @ end 22 23 @ implementation ViewController 24 25-(void) viewDidLoad {26 [super viewDidLoad]; 27 // Do any additional setup after loading the view, typically from a nib. 28 29 // set dataSource 30 self. tableView. dataSource = self; 31 32 // set the Row Height to 33 self. tableView. rowHeight = 60; 34 35 // set delegate 36 self. tableView. delegate = self; 37} 38 39-(void) didReceiveMemoryWarning {40 [super didReceiveMemoryWarning]; 41 // Dispose of any resources that can be recreated. 42} 43 44/** hide the status bar */45-(BOOL) prefersStatusBarHidden {46 return YES; 47} 48 49/** delayed loading of hero data */50-(NSArray *) heros {51 if (nil = _ heros) {52 NSArray * dictArray = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "heros. plist "ofType: nil]; 53 54 NSMutableArray * herosArray = [NSMutableArray array]; 55 for (NSDictionary * dict in dictArray) {56 Hero * hero = [Hero heroWithDictionary: dict]; 57 [herosArray addObject: hero]; 58} 59 60 _ heros = herosArray; 61} 62 63 return _ heros; 64} 65 66 # pragma mark-list method 67 68 // number of sections, default value: 1 69-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {70 return 1; 71} 72 73 // The number of rows in a specific section 74-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {75 return self. heros. count; 76} 77 78 79 // the content of a Specific Row is 80-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {81 Hero * hero = self. heros [indexPath. row]; 82 83 // use static to modify local variables, ensure that only one bucket is allocated 84 static NSString * hereCellId = @ "hero"; 85 86 // use a specific Identifier, when Viewing from the cache pool, the appropriate cell exists 87 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: hereCellId]; 88 89 if (nil = cell) {90 // specify the identifier 91 cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: hereCellId] during creation; 92} 93 94 // Title 95 cell. textLabel. text = hero. name; 96 97 // subtitle 98 cell. detailTextLabel. text = hero. intro; 99 100 // icon 101 cell. imageView. image = [UIImage imageNamed: hero. icon]; 102 103 return cell; 104} 105 106 # pragma mark-RESPONSE event 107 108/** click to select a row */109-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {110 // get model111 Hero * hero = self. heros [indexPath. row]; 112 113 // pop-up window 114 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "Modify hero name" message: nil delegate: nil cancelButtonTitle: @ "cancel" failed: @ "OK", nil]; 115 116 // set delegate. You can also specify 117 alertView In the init method. delegate = self; 118 119 // set the pop-up window style 120 alertView. alertViewStyle = UIAlertViewStylePlainTextInput; 121 122 // set the content of the pop-up window input box 123 [alertView textFieldAtIndex: 0]. text = hero. name; 124 125 // save index126 alertView. tag = indexPath. row; 127 128 // display the pop-up window 129 [alertView show]; 130} 131 132/** handle the pop-up window click "OK" button event */133-(void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex {134 // cancel 135 if (0 = buttonIndex) return; 136 137 // confirm 138 // Save the new name to model139 Hero * hero = self. heros [alertView. tag]; 140 hero. name = [alertView textFieldAtIndex: 0]. text; 141 142 // refresh data 143 // 1. global refresh 144 // [self. tableView reloadData]; 145 146 // 2. partial refresh 147 NSIndexPath * indexPath = [NSIndexPath indexPathForRow: alertView. tag inSection: 0]; 148 [self. tableView reloadRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationRight]; 149 150} 151 152 @ end

 

 

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.