打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
在tableview中添加更多的行
iPhone屏幕尺寸是有限的,如果需要显示的数据很多,可以先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据。基本上就是数据源里先只放10条, 点击最后一个cell时, 添加更多的数据到数据源中. 比如:
数据源是个array:
NSMutableArray *items;
ViewController的这个方法返回数据条数: +1是为了显示"加载更多"的那个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        int count = [items count];
    return  count + 1;
}
  这个方法定制cell的显示, 尤其是"加载更多"的那个cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if([indexPath row] == ([items count])) {
        //创建loadMoreCell
        return loadMoreCell;
    }
        //create your data cell

    return cell;
}
  还要处理"加载更多"的那个cell的选择事件,触发一个方法来加载更多数据到列表

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == [items count]) {
        [loadMoreCell setDisplayText:@"loading more ..."];
        [loadMoreCell setAnimating:YES];
        [self performSelectorInBackground:@selector(loadMore) withObject:nil];
        //[loadMoreCell setHighlighted:NO];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        return;
    }
    //其他cell的事件
   }
  加载数据的方法:
-(void)loadMore
{
    NSMutableArray *more;
        //加载你的数据
    [self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];
}
  添加数据到列表:
-(void) appendTableWith:(NSMutableArray *)data
{
    for (int i=0;i<[data count];i++) {
        [items addObject:[data objectAtIndex:i]];
    }
    NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10];
    for (int ind = 0; ind < [data count]; ind++) {
        NSIndexPath    *newPath =  [NSIndexPath indexPathForRow:[items indexOfObject:[data objectAtIndex:ind]] inSection:0];
        [insertIndexPaths addObject:newPath];
    }
    [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
UITableView 实现Cell删除操作
iOS 多选删除(附tableViewTips及单选删除)
UITableView中定义快捷键 拷贝粘贴
ios UITableview 刷新某一个cell 或 section
UITableView侧滑删除
iOS系列译文:整洁的表视图代码
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服