现在的位置: 首页 > 综合 > 正文

【iOS开发-59】LOL案例:单组tabView、alertView样式、实现监听,以及用reloadData数据刷新

2017年03月21日 ⁄ 综合 ⁄ 共 3776字 ⁄ 字号 评论关闭

案例效果:

(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里的哪个按钮,所以需要协议

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

并且需要设置代理(Alert的代理,在创建的时候直接指定为self即可)

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

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

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

——监听是否点击的时“确定”按钮,如果是,则先获取文本框文字,然后利用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];
}

最终效果:

抱歉!评论已关闭.