object-c之UItableView下拉刷新
2015-11-29 10:33
459 查看
1.首先模拟一个假的数据
2.实列化一个tableView控件
5.将这两个控件添加到view里面去
6.下面全是tableView的代理方法了这里只需要两个代理方法就可以完成数据的添加
第一个:设置数据的显示条数
还有一个就是tableView还有一种模式就是组模式,也就是把数据进行分组简单的说下
跟普通显示数据不同的就是,它要返回一个组数。和设置组名。然后自动会跟你分好组
1.返回组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.carArray.count;
}
2.返回组名
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
CarModel *cars = self.carArray[section];
return cars.title;
}
3.设置侧边索引就像我们手机通讯录一样//返回需要显示的标题数组
-(nullable NSArray<NSString *> *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
//第一种方式
// NSMutableArray *titleArray = [[NSMutableArray alloc]init];
// for(CarModel *g in self.carArray)
// {
// [titleArray addObject:g.title];
// }
//第二种方式
//使用kvc方法获取
return [self.carArray valueForKeyPath:@"title"];
//return titleArray;
}
//数组里面装得是一个时间字符串 - (NSMutableArray *) arrayTime { if(!_arrayTime) { _arrayTime = [[NSMutableArray alloc]init]; } return _arrayTime; }
2.实列化一个tableView控件
- (UITableView *) tableView { if(!_tableView) { _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; //添加头 UIView *tempView = [[UIView alloc]init]; //把一个视图添加到一个tableview里面会自动适配,所以这里只需要给视图一个高度就行了 tempView.bounds = CGRectMake(0, 0, 0, 100); tempView.backgroundColor = [UIColor cyanColor]; _tableView.tableHeaderView = tempView; //不显示y滚动条 _tableView.showsVerticalScrollIndicator = NO; //设置代理 _tableView.dataSource = self; _tableView.delegate = self; } return _tableView; }3.添加刷新控件并且实现刷新事件的监听
- (UIRefreshControl *)refresh { if(!_refresh) { _refresh = [[UIRefreshControl alloc]init]; //添加刷新监听事件 [_refresh addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged]; //下拉刷新显示的提示文字 _refresh.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在加载"]; //刷新控件的颜色 _refresh.tintColor = [UIColor redColor]; } return _refresh; }4.实现刷新事件的监听
- (void) refreshData { //设置刷新后显示的数据 refreshNum++; //添加时间到数组 NSDate *mydate = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; formatter.dateFormat = @"yyyy-mm-dd HH:mm:ss"; [self.arrayTime addObject:[formatter stringFromDate:mydate]]; //模拟延时加载 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //停止显示菊花 [self.refresh endRefreshing]; //重新加载表格 [self.tableView reloadData]; }); }
5.将这两个控件添加到view里面去
- (void)viewDidLoad { [super viewDidLoad]; //取消自动索引适配缩进,否则每行数据的分割线就会不好看 self.edgesForExtendedLayout = UIRectEdgeNone; //添加刷新控件 [self.tableView addSubview:self.refresh]; //添加tableView控件 [self.view addSubview:self.tableView]; }
6.下面全是tableView的代理方法了这里只需要两个代理方法就可以完成数据的添加
第一个:设置数据的显示条数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return refreshNum; }第二个设置数据
//设置数据 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; if(self.arrayTime.count!=0) { cell.textLabel.text = @"时间"; cell.detailTextLabel.text = (NSString *)self.arrayTime[indexPath.section]; // 组 // indexPath.section; // 行 // indexPath.row; } return cell; }好了就这么简单。也就20多分钟的事情
还有一个就是tableView还有一种模式就是组模式,也就是把数据进行分组简单的说下
跟普通显示数据不同的就是,它要返回一个组数。和设置组名。然后自动会跟你分好组
1.返回组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.carArray.count;
}
2.返回组名
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
CarModel *cars = self.carArray[section];
return cars.title;
}
3.设置侧边索引就像我们手机通讯录一样//返回需要显示的标题数组
-(nullable NSArray<NSString *> *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
//第一种方式
// NSMutableArray *titleArray = [[NSMutableArray alloc]init];
// for(CarModel *g in self.carArray)
// {
// [titleArray addObject:g.title];
// }
//第二种方式
//使用kvc方法获取
return [self.carArray valueForKeyPath:@"title"];
//return titleArray;
}
相关文章推荐
- flex 控件的重要属性
- Delphi控件ListView的属性及使用方法详解
- web下载的ActiveX控件自动更新
- WinForm实现按名称递归查找控件的方法
- C#中父窗口和子窗口之间控件互操作实例
- Android编程之Button控件用法实例分析
- Android控件之CheckBox、RadioButton用法实例分析
- MFC中动态创建控件以及事件响应实现方法
- WinForm自定义函数FindControl实现按名称查找控件
- Android控件之ProgressBar用法实例分析
- WinForm拖拽控件生成副本的解决方法
- ASP.NET动态添加用户控件的方法
- ASP.NET的HtmlForm控件学习及Post与Get的区别概述
- WinForm实现移除控件某个事件的方法
- C#分屏控件用法实例
- 可以浮动某个物体的jquery控件用法实例
- jQuery操作表单常用控件方法小结
- C#的winform控件命名规范
- Jquery给基本控件的取值、赋值示例
- ASP.net 动态加载控件时一些问题的总结