UITableView无限滚动

如何在中无限滚动UITableView?我知道如何使用来完成此操作UIScrollView,其中WWDC的视频中已经演示了苹果。我尝试在执行以下操作tableView:cellForRowAtIndexPath::


if (indexPath.row == [self.newsFeedData_ count] - 1)

{

    [self.newsFeedData_ addObjectsFromArray:self.newsFeedData_];

    [self.tableView reloadData];

}

但这失败了。还有其他想法吗?


慕慕森
浏览 769回答 3
3回答

MMMHUHU

如果您需要知道何时到达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];     }}

偶然的你

这是我放在一起的无限滚动UITableView的非常快速和完整的演示...@interface InfiniteScrollViewController ()@property (nonatomic) NSMutableArray *tableViewData;@property (nonatomic) BOOL loadingMoreTableViewData;@end@implementation InfiniteScrollViewController- (void)viewDidLoad {&nbsp; &nbsp; self.tableViewData = [[NSMutableArray alloc] init];&nbsp; &nbsp; [self addSomeMoreEntriesToTableView];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {&nbsp; &nbsp; return self.tableViewData.count + 1;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {&nbsp; &nbsp; static NSString *CellIdentifier = @"Cell";&nbsp; &nbsp; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];&nbsp; &nbsp; if (cell == nil) {&nbsp; &nbsp; &nbsp; &nbsp; cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];&nbsp; &nbsp; }&nbsp; &nbsp; if (indexPath.row < self.tableViewData.count) {&nbsp; &nbsp; &nbsp; &nbsp; cell.textLabel.text = [self.tableViewData objectAtIndex:indexPath.row];&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; cell.textLabel.text = @"Loading more data...";&nbsp; &nbsp; &nbsp; &nbsp; // User has scrolled to the bottom of the list of available data so simulate loading some more if we aren't already&nbsp; &nbsp; &nbsp; &nbsp; if (!self.loadingMoreTableViewData) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.loadingMoreTableViewData = YES;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [self performSelector:@selector(addSomeMoreEntriesToTableView) withObject:nil afterDelay:5.0f];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return cell;}- (void)addSomeMoreEntriesToTableView {&nbsp; &nbsp; int loopTill = self.tableViewData.count + 20;&nbsp; &nbsp; while (self.tableViewData.count < loopTill) {&nbsp; &nbsp; &nbsp; &nbsp; [self.tableViewData addObject:[NSString stringWithFormat:@"%i", self.tableViewData.count]];&nbsp; &nbsp; };&nbsp; &nbsp; self.loadingMoreTableViewData = NO;&nbsp; &nbsp; [self.tableView reloadData];}@end
打开App,查看更多内容
随时随地看视频慕课网APP