您的位置:首页 > 移动开发 > Objective-C

object-c之UItableView下拉刷新

2015-11-29 10:33 459 查看
1.首先模拟一个假的数据

//数组里面装得是一个时间字符串
- (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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息