【iOS開發之旅】UITableView樣本-LOL英雄列表

來源:互聯網
上載者:User

標籤:

UITableView樣本-LOL英雄列表
運行效果
     
  

CZHero.h

////  CZHero.h//  04-UITableView樣本-載入plist檔案////  Created by ChenQianPing on 16/1/21.//  Copyright © 2016年 chenqp. All rights reserved.//#import <Foundation/Foundation.h>@interface CZHero : NSObject// 頭像圖片@property (nonatomic,copy) NSString *icon;// 英雄簡介@property (nonatomic,copy) NSString *intro;// 英雄名稱@property (nonatomic,copy) NSString *name;- (instancetype)initWithDict:(NSDictionary *)dict;+ (instancetype)heroWithDict:(NSDictionary *)dict;@end

CZHero.m

////  CZHero.m//  04-UITableView樣本-載入plist檔案////  Created by ChenQianPing on 16/1/21.//  Copyright © 2016年 chenqp. All rights reserved.//#import "CZHero.h"@implementation CZHero- (instancetype)initWithDict:(NSDictionary *)dict{    if (self = [super init]){        [self setValuesForKeysWithDictionary:dict];    }    return self;}+ (instancetype)heroWithDict:(NSDictionary *)dict{    return [[self alloc] initWithDict:dict];}@end

ViewController.m

////  ViewController.m//  04-UITableView樣本-載入plist檔案////  Created by ChenQianPing on 16/1/21.//  Copyright © 2016年 chenqp. All rights reserved.//#import "ViewController.h"#import "CZHero.h"@interface ViewController () <UITableViewDataSource,UITableViewDelegate>// 儲存英雄的集合@property (nonatomic,strong)NSArray *heros;@property (weak, nonatomic) IBOutlet UITableView *tableView;@end@implementation ViewController#pragma mark - 代理方法//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath//{//    int rowNum = indexPath.row;//    if(rowNum % 2 == 0){//        return 60;//    } else {//        return 120;//    }//    //}// 監聽行被選中的代理方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // 擷取當前被選中的這行的英雄的名稱    CZHero *hero = self.heros[indexPath.row];        //    NSLog(@"%ld", (long)indexPath.row);    // 省去了繁瑣的代理方法,原來的控制項點擊每個功能按鈕調用方法 還得調用代理方法 要不然就是自己封裝一下,現在好了 由一個控制器來管理 操作方便了些 而且每個功能鍵都很清晰,點擊調用的方法都寫在block回調中這樣方便了很多不是嗎? 而且將原來的兩個控制項合二為一。我們可以自行再次對其封裝 使用會更加方便。        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"編輯英雄" message:hero.name preferredStyle:UIAlertControllerStyleAlert];    __weak typeof(alertController) weakAlert = alertController;    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"點擊取消");    }]];    [alertController addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"點擊了確定按鈕--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);        // 更新資料        // 1.擷取使用者文字框中輸入的內容        NSString *name = [weakAlert.textFields.firstObject text];        // 2.找到對應的英雄模型//        CZHero *hero = self.heros[indexPath.row];        // 3.修改英雄模型的name        hero.name = name;        // 4.重新整理TableView(重新重新整理資料的意思就是重新調用UITableView的資料來源對象中的那些資料來源方法)        // reloadData表示重新整理整個tableView        // [self.tableView reloadData];                // 局部重新整理,重新整理指定的行        // 建立一個行對象        NSIndexPath *idxPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];        [self.tableView reloadRowsAtIndexPaths:@[idxPath] withRowAnimation:UITableViewRowAnimationLeft];                    }]];        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {        NSLog(@"添加一個textField就會調用 這個block");        textField.text = hero.name;    }];        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {        NSLog(@"添加一個textField就會調用 這個block");        textField.text = hero.intro;    }];    // 由於它是一個控制器 直接modal出來就好了    [self presentViewController:alertController animated:YES completion:nil];}#pragma mark - 懶載入資料// 一般的資料,在從檔案或資料庫中讀取後,會用到多次,這時候要養成懶載入資料的習慣,以提高程式的效能,其實你不使用懶載入,從使用者來說,如果資料量小,沒什麼影響,但如果資料量大,使用懶載入資料就有明顯的優勢了。- (NSArray *)heros{    if (_heros == nil) {        // 1.獲得plist的全路徑        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];        // 2.載入數組        NSArray *arraryDict = [NSArray arrayWithContentsOfFile:path];        // 3.將arraryDict裡面的所有字典轉成模型對象,放到新的數組中        NSMutableArray *arrayModels = [NSMutableArray array];        for (NSDictionary *dict in arraryDict) {            // 3.1.建立模型對象            CZHero *model = [CZHero heroWithDict:dict];            // 3.2.添加模型對象到數組中            [arrayModels addObject:model];        }        // 4.賦值        _heros = arrayModels;    }    return _heros;}#pragma mark - 資料來源方法- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{//    NSLog(@"numberOfSectionsInTableView");    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{//    NSLog(@"numberOfRowsInSection--組索引:%ld",(long)section);    return self.heros.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{//    NSLog(@"cellForRowAtIndexPath---組索引:%ld,行索引:%ld",(long)indexPath.section,(long)indexPath.row);    // 1.擷取模型資料    CZHero *model = self.heros[indexPath.row];        // 2. 建立儲存格//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];    // 因為每次都建立一個儲存格效率比較低,所以要對儲存格進行重用    // 儲存格重用的基本思路就是:    // 1> 在建立儲存格的時候指定一個"重用ID"    // 2> 當需要一個新的儲存格的時候,先去"緩衝池"中根據"重用ID"是否有可用的儲存格    // ** 如果有,則直接從緩衝池中取出這個儲存格,進行使用(修改這個儲存格中顯示的的資料樣式)    // ** 如果沒有需要的儲存格,此時只能重新建立一個儲存格了.        // 聲明一個重用ID    // 英雄儲存格重用ID    static NSString *ID = @"hero_cell";        // 根據這個重用ID去"緩衝池"中尋找對應的Cell    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];        // 判斷,如果沒有手到可用的cell,那麼重新建立一個    if (cell == nil){//        NSLog(@"=========組索引:%ld,行索引:%ld",(long)indexPath.section,(long)indexPath.row);        // 建立一個新的儲存格        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];    }    // 3. 把模型資料設定給儲存格        // 設定英雄表徵圖    cell.imageView.image = [UIImage imageNamed:model.icon];    // 設定英雄姓名    cell.textLabel.text = model.name;    // 設定英雄描述    cell.detailTextLabel.text = model.intro;            // 要在儲存格的最右邊顯示一個小箭頭,所以要設定儲存格對象的某個屬性    // 通常accessoryType提供的類型不能滿足時,才會使用自訂控制項    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;        // 可以自訂儲存格右邊的accessory//    cell.accessoryView = [[UISwitch alloc] init];        // 輸出目前的儲存格的地址    NSLog(@"%p-----行索引:%d",cell,indexPath.row);        // 4.返回儲存格    return cell;}// 隱藏頂部狀態列- (BOOL)prefersStatusBarHidden{    return YES;}- (void)viewDidLoad {    [super viewDidLoad];    // 如果每行的行高是一樣的,那麼通過rowHeight統一設定行高效率更高    self.tableView.rowHeight = 60;    //    // 設定分割線的顏色//    self.tableView.separatorColor = [UIColor redColor];        // 對於行的行高不一樣的情況,無法通過tableView.rowHeight來實現    // 此時,只能通過一個代理方法來實現。}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

 



【iOS開發之旅】UITableView樣本-LOL英雄列表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.