我有2000条记录,我只能在UITableView
显示15条记录,当用户滚动UITableView
,更多15条记录被载入表中。我应该怎么做?加载数据在Uitableview滚动
回答
我想答案是:- (void)scrollViewDidScroll:(UIScrollView *)scrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint offset = scrollView.contentOffset;
CGRect bounds = scrollView.bounds;
CGSize size = scrollView.contentSize;
UIEdgeInsets inset = scrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
float reload_distance = 15;
if(y > h + reload_distance)
{
//Call the Method to load More Data...
}
}
希望这会有所帮助...! !
您可以使用UIScrollView
这两种代表方法。由于UITableView
出现在UIScrollView
的层次中,因此您可以在UIScrollView
的此代表方法中获得控制权。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"TableView scrolling");
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"TableView Started scrolling");
}
试试以上两种方法。并选择一个适合你的。首先看看当你开始滚动UITableVIew
时NSLog
是怎么回事。然后在你的代码里写下你的代码。
您必须检查UIScrollView
contentOffset
那(UITableView
建立在UIScrollView
上)。为了更多的细胞,当用户到达你的表视图的底部添加到您的表
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// when reaching bottom, load more
float offs = (scrollView.contentOffset.y+scrollView.bounds.size.height);
float val = (scrollView.contentSize.height);
if (offs==val)
{
//add more data on your data array
}
}
:所以,这,添加到您的UITableViewDelegate
类。
默认numberOfItemsToDisplay = 15;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (numberOfItemsToDisplay >= [self.yourArray count])
{
numberOfItemsToDisplay = [self.yourArray count];
return 1;
}
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return numberOfItemsToDisplay;
}
else
{
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
static NSString *CellIdentifier = @"YourCustomCell";
YourCustomCell *objYourCustomCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (objYourCustomCell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"YourCustomCell" owner:self options:nil];
for(id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[YourCustomCell class]])
{
objYourCustomCell = (AlertsCustomCell *) currentObject;
break;
}
}
}
objYourCustomCell.lbl.text = [[self.yourArray objectAtIndex:indexPath.row]valueForKey:@"vName"];
return objYourCustomCell;
}
else
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell.contentView addSubview:[self loadMoreViewForTable:self.view]];
}
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tblAlertsList deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == 1)
{
numberOfItemsToDisplay = [self loadMore:numberOfItemsToDisplay arrayTemp:self.yourArray tblView:self.tblAlertsList];
[self.tblAlertsList endUpdates];
}else
{
// action if it is not LoadMore
}
}
+ (NSInteger)loadMore : (NSInteger)numberOfItemsToDisplay arrayTemp:(NSMutableArray*)aryItems tblView:(UITableView*)tblList
{
int count =0;
NSUInteger i, totalNumberOfItems = [aryItems count];
NSUInteger newNumberOfItemsToDisplay = MIN(totalNumberOfItems, numberOfItemsToDisplay + kNumberOfItemsToAdd);
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
for (i=numberOfItemsToDisplay; i<newNumberOfItemsToDisplay; i++)
{
count++;
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
numberOfItemsToDisplay = newNumberOfItemsToDisplay;
[tblList beginUpdates];
[tblList insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
if (numberOfItemsToDisplay == totalNumberOfItems) {
[tblList deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationLeft];
}
NSIndexPath *scrollPointIndexPath;
if (newNumberOfItemsToDisplay < totalNumberOfItems)
{
scrollPointIndexPath = [NSIndexPath indexPathForRow:numberOfItemsToDisplay-kNumberOfItemsToAdd inSection:0];
}
else
{
scrollPointIndexPath = [NSIndexPath indexPathForRow:i-count inSection:0];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100000000), dispatch_get_main_queue(), ^(void){
[tblList scrollToRowAtIndexPath:scrollPointIndexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
});
return numberOfItemsToDisplay;
}
我必须采取自定义单元格使用此方法?我只采取了UItableview
你不应该只使用customcell ..你可以继续使用你的tableView。但请确保您正在重新使用不会一直创建的单元格.. – 2013-03-12 06:32:47
你所要做的就是懒洋洋地加载tableview数据。与当前可见的tableview单元的加载数据不同,一次不是所有的数据。
试试这个:
http://www.cocoacontrols.com/controls/stableviewcontroller
使用此两种方法
//for refresh
- (void) addItemsOnTop {
//[self getLoginFollowingUserVideosCall];
[self refreshCompleted];
}
//for load more
- (void) addItemsOnBottom {
//[self getLoginFollowingUserVideosCall];
[self loadMoreCompleted];
}
- 1. 滚动以加载数据。迅速。 UITableView
- 2. 滚动加载数据
- 3. iOS UITableView只滚动时重新加载单元格数据
- 4. 无UITableView滚动重新加载UITableViewCell
- 5. 如何在滚动中加载数据?
- 6. 加载数据后自动滚动jqgrid
- 7. UITableView重新加载数据
- 8. UITableView已加载数据
- 9. 在UITableview中加载数据问题
- 10. 在viewdidappear发生后加载uitableview数据?
- 11. 滚动加载数据点播
- 12. 使用javascript滚动加载数据
- 13. 加强UITableView的滚动
- 14. UIVIew中的UITableView在滚动时冻结重新加载
- 15. 在滚动之前加载UITableView中的所有单元格
- 16. 在重新加载uitableview后滚动表格视图
- 17. UITableview控件文字在滚动显示时不会被加载
- 18. UITableView - >如何在向下滚动时加载其他条目?
- 19. UITableView限制滚动速度或UITableView限制行数滚动
- 20. 如何从服务器上加载动态数据在uitableview上
- 21. 在swift中 - UITableview滚动时滚动
- 22. Slow UITableView滚动
- 23. 自动滚动UITableView
- 24. UITableView的滚动下载细胞图像
- 25. 从缓存中加载UITableView数据Parse
- 26. UITableView - 从互联网加载数据
- 27. UITableView延迟加载单元格数据
- 28. UITableView数据重新加载问题
- 29. UITableView不加载Firebase快照数据。
- 30. 并排重新加载数据UITableView
你想只显示15条记录??? – iPatel 2013-03-12 05:45:49
是的,只有前15个,然后是另外15个表滚动 – sharmaravi 2013-03-12 05:46:30
而不是将其固定到15条记录,在表格中获取可见单元格并仅将数据更新到那些单元格。你可以得到这样的可见单元格信息:NSArray * visibleCells = [tableview visibleCells];请参阅此示例以进行延迟加载:http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009394-Intro-DontLinkElementID_2 – Dee 2013-03-12 05:51:58