貓貓學IOS(十二)UI之UITableView學習(上)LOL英雄聯盟練習,iosuitableview

來源:互聯網
上載者:User

貓貓學IOS(十二)UI之UITableView學習(上)LOL英雄聯盟練習,iosuitableview

貓貓分享,必須精品

素材代碼地址:http://blog.csdn.net/u013357243/article/details/44706671
原文地址:http://blog.csdn.net/u013357243?viewmode=contents

先看



原始碼NYViewController的代碼
//ps:建立iOS交流學習群:304570962 可以加貓貓QQ:1764541256 或則znycat 讓我們一起努力學習吧。 原文:http://blog.csdn.net/u013357243?viewmode=contents//  NYViewController.m//  06 - lol英雄聯盟////  Created by apple on 15-3-28.//  Copyright (c) 2015年 znycat. All rights reserved.//#import "NYViewController.h"#import "NYHero.h"@interface NYViewController () <UITableViewDataSource,UITableViewDelegate>@property (strong,nonatomic) UITableView *tableView;@property (strong,nonatomic) NSArray *heros;@end@implementation NYViewController-(NSArray *)heros{    if (_heros == nil)_heros = [NYHero heros];    return _heros;}/**懶載入tableView */-(UITableView *)tableView{    if (_tableView == nil) {        //表格控制項在建立時必須指定樣式        //        UITableViewStylePlain 平板格式        //        UITableViewStyleGrouped分組格式        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];        //添加資料來源->加入協議        _tableView.dataSource = self;        /*設定行高方法有兩種,代理方法的優先順序比setRowHeight的優先順序高。            應用情境,很多應用程式,每一行高度是不一樣的,例如:新浪微博        *///        _tableView.rowHeight = 80;//第一種        _tableView.delegate = self;//第二種,要設定代理,-》協議 -》實現代理方法        [self.view addSubview:_tableView];    }    return _tableView;}- (void)viewDidLoad{    [super viewDidLoad];    [self tableView];}#pragma mark - 資料來源方法/**每個分組中的資料總數*/-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.heros.count;}/**告訴表格,每個儲存格的明細*/-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    /*     UITableViewCellStyleDefault        預設類型 標題+可選映像     UITableViewCellStyleValue1         標題+明細+映像     UITableViewCellStyleValue2         不顯示映像,標題+明細     UITableViewCellStyleSubtitle       標題+明細+映像     */    NSLog(@"表格行明細 %d",indexPath.row);    //static 靜態變數,能夠保證系統為變數在記憶體中只分配一次記憶體空間    //靜態變數,一旦建立,就不會被釋放,只有在應用程式在銷毀是,才會釋放。    static NSString *ID = @"cell";    //1,去緩衝池尋找可重複用得儲存格    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    //2,如果沒找到    if (cell == nil) {//        NSLog(@"執行個體化儲存格");       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;    }    //設定儲存格內容//    取出英雄對象    NYHero *hero = self.heros[indexPath.row];    //設定標題 :英雄名字    cell.textLabel.text = hero.name;    //設定詳細內容: 英雄描述    cell.detailTextLabel.text = hero.intro;    //設定英雄表徵圖    cell.imageView.image = [UIImage imageNamed:hero.icon];    // 設定右邊的箭頭    // 1> UITableViewCellAccessoryDisclosureIndicator 箭頭,可以"提示"使用者,當前行是可以點擊的,通常選中行,會跳到新的頁面    // 2> UITableViewCellAccessoryCheckmark 對號,通常提示使用者該行資料設定完畢,使用的比較少    // 3> UITableViewCellAccessoryDetailButton 按鈕,通常點擊按鈕可以做獨立的操作,例如alertView    //    點擊按鈕並不會選中行    // 4> UITableViewCellAccessoryDetailDisclosureButton 按鈕+箭頭,各自操作//      cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;    // 指定右側的自訂視圖    /**     通常accessoryType提供的類型不能滿足時,才會使用自訂控制項     但是需要自行添加監聽方法,通常用在自訂cell,不要寫在視圖控制器中!!!     自訂控制項的事件觸發,同樣不會影響表格行的選中!     *///    UISwitch *switcher = [[UISwitch alloc] init];//    //添加監聽方法//    [switcher addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];//   //    cell.accessoryView = switcher;    return cell;}-(void)switchChanged:(UISwitch *) sender{     NSLog(@"%s %@", __func__, sender);}#pragma mark - 實現代理方法 (行高設定)/**設定行高,比setRowHeight優先順序高*/-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 70;}// 取消選中某一行,極少用,極容易出錯!// didDeselectRowAtIndexPath// didSelectRowAtIndexPath- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"%s %@", __func__, indexPath);}/**選中了某一行*/-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"%s   %@",__func__, indexPath);}/**  accessoryType為按鈕時,UITableViewCellAccessoryDetailButton點擊右側按鈕的監聽方法        此方法不會觸發行選中,跟行選中各自獨立        只是為accessoryType服務,對自訂控制項不響應 */- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{    NSLog(@"%s %@", __func__, indexPath);}@end
模型的代碼的代碼
////  NYHero.h//  06 - lol英雄聯盟////  Created by apple on 15-3-28.//  Copyright (c) 2015年 znycat. All rights reserved.//#import <Foundation/Foundation.h>@interface NYHero : NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *icon;@property (nonatomic, copy) NSString *intro;-(instancetype) initWithDict:(NSDictionary *)dict;+(instancetype) heroWithDict:(NSDictionary *)dict;+(NSArray *) heros;@end

m實現檔案

////  NYHero.m//  06 - lol英雄聯盟////  Created by apple on 15-3-28.//  Copyright (c) 2015年 znycat. All rights reserved.//#import "NYHero.h"@implementation NYHero-(instancetype)initWithDict:(NSDictionary *)dict{    self = [super init];    if (self) {        [self setValuesForKeysWithDictionary:dict];    }    return self;}+(instancetype)heroWithDict:(NSDictionary *)dict{    return [[self alloc] initWithDict:dict];}+(NSArray *)heros{    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil]];    NSMutableArray *arrayM = [NSMutableArray array];    for (NSDictionary *dict in array) {//        這才是應該寫的        [arrayM addObject:[self heroWithDict:dict]];    }    return arrayM;}@end
貓貓犯二了——關於字典模型的類方法

貓貓今天犯二了,字典初始化方法中又一個竟然這麼寫了,不多說了,大家看看引以為見吧

@implementation NYHero-(instancetype)initWithDict:(NSDictionary *)dict{    self = [super init];    if (self) {        [self setValuesForKeysWithDictionary:dict];    }    return self;}+(instancetype)heroWithDict:(NSDictionary *)dict{    return [[self alloc] initWithDict:dict];}+(NSArray *)heros{    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil]];    NSMutableArray *arrayM = [NSMutableArray array];    for (NSDictionary *dict in array) {        //這裡我原來是這麼寫的(注釋的是錯誤的)        //arrayM =[self heroWithDict:dict];//        這才是應該寫的        [arrayM addObject:[self heroWithDict:dict]];    }

這二犯的,關鍵用debug你看的話,他會有內容,但是因為名稱差不太多,應該是heros傳回值是一堆對象組成的數組,結果你NSLog就發現,他丫的就返回一個地址值,很奇怪,開始我還以為是xcode大姨媽來了呢。。。事實證明,代碼裡面無秘密啊。
——————話說這好像是貓貓寫的代碼中注釋最少的耶。。。

代理模式階段性小結

監聽控制項的某些事件
使用代理模式,是為了在程式直接“解耦”。

表格可以顯示非常豐富的資料,為了達到這一結果,設定表格的“資料來源”。
@required 必須實現的方法。
@optional 可選的實現方法->不強求實現->如果實現了能得到特殊的效果,如果不實現,也不影響程式的正常運行——能夠增加控制項的靈活度。

代理階段性小結:(怎麼用)
1,遵守協議,預先定義好方法,不實現,具體的實現工作由代理負責。
<控制項的名字+DataSource> 定義的與資料有關的方法。
<控制項的名字+Delegate> 定義的與事件有關的方法(通常用來監聽控制項事件)。

2,代理方法:
1> 方法名以控制項名稱開頭(沒有類首碼) -> 方便程式員書寫的時候,快速找到需要的協議方法。
2> 第一個參數是自己 -> 意味著在協議方法中,可以直接存取對象的屬性,或調用方法。
3> 代理方法的傳回值 -> 控制器向控制項(委託)發送資料 。

cell——UITableViewCell的注意點

緩衝池的運用(老闆本,後面會有新的東西更新,但是需要瞭解,看別人程式時候別不認識了——還有方便理解)

    //1,去緩衝池尋找可重複用得儲存格    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    //2,如果沒找到    if (cell == nil) {//        NSLog(@"執行個體化儲存格");       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;    }    //3設定儲存格內容    //4return cell

初始化的時候需要指定cell的樣式
UITableViewCellStyleDefault 預設類型 標題+可選映像
UITableViewCellStyleValue1 標題+明細+映像
UITableViewCellStyleValue2 不顯示映像,標題+明細
UITableViewCellStyleSubtitle 標題+明細+映像

用法:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
指定cell右側的視圖

設定右邊的箭頭
1> UITableViewCellAccessoryDisclosureIndicator 箭頭,可以”提示”使用者,當前行是可以點擊的,通常選中行,會跳到新的頁面
2> UITableViewCellAccessoryCheckmark 對號,通常提示使用者該行資料設定完畢,使用的比較少
3> UITableViewCellAccessoryDetailButton 按鈕,通常點擊按鈕可以做獨立的操作,例如alertView

   點擊按鈕並不會選中行

4> UITableViewCellAccessoryDetailDisclosureButton 按鈕+箭頭,各自操作
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

指定右側的自訂視圖(例:選擇按鈕)

 通常accessoryType提供的類型不能滿足時,才會使用自訂控制項 但是需要自行添加監聽方法,通常用在自訂cell,不要寫在視圖控制器中!!! 自訂控制項的事件觸發,同樣不會影響表格行的選中!
    UISwitch *switcher = [[UISwitch alloc] init];    //添加監聽方法    [switcher addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];    cell.accessoryView = switcher;

ps:建立iOS交流學習群:304570962
可以加貓貓QQ:1764541256 或則znycat
讓我們一起努力學習吧。
原文:http://blog.csdn.net/u013357243?viewmode=contents

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.