ios-day06-04(UITableView的全局刷新和局部刷新、UIAlertView的使用)

源码下载地址:http://download.csdn.net/detail/liu537192/8463517


效果图:

          


         


核心代码:

//
//  LiuJieViewController.m
//  04-数据刷新
//
//  Created by XinYou on 15-2-28.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieViewController.h"
#import "LiuJieHero.h"

@interface LiuJieViewController () 
    
    
     
     
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong)NSArray *heros;

@end

@implementation LiuJieViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// 给UITableView设置数据源
    self.tableView.dataSource = self;
    // 给UITableView设置代理
    self.tableView.delegate = self;
    
    // 设置行高
    self.tableView.rowHeight = 60;
}

/**
 *  隐藏状态栏
 */
- (BOOL)prefersStatusBarHidden{

    return YES;
}

- (NSArray *)heros{

    if (_heros == nil) {
        // 获取plist文件的路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
        
        // 加载数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 将dictArray中得所有字典对象转成模型对象,放到新的数组中
        NSMutableArray *tempArray = [NSMutableArray array];
        
        for (NSDictionary *dict in dictArray) {
            // 创建模型对象
            LiuJieHero *hero = [LiuJieHero heroWithDict:dict];
            // 添加模型对象到数组中
            [tempArray addObject:hero];
        }
        
        // 赋值
        _heros = tempArray;
    }
    
    return _heros;
}

#pragma mark -数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.heros.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 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设置新的数据
    LiuJieHero *hero = self.heros[indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:hero.icon];
    cell.textLabel.text = hero.name;
    cell.detailTextLabel.text = hero.intro;
    
    return cell;
}

#pragma mark -代理方法
/**
 *  当用户点击了某一行,就会执行这个方法
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//    NSLog(@"点击了第%d行",indexPath.row);
    
    // 取得被点击这行对应的模型
    LiuJieHero *hero = self.heros[indexPath.row];
    
    // 创建弹框
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"数据展示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    // 设置对话框的类型
    // UIAlertViewStyleDefault 默认的样式的对话框
    // UIAlertViewStyleSecureTextInput 带有一个密文输入框的对话框
    // UIAlertViewStylePlainTextInput 带有一个普通输入框的对话框
    // UIAlertViewStyleLoginAndPasswordInput 带有两个输入框(一个普通和一个密文)的对话框
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    
    // 设置对话框中输入框的文本
    [alert textFieldAtIndex:0].text = hero.name;
    
    // 显示弹框
    [alert show];
    
    // 绑定行号到alertView上
    alert.tag = indexPath.row;
}

#pragma mark -alertView的代理方法
/**
 *  点击了alertView上面的按钮就会调用这个方法
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    // 如果按钮的索引为0,表示点击了“取消”按钮
    if (buttonIndex == 0) {
        return;
    }
    
    // 1 取得文本输入框最后的文字
    NSString *name = [alertView textFieldAtIndex:0].text;
    
    // 2 修改模型数据
    int row = alertView.tag;
    LiuJieHero *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];
    // 3 局部刷新
    [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];
}

@end
//
//  LiuJieHero.h
//  04-数据刷新
//
//  Created by XinYou on 15-2-28.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
     
     
      
      

@interface LiuJieHero : NSObject
/**
 *  每个英雄对应的图片
 */
@property (nonatomic,copy) NSString *icon;
/**
 *  每个英雄对应的名字
 */
@property (nonatomic,copy) NSString *name;
/**
 *  每个英雄的介绍
 */
@property (nonatomic,copy) NSString *intro;

+ (instancetype)heroWithDict:(NSDictionary *)dict;

- (instancetype)initWithDict:(NSDictionary *)dict;

@end
//
//  LiuJieHero.m
//  04-数据刷新
//
//  Created by XinYou on 15-2-28.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieHero.h"

@implementation LiuJieHero

- (instancetype)initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {
        self.icon = dict[@"icon"];
        self.name = dict[@"name"];
        self.intro = dict[@"intro"];
        
        // 也可以使用KVC
//        [self setValuesForKeysWithDictionary:dict];
    }
    
    return self;
}

+ (instancetype)heroWithDict:(NSDictionary *)dict{

    return [[self alloc] initWithDict:dict];
}

@end

     
     
    
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS-RATreeView是一个开源的第三方库,提供了多层级的UITableView展示功能。使用该库可以轻松实现多级列表的展开与收起。 首先,在项目中引入iOS-RATreeView库。可以使用CocoaPods引入,也可以手动下载并导入。 接下来,在需要使用多级列表的UIViewController中,创建一个RADataObject类型的数组,用来存储数据。RADataObject是iOS-RATreeView中的一个数据模型,用来表示一条记录。每个RADataObject可以包含多个子RADataObject,从而形成多级列表。 ``` // 创建RADataObject数组 NSMutableArray *data = [NSMutableArray array]; // 创建一级列表 RADataObject *level1_1 = [RADataObject dataObjectWithName:@"Level 1-1" children:nil]; RADataObject *level1_2 = [RADataObject dataObjectWithName:@"Level 1-2" children:nil]; RADataObject *level1_3 = [RADataObject dataObjectWithName:@"Level 1-3" children:nil]; // 创建二级列表 RADataObject *level2_1 = [RADataObject dataObjectWithName:@"Level 2-1" children:nil]; RADataObject *level2_2 = [RADataObject dataObjectWithName:@"Level 2-2" children:nil]; RADataObject *level2_3 = [RADataObject dataObjectWithName:@"Level 2-3" children:nil]; // 将二级列表添加到一级列表中 level1_1.children = @[level2_1, level2_2]; level1_2.children = @[level2_3]; // 将一级列表添加到RADataObject数组中 [data addObject:level1_1]; [data addObject:level1_2]; [data addObject:level1_3]; ``` 创建完数据源后,需要创建RATreeView对象,并设置代理和数据源。 ``` // 创建RATreeView对象 self.treeView = [[RATreeView alloc] initWithFrame:self.view.bounds]; // 设置代理和数据源 self.treeView.delegate = self; self.treeView.dataSource = self; ``` 接下来实现RATreeViewDataSource协议中的方法,用来返回列表的数据。具体实现可以参考下面的代码。 ``` - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item { static NSString *identifier = @"Cell"; UITableViewCell *cell = [treeView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } // 获取RADataObject对象 RADataObject *dataObject = item; // 设置cell的文本 cell.textLabel.text = dataObject.name; return cell; } - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item { if (item == nil) { // 如果item为nil,表示请求根节点的子节点数量 return self.data.count; } else { // 获取RADataObject对象 RADataObject *dataObject = item; // 返回子节点数量 return dataObject.children.count; } } - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item { if (item == nil) { // 如果item为nil,表示请求根节点的子节点 return self.data[index]; } else { // 获取RADataObject对象 RADataObject *dataObject = item; // 返回子节点 return dataObject.children[index]; } } - (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item { // 返回是否可以编辑 return YES; } - (void)treeView:(RATreeView *)treeView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowForItem:(id)item { if (editingStyle == UITableViewCellEditingStyleDelete) { // 删除节点 RADataObject *parentObject = [treeView parentForItem:item]; if (parentObject) { NSMutableArray *children = [NSMutableArray arrayWithArray:parentObject.children]; [children removeObject:item]; parentObject.children = children; } else { NSMutableArray *data = [NSMutableArray arrayWithArray:self.data]; [data removeObject:item]; self.data = data; } // 刷新列表 [treeView reloadData]; } } ``` 最后,在RATreeViewDelegate协议中实现展开与收起节点的方法。 ``` - (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item { // 获取RADataObject对象 RADataObject *dataObject = item; // 设置节点的展开状态 dataObject.expanded = YES; } - (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item { // 获取RADataObject对象 RADataObject *dataObject = item; // 设置节点的展开状态 dataObject.expanded = NO; } ``` 至此,多级列表展开与收起的功能就实现了。在需要展示多级列表的地方,只需要将创建的RATreeView添加到视图中即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值