为了账号安全,请及时绑定邮箱和手机立即绑定

UITableView无限滚动

UITableView无限滚动

慕慕森 2019-12-17 10:43:24
如何在中无限滚动UITableView?我知道如何使用来完成此操作UIScrollView,其中WWDC的视频中已经演示了苹果。我尝试在执行以下操作tableView:cellForRowAtIndexPath::if (indexPath.row == [self.newsFeedData_ count] - 1){    [self.newsFeedData_ addObjectsFromArray:self.newsFeedData_];    [self.tableView reloadData];}但这失败了。还有其他想法吗?
查看完整描述

3 回答

?
MMMHUHU

TA贡献1834条经验 获得超8个赞

如果您需要知道何时到达UITableView的底部,请成为它的委托(因为它是UIScrollView的子类),然后使用-scrollViewDidScroll:委托方法比较表的内容高度和实际滚动位置。


编辑(类似这样):


- (void)scrollViewDidScroll:(UIScrollView *)scrollView_ 

{   

    CGFloat actualPosition = scrollView_.contentOffset.y;

    CGFloat contentHeight = scrollView_.contentSize.height - (someArbitraryNumber);

    if (actualPosition >= contentHeight) {

        [self.newsFeedData_ addObjectsFromArray:self.newsFeedData_];

        [self.tableView reloadData];

     }

}


查看完整回答
反对 回复 2019-12-17
?
偶然的你

TA贡献1841条经验 获得超3个赞

这是我放在一起的无限滚动UITableView的非常快速和完整的演示...


@interface InfiniteScrollViewController ()


@property (nonatomic) NSMutableArray *tableViewData;

@property (nonatomic) BOOL loadingMoreTableViewData;


@end


@implementation InfiniteScrollViewController


- (void)viewDidLoad {

    self.tableViewData = [[NSMutableArray alloc] init];

    [self addSomeMoreEntriesToTableView];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.tableViewData.count + 1;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }


    if (indexPath.row < self.tableViewData.count) {

        cell.textLabel.text = [self.tableViewData objectAtIndex:indexPath.row];

    } else {

        cell.textLabel.text = @"Loading more data...";


        // User has scrolled to the bottom of the list of available data so simulate loading some more if we aren't already

        if (!self.loadingMoreTableViewData) {

            self.loadingMoreTableViewData = YES;

            [self performSelector:@selector(addSomeMoreEntriesToTableView) withObject:nil afterDelay:5.0f];

        }

    }


    return cell;

}


- (void)addSomeMoreEntriesToTableView {

    int loopTill = self.tableViewData.count + 20;

    while (self.tableViewData.count < loopTill) {

        [self.tableViewData addObject:[NSString stringWithFormat:@"%i", self.tableViewData.count]];

    };

    self.loadingMoreTableViewData = NO;

    [self.tableView reloadData];

}


@end


查看完整回答
反对 回复 2019-12-17
  • 3 回答
  • 0 关注
  • 710 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信