最近项目中需要给UITableView设置一个类似于“已经到底部啦”的footview提醒,想都不想直接用代理方法
(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
自定义一个view返回给这个代理方法,然后实现另一个方法
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
设置了footerview的高度,满心欢喜的跑项目,发现并不是我想要的,我想要的是footerview跟随着cell的滑动而滑动,当数据超过一屏幕时候,需要不显示,等滑动到底部的时候再显示出来。
网上各种说滑动的时候重新设置frame的方法,从内心里比较抵触这样做。。。
最后还是想到一个方式来解决这个问题,既然我不能直接用系统的footerview,那么我定义两个section总可以了吧,然后,返回tableview的section的时候返回2,在cellForRowAtIndexPath方法里先判断是哪个section,如果section是要显示footerview的那个分组时,返回一个之前定义好的view给它。然后运行程序,结果如预期一样
部分代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section==0)
return 25+25+15+120;
else
return 40;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section == 1)//footer
return 1;
if(tableView == self.courseListTableView){//待开始
if(self.courseListDataArray.count == 0){
[LCSPlaceholderView showOnView:tableView type:LCSPlaceholderViewType_Course withTapBlock:^{
NSLog(@"无课程");
}];
}else{
[LCSPlaceholderView removeFromSuperView:tableView];
}
return self.courseListDataArray.count;
}else{//已完成
if(self.courseFinishDataArray.count == 0){
[LCSPlaceholderView showOnView:tableView type:LCSPlaceholderViewType_Course withTapBlock:^{
NSLog(@"无课程");
}];
}else{
[LCSPlaceholderView removeFromSuperView:tableView];
}
return self.courseFinishDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section == 1){
UITableViewCell *cell = [[UITableViewCell alloc] init];
LCSCustomTableFooterView *footerView = [[LCSCustomTableFooterView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
[cell.contentView addSubview:footerView];
return cell;
}
static NSString *cellReuseID = @"cellReuseID";