zoukankan      html  css  js  c++  java
  • 【iOS开发-59】LOL案例:单组tabView、alertView样式、实现监听,以及用reloadData数据刷新

    案例效果:


    (1)先在storyboard中拖拽出一个tableView,然后下面用代码。

    ——tableView继承自scrollView。所以自然有滚动的特性

    ——最基本的还是数据转模型。以及对cell的赋值

    ——而cell的赋值那一块,为了优化性能。我们先从tableView的缓存中查找有无被缓存的cell。假设有。直接取出,假设没有再创建,这样提高性能。

    ——这个缓存池是tableView自带的,当滚动的时候。cell不在视线范围内时,这个cell就被放到缓存池里了。

    #import "ViewController.h"
    #import "WSHeros.h"
    
    @interface ViewController ()<UITableViewDataSource>
    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    @property (strong,nonatomic) NSArray *herosArray;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        //定义数据源是谁
        self.tableView.dataSource=self;
        //每行高度
        self.tableView.rowHeight=60;
        //行间切割线颜色和样式
        self.tableView.separatorColor=[UIColor colorWithRed:0 green:0 blue:1 alpha:1];
        self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    //隐藏状态栏
    -(BOOL)prefersStatusBarHidden{
        return YES;
    }
    //设置多少行
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.herosArray.count;//数组有多少就多少行
    }
    //设置每行的cell内容
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *ID=@"hero";
        //1、先推断tableView中有无我们须要的缓存的cell,用ID类识别
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
        //2、假设没有,就直接创建。记得给ID识别号
        if (cell==nil) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        }
        //无论是直接取,还是直接创建。截止此处,有一个cell了。

    //先利用indexPath.row取得行号相应的模型 WSHeros *hero=self.herosArray[indexPath.row]; //然后给cell赋值 cell.textLabel.text=hero.name; cell.imageView.image=[UIImage imageNamed:hero.icon]; cell.detailTextLabel.text=hero.intro; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; //cell还有背景以及选中背景等选项 cell.backgroundColor=[UIColor grayColor]; UIView *bgView=[[UIView alloc]init]; bgView.backgroundColor=[UIColor redColor]; cell.selectedBackgroundView=bgView; return cell; } //字典转模型 -(NSArray *)herosArray{ if (_herosArray==nil) { NSString *path=[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil]; NSArray *arr=[NSArray arrayWithContentsOfFile:path]; NSMutableArray * herosMuArr=[[NSMutableArray alloc]init]; for (NSDictionary * dict in arr) { WSHeros *heros=[[WSHeros alloc]initWithDict:dict]; [herosMuArr addObject:heros]; } _herosArray=herosMuArr; } return _herosArray; } @end


    (2)添加点击弹出alert的效果。而且能够改动名字

    ——由于用到监听tableView点击,所以须要引入协议

    ——由于用到监听用户点击了Alert里的哪个button。所以须要协议

    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

    而且须要设置代理(Alert的代理。在创建的时候直接指定为self就可以)

    - (void)viewDidLoad {
       ……
        self.tableView.delegate=self;
       ……
    }


    ——监听tableView被点击时,须要弹出一个带有textField的框。能够用alert.alertViewStyle属性设置,而且把这个模型里面的名字赋值给文本框显示出来。

    ——此外。还须要把是第几个模型,这个数字记录下来,在下一个方法监听点击alert哪个button的哪个里面须要用到,正好记录到alert的tag中。

    ——监听是否点击的时“确定”button。假设是,则先获取文本框文字,然后利用alert.tag找到相应的数据模型,用这个获得的文字替换原来的模型数据,最后刷新一下tableView,仅仅有改动数据模型。才干彻底改变tableView的显示结果,否则仅仅改动tableView,等又一次载入的时候还是会显示原来的数据。由于数据模型没有改动。

    ——思想是:用数据模型控制视图,即改动了数据模型,视图的呈现自然跟着改动。

    ——这里用到的知识点相对较多,涉及到tableView的reloadData方法。

    ——涉及到alertView得样式设置方法。

    ——还涉及到使用tableView和alertView的代理来实现监听。

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        WSHeros *hero=self.herosArray[indexPath.row];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
        alert.alertViewStyle=UIAlertViewStylePlainTextInput;
        [alert textFieldAtIndex:0].text=hero.name;
        alert.tag=indexPath.row;
        [alert show];
    }
    
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        if (buttonIndex==0) return;
        WSHeros *hero=self.herosArray[alertView.tag];
        hero.name=[alertView textFieldAtIndex:0].text;
        //这里须要传递一个indexPath数组,由于可能刷新不止一行。所以须要知道是几组几行。然后把非常多个组成数组传递进去
        NSIndexPath *path=[NSIndexPath indexPathForRow:alertView.tag inSection:0];
        [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];
        //下面是刷新所有数据
    //    [self.tableView reloadData];
    }
    

    终于效果:


  • 相关阅读:
    PS3 可播放的多媒体类型
    VB个性化文件夹图标
    Delphi源码:编辑长求字符串相似度
    Delphi使用zlib来压缩文件
    汉字编码问题
    Silverlight 3 学习概要
    asp.net下大文件上传知识整理
    DHTML动态创建一个弹出遮罩层
    Delphi的运算符重载
    Windows Vista 交互式服务编程
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6767354.html
Copyright © 2011-2022 走看看