我有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. 滾動以加載數據。迅速。 UITableView
- 3. 如何在滾動後在UITableView中加載更多數據
- 4. UITableView在滾動到頂部時加載更多數據?
- 5. 爲什麼UITableview在重新加載數據時向上滾動
- 6. UITableView在滾動表時使用NSTimer重新加載數據
- 7. 滾動加載數據
- 8. 無UITableView滾動重新加載UITableViewCell
- 9. Xamarin iOS,UITableView,滾動以加載更多
- 10. 如何在滾動中加載數據?
- 11. 在滾動加載更多的數據
- 12. 滾動時在UITableView中重複數據
- 13. 加載數據後自動滾動jqgrid
- 14. UITableView不加載數據
- 15. UITableView已加載數據
- 16. UITableView加載數據問題
- 17. UITableView重新加載數據
- 18. 如何在UITableview中滾動時拆分數據並重新加載?
- 19. uitableview]如何防止用戶在數據完全加載前滾動?
- 20. 疊加視圖滾動爲UITableView滾動
- 21. 在viewdidappear發生後加載uitableview數據?
- 22. 在UITableview中加載數據問題
- 23. 在UItableview中保存和加載數據
- 24. 在Android中加載json數據-uitableview
- 25. 無限滾動數據預加載
- 26. 滾動AngularJS ngRepeat用Ajax加載數據
- 27. 使用javascript滾動加載數據
- 28. 滾動加載數據點播
- 29. VS向下滾動時加載數據
- 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