UI 一一 数据刷新
来源:互联网 发布:360软件源ios10 编辑:程序博客网 时间:2024/04/30 05:12
效果图如下:
先介绍一下数据刷新:
数据刷新
- 添加数据
- 删除数据
- 更改数据
全局刷新方法(最常用)
[self.tableView reloadData];// 屏幕上的所有可视的cell都会刷新一遍
局部刷新方法
- 添加数据
NSArray *indexPaths = @[ [NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0] ];[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
- 删除数据
NSArray *indexPaths = @[ [NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0] ];[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
- 更新数据(没有添加和删除数据,仅仅是修改已经存在的数据)
NSArray *indexPaths = @[ [NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0] ];[self.tableView relaodRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
实现上面效果图的思路:
1. 整个界面是由橙色的UIView 和 UITableView组成的,所以在storyboard中用UIVIewController就行
2. 创建一个酒模型,用来给每一行cell设置数据.ZYWine
3. 创建一个继承UITableViewCell 的ZYWineCell 用来接收酒模型传来的数据
4. 实现tableView的数据源方法,给ZYWineCell 设置每一行数据
5. 当点击添加按钮,增加一行cell,点击更新按钮就修改cell的数据,点击删除按钮就删除一行cell
6. 通过对模型的修改来达到增,删,改操作
具体代码如下:
ZYWine文件
#import <Foundation/Foundation.h>/** 酒数据模型 */@interface ZYWine : NSObject/** 图片名 */@property (nonatomic, copy) NSString *image;/** 价格 */@property (nonatomic, copy) NSString *money;/** 酒名称 */@property (nonatomic, copy) NSString *name;@end@implementation ZYWine@end
ZYWineCell文件
#import <UIKit/UIKit.h>@class ZYWine;// 每一个cell@interface ZYWineCell : UITableViewCell/** 酒的数据模型 */@property(nonatomic,strong)ZYWine * wine;@end#import "ZYWine.h"@implementation ZYWineCell// 重写wine的set方法,把模型中的数据赋值进来- (void)setWine:(ZYWine *)wine{ _wine = wine; self.imageView.image = [UIImage imageNamed:wine.image]; self.textLabel.text = wine.name; self.detailTextLabel.text = [NSString stringWithFormat:@"¥%@",wine.money]; self.detailTextLabel.textColor = [UIColor orangeColor];}@end
Main.storyboard文件
ViewController文件
#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end#import "MJExtension.h"#import "ZYWineCell.h"#import "ZYWine.h"@interface ViewController () <UITableViewDataSource>@property (weak, nonatomic) IBOutlet UITableView *tableView;/** 酒模型数组,装的都是酒模型 */@property (strong,nonatomic) NSMutableArray *wineArray;@end@implementation ViewController// 懒加载- (NSMutableArray *)wineArray{ if (!_wineArray) { _wineArray = [ZYWine mj_objectArrayWithFilename:@"wine.plist"]; } return _wineArray;}- (void)viewDidLoad { [super viewDidLoad]; }#pragma -mark 按钮的点击方法/** 以后想要修改tableView上面显示的数据,要通过模型去决定 */- (IBAction)addBtn { //1. 修改模型 // 当点击添加按钮,增加一行cell ZYWine *wine = [[ZYWine alloc] init]; wine.image = @"newWine"; wine.money = @"55.5"; wine.name = @"女儿红"; // 把这个酒模型添加到wineArray的第一个中 [self.wineArray insertObject:wine atIndex:0]; NSArray *indexPaths = @[ [NSIndexPath indexPathForRow:0 inSection:0] ]; [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight]; }- (IBAction)removeBtn { // 因为每个cell的内容都是由模型(数据源)决定的 [self.wineArray removeObjectAtIndex:0]; // 刷新表格 NSArray *indexPaths = @[ [NSIndexPath indexPathForRow:0 inSection:0], ]; [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle]; }- (IBAction)updateBtn { // 更新数据 ZYWine *wine = self.wineArray[0]; wine.money = @"100"; // 全局刷新// [self.tableView reloadData]; /** 局部刷新: 只会刷新指定的那一行的cell reloadRowsAtIndexPaths: 传递一个indexPaths的数组 withRowAnimation: 刷新动画 */ NSArray *indexPaths = @[ [NSIndexPath indexPathForRow:0 inSection:0], ]; // 使用这个方法的前提: 包装模型数组中的个数不变,所以不可以使用这个方法进行增加和删除 [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; }#pragma -mark 实现数据源方法/** 有多少行数据 */- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.wineArray.count;}/** 每一行数据的内容 */- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"cellForRowAtIndexPath--%ld",indexPath.row); // 定义一个重用标识 static NSString *ID = @"wine"; ZYWineCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 判断缓存池中是否有可重用的cell,如果没有就创建 if (cell == nil) { cell = [[ZYWineCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } // 设置每一行数据 cell.wine = self.wineArray[indexPath.row]; return cell;}@end
注意:
以后想要修改tableView上面显示的数据,要通过模型去决定,因为每个cell的内容都是由模型(数据源)决定的,不可以直接通过一些方法拿到cell来修改cell的内容,因为有循环利用,可能会导致数据紊乱
阅读全文
0 0
- UI 一一 数据刷新
- UI 一一 UI基础知识
- UI高级 一一 画板
- UI 一一 基本控件认识
- UI 一一 常用控件-UIButton
- UI 一一 九宫格思想
- UI 一一 自定义批量删除
- UI 一一 UIApplication基本介绍
- Ui 一一 UIWindow基本介绍
- UI 一一 UITableView多组和单组数据展示案例
- iOS 一一 数据存储
- IOS开发UI基础--数据刷新
- 数据加载后刷新UI界面
- UI 一一 常用控件-UILabel,UIImageView
- UI 一一 UIScrollView的基本使用详解
- UI 一一 UIWebView的基本使用
- UI 一一 UIWebView的应用小案例
- 一一
- yarn的HA搭建
- UVA 540 Team Queue【queue的用法】
- 计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127)。不在范围内的不作统计。
- hud 2546饭卡(动态规划)
- JS实现单选、多选、全选、反选
- UI 一一 数据刷新
- RxSwift样例四 GeolocationExample
- 201708面试问题
- 实现一个位图
- 小球自由落体动态模拟(Position Based Simulation)
- 找出给定字符串中大写字符(即'A'-'Z')的个数
- Maven私服搭建以及Gradle使用私服的配置
- 使用Eclipse Maven创建Web4.0/Servlet4.0项目
- 还在用枚举?我早就抛弃了!(Android 注解详解)