欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解iOS開發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實(shí)現(xiàn)

 更新時(shí)間:2015年12月15日 09:23:01   作者:文頂頂  
這篇文章主要介紹了詳解iOS開發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實(shí)現(xiàn),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

實(shí)現(xiàn)UItableview控件數(shù)據(jù)刷新
一、項(xiàng)目文件結(jié)構(gòu)和plist文件

2015121591808961.png (872×310)

二、實(shí)現(xiàn)效果

1.說(shuō)明:這是一個(gè)英雄展示界面,點(diǎn)擊選中行,可以修改改行英雄的名稱(完成數(shù)據(jù)刷新的操作).

運(yùn)行界面:

2015121591918849.png (320×479)

點(diǎn)擊選中行:

2015121591936809.png (318×497)

修改數(shù)據(jù)后自動(dòng)刷新:

2015121591959725.png (318×497)

三、代碼示例

數(shù)據(jù)模型部分:

YYheros.h文件

復(fù)制代碼 代碼如下:

//
//  YYheros.h
//  10-英雄展示(數(shù)據(jù)刷新)
//
//  Created by apple on 14-5-29.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Global.h"

@interface YYheros : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *intro;

//-(instancetype)initWithDict:(NSDictionary *)dict;
//+(instancetype)herosWithDict:(NSDictionary *)dict;
YYinitH(hero)
@end


YYheros.m文件
復(fù)制代碼 代碼如下:

//
//  YYheros.m
//  10-英雄展示(數(shù)據(jù)刷新)
//
//  Created by apple on 14-5-29.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYheros.h"

@implementation YYheros
//-(instancetype)initWithDict:(NSDictionary *)dict
//{
//    if (self=[super init]) {
////        self.name=dict[@"name"];
////        self.icon=dict[@"icon"];
////        self.intro=dict[@"intro"];
//       
//        //使用KVC
//        [self setValuesForKeysWithDictionary:dict];
//    }
//    return self;
//}
//
//+(instancetype)herosWithDict:(NSDictionary *)dict
//{
//    return [[self alloc]initWithDict:dict];
//}
YYinitM(hero)
@end


主控制器 YYViewController.m文件

復(fù)制代碼 代碼如下:

//
//  YYViewController.m
//  10-英雄展示(數(shù)據(jù)刷新)
//
//  Created by apple on 14-5-29.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"
#import "YYheros.h"

@interface YYViewController ()<UITableViewDataSource,UIAlertViewDelegate,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableview;
@property(nonatomic,strong)NSArray *heros;
@end


復(fù)制代碼 代碼如下:

@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //設(shè)置數(shù)據(jù)源
    self.tableview.dataSource=self;
    self.tableview.delegate=self;
    self.tableview.rowHeight=60.f;
    NSLog(@"%d",self.heros.count);
}

#pragma mark -懶加載
-(NSArray *)heros
{
    if (_heros==nil) {
       
        NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil];
        NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];
       
        NSMutableArray *arrayM=[NSMutableArray array];
        for (NSDictionary *dict in temparray) {
            YYheros *hero=[YYheros herosWithDict:dict];
            [arrayM addObject:hero];
        }
        _heros=[arrayM mutableCopy];
    }
    return _heros;
}

#pragma mark- tableview的處理
//多少組
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.heros.count;
}
//每組每行的數(shù)據(jù),設(shè)置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"cellForRowAtIndexPath 修改的了 %d", indexPath.row);
    //1.去緩存中取
    static NSString *identifier=@"hero";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    //2.如果沒有,那么就自己創(chuàng)建
    if (cell==nil) {
        NSLog(@"chuangjiancell");
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //3.設(shè)置數(shù)據(jù)
    
    //3.1拿到該行的模型
    YYheros *hero=self.heros[indexPath.row];
    cell.textLabel.text=hero.name;
    cell.imageView.image=[UIImage imageNamed:hero.icon];
    cell.detailTextLabel.text=hero.intro;
    //4.返回cell
    return cell;
}

#pragma mark-數(shù)據(jù)刷新
//1.彈出窗口,拿到數(shù)據(jù)
//當(dāng)某一行被選中的時(shí)候調(diào)用該方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //拿到改行的數(shù)據(jù)模型
    YYheros *hero=self.heros[indexPath.row];
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"修改數(shù)據(jù)" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
   
    //密碼框形式的
    //alert.alertViewStyle=UIAlertViewStyleSecureTextInput;
    alert.alertViewStyle=UIAlertViewStylePlainTextInput;
    UITextField *text=[alert textFieldAtIndex:0];
    //把當(dāng)前行的英雄數(shù)據(jù)顯示到文本框中
    text.text=hero.name;
    alert.tag=indexPath.row;
    [alert show];
}
//2.修改數(shù)據(jù),完成刷新操作
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //1.修改模型
    //如果選中的是取消,那么就返回,不做任何操作
    if (0==buttonIndex) return;
    //否則就修改模型,刷新數(shù)據(jù)
    YYheros *hero=self.heros[alertView.tag];
   
    //拿到當(dāng)前彈窗中的文本數(shù)據(jù)(已經(jīng)修改后的數(shù)據(jù))
    UITextField *text=[alertView textFieldAtIndex:0];
    //用修改后的數(shù)據(jù)去修改模型
    hero.name=text.text;
  
    //2.刷新數(shù)據(jù)
    // 只要調(diào)用tableview的該方法就會(huì)自動(dòng)重新調(diào)用數(shù)據(jù)源的所有方法
    // 會(huì)自動(dòng)調(diào)用numberOfSectionsInTableView
    // 會(huì)自動(dòng)調(diào)用numberOfRowsInSection
    // 會(huì)自動(dòng)調(diào)用cellForRowAtIndexPath
    //    [self.tableview reloadData];
   
    // 刷新指定行
       NSIndexPath *path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
        [self.tableview reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];
    //如果不進(jìn)行刷新會(huì)怎么樣?(修改之后不會(huì)即時(shí)刷新,要等到重新對(duì)cell進(jìn)行數(shù)據(jù)填充的時(shí)候才會(huì)刷新)
}
//隱藏狀態(tài)欄
-(BOOL)prefersStatusBarHidden
{
    return YES;
}
@end


四、把常用的代碼封裝成一個(gè)帶參數(shù)的宏

封裝方法和代碼:

復(fù)制代碼 代碼如下:

//
//  Global.h
//  10-英雄展示(數(shù)據(jù)刷新)
//
//  Created by apple on 14-5-29.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#ifndef _0____________Global_h
#define _0____________Global_h

/**
 *  自定義帶參數(shù)的宏
 */
#define     YYinitH(name)   -(instancetype)initWithDict:(NSDictionary *)dict;\
+(instancetype)herosWithDict:(NSDictionary *)dict;


#define     YYinitM(name)  -(instancetype)initWithDict:(NSDictionary *)dict\
{\
    if (self=[super init]) {\
        [self setValuesForKeysWithDictionary:dict];\
    }\
    return self;\
}\
\
+(instancetype)herosWithDict:(NSDictionary *)dict\
{\
    return [[self alloc]initWithDict:dict];\
}\

#endif


以后在需要使用的時(shí)候,只需要使用宏即可。

如在YYheros.m文件中使用YYinitM(hero)這一句代碼可以代替下面的代碼段:

復(fù)制代碼 代碼如下:

-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self=[super init]) {
//        self.name=dict[@"name"];
//        self.icon=dict[@"icon"];
//        self.intro=dict[@"intro"];
       
        //使用KVC
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+(instancetype)herosWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}


五、注意點(diǎn)

1.刷新數(shù)據(jù)的兩個(gè)步驟:

1)修改模型

2)刷新表格數(shù)據(jù)(可以全部刷新,也可以刷新指定的行)

2.在主控制器文件中,遵守了三個(gè)協(xié)議

分別是:

UITableViewDataSource,

UIAlertViewDelegate,

UITableViewDelegate

UITableview控件使用小結(jié)

一、UITableview的使用步驟

UITableview的使用就只有簡(jiǎn)單的三個(gè)步驟:

1.告訴一共有多少組數(shù)據(jù)

方法:

復(fù)制代碼 代碼如下:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

2.告訴每組一共有多少行

方法:

復(fù)制代碼 代碼如下:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

3.設(shè)置每組每行(cell)

方法:

復(fù)制代碼 代碼如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

二、使用說(shuō)明

1.多少組數(shù)據(jù)和顯示多少行通常是和數(shù)據(jù)息息相關(guān)的,在開發(fā)中數(shù)據(jù)通常存儲(chǔ)在plist文件中,需要以一定的方式加載到項(xiàng)目中(模型)。

2.設(shè)置每組每行,說(shuō)簡(jiǎn)單點(diǎn)就是設(shè)置tableview中得每個(gè)cell.

設(shè)置cell的步驟有三步:

(1)創(chuàng)建一個(gè)cell(需要考慮性能,對(duì)cell進(jìn)行循環(huán)利用,注意緩存處理方式)

(2)為cell設(shè)置數(shù)據(jù)

(3)返回一個(gè)cell

設(shè)置cell有三種方式:

(1)使用系統(tǒng)提供的tableviewcell進(jìn)行設(shè)置

(2)通過xib自定義tableviewcell,適用于長(zhǎng)相一致的,如團(tuán)購(gòu)展示界面

(3)通過純代碼自定義tableviewcell,適用于有差距的,如表現(xiàn)為高度不一樣,有的cell擁有某個(gè)屬性,而有的cell中沒有,如微博展示界面

三、自定義tableviewcell

1.通過xib文件自定義一個(gè)view的步驟

(1)新建一個(gè)xib文件,描述一個(gè)view的內(nèi)部

(2)新建一個(gè)自定義的類,自定義的類需要繼承自系統(tǒng)自帶的view,繼承自哪個(gè)類,取決于xib跟對(duì)象的class

(3)新建類的類型最好跟xib的文件名保持一致

(4)將xib的控件和自定義類的.m文件進(jìn)行連線

(5)提供一個(gè)類的方法返回一個(gè)創(chuàng)建好的自定iview(屏蔽從xib加載的過程)

(6)提供一個(gè)模型屬性讓外界傳遞模型數(shù)據(jù)

(7)重寫模型屬性的setter方法,在這里講模型數(shù)據(jù)展示到對(duì)應(yīng)的子控件上面

2.通過代碼方式自定義cell

(1)新建⼀一個(gè)繼承自UITableViewCell的類

(2)重寫initWithStyle:reuseIdentifier:方法

添加所有需要顯示的子控件(不需要設(shè)置子控件的數(shù)據(jù)和frame, 子控件要添加 到contentView中)

對(duì)子控件進(jìn)行一次性的屬性設(shè)置(有些屬性只需要設(shè)置一次, 比如字體\固定的圖片)

(3)提供2個(gè)模型

數(shù)據(jù)模型: 存放文字?jǐn)?shù)據(jù)\圖片數(shù)據(jù)

frame模型: 存放數(shù)據(jù)模型\所有子控件的frame\cell的高度

(4)cell擁有一個(gè)frame模型(不要直接擁有數(shù)據(jù)模型)

(5)重寫frame模型屬性的setter方法: 在這個(gè)方法中設(shè)置子控件的顯示數(shù)據(jù)和frame

(6)frame模型數(shù)據(jù)的初始化已經(jīng)采取懶加載的方式(每一個(gè)cell對(duì)應(yīng)的frame模型數(shù)據(jù)只加載一 次)

四、使用代理的步驟

(1)先搞清楚誰(shuí)是誰(shuí)的代理(delegate)

(2)定義代理協(xié)議,協(xié)議名稱的命名規(guī)范:控件類名 + Delegate

(3)定義代理方法

代理方法一般都定義為@optional

代理方法名都以控件名開頭

代理方法至少有1個(gè)參數(shù),將控件本身傳遞出去

(4)設(shè)置代理(delegate)對(duì)象 (⽐比如myView.delegate = xxxx;)

代理對(duì)象遵守協(xié)議

代理對(duì)象實(shí)現(xiàn)協(xié)議里面該實(shí)現(xiàn)的方法

(5)在恰當(dāng)?shù)臅r(shí)刻調(diào)⽤代理對(duì)象(delegate)的代理方法,通知代理發(fā)生了什么事情

 (在調(diào)⽤之前判斷代理是否實(shí)現(xiàn)了該代理⽅方法)

相關(guān)文章

  • 詳解 iOS 系統(tǒng)中的視圖動(dòng)畫

    詳解 iOS 系統(tǒng)中的視圖動(dòng)畫

    這篇文章主要介紹了iOS 系統(tǒng)中的視圖動(dòng)畫的的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用ios開發(fā),感興趣的朋友可以了解下
    2021-02-02
  • iOS多控制器實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫

    iOS多控制器實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫

    這篇文章主要為大家詳細(xì)介紹了iOS多控制器實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 詳解IOS WebRTC的實(shí)現(xiàn)原理

    詳解IOS WebRTC的實(shí)現(xiàn)原理

    WebRTC,源自網(wǎng)頁(yè)實(shí)時(shí)通信(Web Real-Time Communication)縮寫,它是支持網(wǎng)頁(yè)瀏覽器進(jìn)行實(shí)時(shí)語(yǔ)音對(duì)話或視頻對(duì)話的技術(shù)。它為我們提供了視頻會(huì)議的核心技術(shù),包括音視頻的采集、編解碼、網(wǎng)絡(luò)傳輸、顯示等功能,并且還支持跨平臺(tái):windows,linux,mac,android,ios。
    2021-06-06
  • iOS的HTTP請(qǐng)求和請(qǐng)求回執(zhí)類用法小結(jié)

    iOS的HTTP請(qǐng)求和請(qǐng)求回執(zhí)類用法小結(jié)

    這里為大家整理了iOS的HTTP請(qǐng)求和請(qǐng)求回執(zhí)類用法小結(jié),包括發(fā)送請(qǐng)求的NSURLRequest、NSMutableURLRequest和負(fù)責(zé)回復(fù)的NSURLResponse類的常用方法和屬性,需要的朋友可以參考下
    2016-06-06
  • IOS 字符串常用處理詳細(xì)介紹

    IOS 字符串常用處理詳細(xì)介紹

    這篇文章主要介紹了IOS 字符串常用處理詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Objective-C中關(guān)于實(shí)例所占內(nèi)存的大小詳解

    Objective-C中關(guān)于實(shí)例所占內(nèi)存的大小詳解

    這篇文章主要給大家介紹了關(guān)于Objective-C中實(shí)例所占內(nèi)存的大小的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • iOS 開發(fā)常用宏總結(jié)

    iOS 開發(fā)常用宏總結(jié)

    這篇文章主要介紹了iOS 開發(fā)常用宏總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • iOS正確監(jiān)聽手機(jī)靜音鍵和側(cè)邊音量鍵的方法示例

    iOS正確監(jiān)聽手機(jī)靜音鍵和側(cè)邊音量鍵的方法示例

    這篇文章主要給大家介紹了關(guān)于iOS正確監(jiān)聽手機(jī)側(cè)邊音量鍵的相關(guān)資料,并且給大家分享了ios監(jiān)聽靜音鍵的示例代碼,文中介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-11-11
  • iOS實(shí)現(xiàn)雷達(dá)掃描效果

    iOS實(shí)現(xiàn)雷達(dá)掃描效果

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)雷達(dá)掃描效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • iOS如何用100行代碼實(shí)現(xiàn)簡(jiǎn)單的抽屜效果

    iOS如何用100行代碼實(shí)現(xiàn)簡(jiǎn)單的抽屜效果

    最近在網(wǎng)上看到一些抽屜效果,看起來(lái)很酷!很眩!但是,下不下來(lái)看代碼, 所以決定還是自己寫吧!!這篇文章通過近100行的代碼就實(shí)現(xiàn)了簡(jiǎn)單的抽屜效果,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。
    2016-10-10

最新評(píng)論