当前位置: 移动技术网 > IT编程>移动开发>IOS > 详解iOS开发中UItableview控件的数据刷新功能的实现

详解iOS开发中UItableview控件的数据刷新功能的实现

2019年07月24日  | 移动技术网IT编程  | 我要评论

风暴之王的复仇 塞穆,峨眉髭蟾,夏和熙出柜

实现uitableview控件数据刷新
一、项目文件结构和plist文件

2015121591808961.png (872×310)

二、实现效果

1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作).

运行界面:

2015121591918849.png (320×479)

点击选中行:

2015121591936809.png (318×497)

修改数据后自动刷新:

2015121591959725.png (318×497)

三、代码示例

数据模型部分:

yyheros.h文件

复制代码 代码如下:

//
//  yyheros.h
//  10-英雄展示(数据刷新)
//
//  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文件
复制代码 代码如下:

//
//  yyheros.m
//  10-英雄展示(数据刷新)
//
//  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文件

复制代码 代码如下:

//
//  yyviewcontroller.m
//  10-英雄展示(数据刷新)
//
//  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


复制代码 代码如下:

@implementation yyviewcontroller

- (void)viewdidload
{
    [super viewdidload];
    //设置数据源
    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;
}
//每组每行的数据,设置cell
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
    //nslog(@"cellforrowatindexpath 修改的了 %d", indexpath.row);
    //1.去缓存中取
    static nsstring *identifier=@"hero";
    uitableviewcell *cell=[tableview dequeuereusablecellwithidentifier:identifier];
    //2.如果没有,那么就自己创建
    if (cell==nil) {
        nslog(@"chuangjiancell");
        cell=[[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:identifier];
    }
    //3.设置数据
    
    //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-数据刷新
//1.弹出窗口,拿到数据
//当某一行被选中的时候调用该方法
-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath
{
    //拿到改行的数据模型
    yyheros *hero=self.heros[indexpath.row];
    uialertview *alert=[[uialertview alloc]initwithtitle:@"修改数据" message:nil delegate:self cancelbuttontitle:@"取消" otherbuttontitles:@"确定", nil];
   
    //密码框形式的
    //alert.alertviewstyle=uialertviewstylesecuretextinput;
    alert.alertviewstyle=uialertviewstyleplaintextinput;
    uitextfield *text=[alert textfieldatindex:0];
    //把当前行的英雄数据显示到文本框中
    text.text=hero.name;
    alert.tag=indexpath.row;
    [alert show];
}
//2.修改数据,完成刷新操作
-(void)alertview:(uialertview *)alertview clickedbuttonatindex:(nsinteger)buttonindex
{
    //1.修改模型
    //如果选中的是取消,那么就返回,不做任何操作
    if (0==buttonindex) return;
    //否则就修改模型,刷新数据
    yyheros *hero=self.heros[alertview.tag];
   
    //拿到当前弹窗中的文本数据(已经修改后的数据)
    uitextfield *text=[alertview textfieldatindex:0];
    //用修改后的数据去修改模型
    hero.name=text.text;
  
    //2.刷新数据
    // 只要调用tableview的该方法就会自动重新调用数据源的所有方法
    // 会自动调用numberofsectionsintableview
    // 会自动调用numberofrowsinsection
    // 会自动调用cellforrowatindexpath
    //    [self.tableview reloaddata];
   
    // 刷新指定行
       nsindexpath *path = [nsindexpath indexpathforrow:alertview.tag insection:0];
        [self.tableview reloadrowsatindexpaths:@[path] withrowanimation:uitableviewrowanimationright];
    //如果不进行刷新会怎么样?(修改之后不会即时刷新,要等到重新对cell进行数据填充的时候才会刷新)
}
//隐藏状态栏
-(bool)prefersstatusbarhidden
{
    return yes;
}
@end


四、把常用的代码封装成一个带参数的宏

封装方法和代码:

复制代码 代码如下:

//
//  global.h
//  10-英雄展示(数据刷新)
//
//  created by apple on 14-5-29.
//  copyright (c) 2014年 itcase. all rights reserved.
//

#ifndef _0____________global_h
#define _0____________global_h

/**
 *  自定义带参数的宏
 */
#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


以后在需要使用的时候,只需要使用宏即可。

如在yyheros.m文件中使用yyinitm(hero)这一句代码可以代替下面的代码段:

复制代码 代码如下:

-(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];
}


五、注意点

1.刷新数据的两个步骤:

1)修改模型

2)刷新表格数据(可以全部刷新,也可以刷新指定的行)

2.在主控制器文件中,遵守了三个协议

分别是:

uitableviewdatasource,

uialertviewdelegate,

uitableviewdelegate

uitableview控件使用小结

一、uitableview的使用步骤

uitableview的使用就只有简单的三个步骤:

1.告诉一共有多少组数据

方法:

复制代码 代码如下:
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview;

2.告诉每组一共有多少行

方法:

复制代码 代码如下:
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section;

3.设置每组每行(cell)

方法:

复制代码 代码如下:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath;

二、使用说明

1.多少组数据和显示多少行通常是和数据息息相关的,在开发中数据通常存储在plist文件中,需要以一定的方式加载到项目中(模型)。

2.设置每组每行,说简单点就是设置tableview中得每个cell.

设置cell的步骤有三步:

(1)创建一个cell(需要考虑性能,对cell进行循环利用,注意缓存处理方式)

(2)为cell设置数据

(3)返回一个cell

设置cell有三种方式:

(1)使用系统提供的tableviewcell进行设置

(2)通过xib自定义tableviewcell,适用于长相一致的,如团购展示界面

(3)通过纯代码自定义tableviewcell,适用于有差距的,如表现为高度不一样,有的cell拥有某个属性,而有的cell中没有,如微博展示界面

三、自定义tableviewcell

1.通过xib文件自定义一个view的步骤

(1)新建一个xib文件,描述一个view的内部

(2)新建一个自定义的类,自定义的类需要继承自系统自带的view,继承自哪个类,取决于xib跟对象的class

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

(4)将xib的控件和自定义类的.m文件进行连线

(5)提供一个类的方法返回一个创建好的自定iview(屏蔽从xib加载的过程)

(6)提供一个模型属性让外界传递模型数据

(7)重写模型属性的setter方法,在这里讲模型数据展示到对应的子控件上面

2.通过代码方式自定义cell

(1)新建⼀一个继承自uitableviewcell的类

(2)重写initwithstyle:reuseidentifier:方法

添加所有需要显示的子控件(不需要设置子控件的数据和frame, 子控件要添加 到contentview中)

对子控件进行一次性的属性设置(有些属性只需要设置一次, 比如字体\固定的图片)

(3)提供2个模型

数据模型: 存放文字数据\图片数据

frame模型: 存放数据模型\所有子控件的frame\cell的高度

(4)cell拥有一个frame模型(不要直接拥有数据模型)

(5)重写frame模型属性的setter方法: 在这个方法中设置子控件的显示数据和frame

(6)frame模型数据的初始化已经采取懒加载的方式(每一个cell对应的frame模型数据只加载一 次)

四、使用代理的步骤

(1)先搞清楚谁是谁的代理(delegate)

(2)定义代理协议,协议名称的命名规范:控件类名 + delegate

(3)定义代理方法

代理方法一般都定义为@optional

代理方法名都以控件名开头

代理方法至少有1个参数,将控件本身传递出去

(4)设置代理(delegate)对象 (⽐比如myview.delegate = xxxx;)

代理对象遵守协议

代理对象实现协议里面该实现的方法

(5)在恰当的时刻调⽤代理对象(delegate)的代理方法,通知代理发生了什么事情

 (在调⽤之前判断代理是否实现了该代理⽅方法)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网