tableview 数据刷新
来源:互联网 发布:mac口红dare you和d 编辑:程序博客网 时间:2024/05/06 12:34
MJHero.h
#import <Foundation/Foundation.h>@interface MJHero : NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *icon;@property (nonatomic, copy) NSString *intro;+ (instancetype)heroWithDict:(NSDictionary *)dict;- (instancetype)initWithDict:(NSDictionary *)dict;@end
MJHero.m
#import "MJHero.h"@implementation MJHero+ (instancetype)heroWithDict:(NSDictionary *)dict{ return [[self alloc] initWithDict:dict];}- (instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; } return self;}@end
MJViewController.h
#import <UIKit/UIKit.h>@interface MJViewController : UIViewController@end
MJViewControl.m
#import "MJViewController.h"#import "MJHero.h"@interface MJViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>@property (nonatomic, strong) NSArray *heros;@property (weak, nonatomic) IBOutlet UITableView *tableView;@end@implementation MJViewController- (void)viewDidLoad{ [super viewDidLoad]; // 设置行高(每一行的高度一致) self.tableView.rowHeight = 60;}- (NSArray *)heros{ if (_heros == nil) { // 初始化 // 1.获得plist的全路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]; // 2.加载数组 NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中 NSMutableArray *heroArray = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { // 3.1.创建模型对象 MJHero *hero = [MJHero heroWithDict:dict]; // 3.2.添加模型对象到数组中 [heroArray addObject:hero]; } // 4.赋值 _heros = heroArray; } return _heros;}- (BOOL)prefersStatusBarHidden{ return YES;}#pragma mark - 数据源方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSLog(@"numberOfRowsInSection-----"); return self.heros.count;}// 离开屏幕的cell会怎样/** * 每当有一个cell进入视野范围内,就会调用 */- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"cellForRowAtIndexPath-----%d", indexPath.row); // static修饰局部变量:可以保证局部变量只分配一次存储空间(只初始化一次) static NSString *ID = @"hero"; // 1.通过一个标识去缓存池中寻找可循环利用的cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 2.如果没有可循环利用的cell if (cell == nil){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } // 3.给cell设置新的数据 // 取出模型 MJHero *hero = self.heros[indexPath.row]; // 设置cell的数据 cell.textLabel.text = hero.name; cell.detailTextLabel.text = hero.intro; cell.imageView.image = [UIImage imageNamed:hero.icon]; return cell;}#pragma mark - 代理方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 1.取得被点击这行对应的模型 MJHero *hero = self.heros[indexPath.row]; // 弹框 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"数据展示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; // 设置对话框的类型 alert.alertViewStyle = UIAlertViewStylePlainTextInput; // 取得唯一的那个文本框,显示英雄的名称 [alert textFieldAtIndex:0].text = hero.name; [alert show]; // 绑定行号到alertView上 alert.tag = indexPath.row;}#pragma mark - alertView的代理方法/** * 点击了alertView上面的按钮就会调用这个方法 * * @param buttonIndex 按钮的索引,从0开始 */- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 0) return; // 按钮的索引肯定不是0 // 1.取得文本框最后的文字 NSString *name = [alertView textFieldAtIndex:0].text;// int row = alertView.tag;// NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];// UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];// cell.textLabel.text = name; // 2.修改模型数据 int row = alertView.tag; MJHero *hero = self.heros[row]; hero.name = name; // 3.告诉tableView重新加载模型数据 // reloadData : tableView会向数据源重新请求数据 // 重新调用数据源的相应方法取得数据 // 重新调用数据源的tableView:numberOfRowsInSection:获得行数 // 重新调用数据源的tableView:cellForRowAtIndexPath:得知每一行显示怎样的cell // 全部刷新// [self.tableView reloadData]; // 局部刷新 NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0]; [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];}//- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath//{// // Deselect : 取消选中// NSLog(@"取消选中了第%d行", indexPath.row);//}@end
0 0
- tableView - 数据刷新
- tableview-数据刷新
- tableview 数据刷新
- swift3 tableView刷新数据
- IOS控件 Tableview 下拉刷新,加载数据
- tableView调用reloadData,数据没有刷新
- IOS控件 Tableview 下拉刷新,加载数据
- 关于不是tableView怎么刷新数据
- iOS tableView刷新选中cell数据
- tableView刷新
- tableview刷新
- ios tableView那些事 (十八) UITableView单行刷新数据
- ios tableView那些事 (十八) UITableView单行刷新数据
- iOS之TableView左划删除与刷新单挑数据
- 点不同的按钮刷新tableview的时间切换数据
- tableView 头部添加状态 今日头条刷新数据效果
- 关于cocos tableView的数据刷新后位置不变
- iOS的tableview与请求刷新数据一块的话.
- CocoaPods详解之----制作篇
- HashMap和Hashtable的区别
- Web Service详细说明
- linux命令:top命令
- 学习java的心得!
- tableview 数据刷新
- linux内核模块时内核树的作用
- LinkedHashMap源码剖析
- 什么是RF、IF信号
- presentViewController:animated:YES view will not appear until user taps again
- 拉链表
- mysql连接池-druid
- Python的一些简单示例(2)
- 设计模式是一个程序的主体!