object-c之UItableView下拉刷新

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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值