ios - 如何在表格或 Collection View 中实现无限加载?
<p><p>我正在构建一个文章阅读应用程序。我正在从 JSON 链接获取数据,例如 uitableview 中的文章图片和标题。</p>
<p>我无法在 uitableview 中实现分页,假设我的 JSON 链接是 www.example.com&page=1 一次包含 10 篇文章,即 1-10。</p>
<p>当我在像 www.example.com&page=2 这样的 JSON 链接中连接以获得 11-20 篇文章列表时。</p>
<p>我无法实现如何在滚动时在 uitableview 中加载数据并使用数据增加行数。</p>
<p>这是我的代码:</p>
<pre><code> int *x=1;
int *inc=10;
@interface ysTableViewController ()
{
Reachability *internetReachable;
}
@end
@implementation ysTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = ;
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
;
;
UILabel *titleLabel = [ initWithFrame:CGRectMake(20,10,0,20)];
titleLabel.textColor = ;
titleLabel.backgroundColor = ;
titleLabel.text = @"Story";
;
}
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
CGSize size = aScrollView.contentSize;
UIEdgeInsets inset = aScrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
float reload_distance = 10;
if(y > h + reload_distance) {
NSLog(@"load more rows");
inc=inc+10;
BOOL myBool = ;
if (myBool)
{
@try {
// for table cell seperator line color
self.tableView.separatorColor = ;
// for displaying the previous screen lable with back button in details view controller
UIBarButtonItem *backbutton1 = [ initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
[ setBackBarButtonItem:backbutton1];
_Title1 = [ init];
_Author1 = [ init];
_Images1 = [ init];
_Details1 = [ init];
_link1 = [ init];
_Date1 = [ init];
NSString *urlString=;
NSLog(@"xxxxx===%d",x);
NSURL *newUrl=;
NSData* data = ;
NSArray *ys_avatars = ;
x++;
if(ys_avatars){
for (int j=0;j<ys_avatars.count;j++)
{
[@"title"]];
[@"author"]];
[@"featured_img"]];
[@"content"]];
[@"permalink"]];
NSString *newStr=[@"date"] substringToIndex:[@"date"] length]-3];
;
} }
else
{
NSLog(@"asd");
} }
@catch (NSException *exception) {
}
}
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return inc;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Cellidentifier1 = @"ysTableViewCell";
ysTableViewCell *cell1 = ;
// Configure the cell...
long row = ;
cell1.TitleLabel1.text = _Title1;
cell1.AuthorLabel1.text = _Author1;
NSString *yourStoryUrl = stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
if(yourStoryUrl) {
NSArray *subStringsUrl = ;
NSString *stripedName = ;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ;
//Local stored image file path
NSString* filePath =];
if(filePath) {
UIImage *image = ;
if(image) {
// Now the image will have been loaded and decoded and is ready to rock for the main thread
ysTableViewCell*updateCell =(id);
if(updateCell)
updateCell.ThumbImage1.image=image;
cell1.ThumbImage1.image=image;
} else {
dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(taskQ, ^{
NSURL *Imageurl = ;
NSData *data =;
UIImage *images1 = [ initWithData:data];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ;
NSData *imageData = UIImagePNGRepresentation(images1);
//_imagePath =];
// NSLog((@"pre writing to file"));
if (!)
{
NSLog((@"Failed to cache image data to disk"));
}
else
{
NSLog((@"the cachedImagedPath is %@",filePath));
}
// Now the image will have been loaded and decoded and is ready to rock for the main thread
dispatch_sync(dispatch_get_main_queue(), ^{
ysTableViewCell*updateCell =(id);
if(updateCell)
updateCell.ThumbImage1.image=images1;
cell1.ThumbImage1.image=images1;
});
});
}
} else {
dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(taskQ, ^{
NSURL *Imageurl = ;
NSData *data =;
UIImage *images1 = [ initWithData:data];
// NSString *myString = ;
// NSLog(@"%@",myString);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ;
NSData *imageData = UIImagePNGRepresentation(images1);
_imagePath =];
// NSLog((@"pre writing to file"));
if (!)
{
NSLog((@"Failed to cache image data to disk"));
}
else
{
//NSLog((@"the cachedImagedPath is %@",_imagePath));
}
// Now the image will have been loaded and decoded and is ready to rock for the main thread
dispatch_sync(dispatch_get_main_queue(), ^{
ysTableViewCell*updateCell =(id);
if(updateCell)
updateCell.ThumbImage1.image=images1;
cell1.ThumbImage1.image=images1;
});
});
}
}
return cell1;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这绝非易事。一般而言,您需要看起来像这样的代码..</p>
<p>注意这个基本例程的四个非常不同的部分。</p>
<p>我从来没有为这个问题找到一个有效的“通用”包解决方案。</p>
<p>再次,看看这里的“四个部分”:它们给出了您正在寻找的逻辑!</p>
<pre><code>-(void)forTerm:(NSString *)term doPageAfter:(int)doingThisPage
{
doingThisPage = doingThisPage + 1;
if ( doingThisPage > 20 ) return; // never, ever, ever forget that!! heh.
[CLOUD search:term page:doingThisPage then:^(NSArray *thoseTenResults)
{
self.searchSpinner.hidden = YES;
// (step 1) IF IT IS "PAGE 1", we need to re-kick-off the array...
if ( doingThisPage == 1 )
CLOUD.searchResultsRA = [ init];
// (step 2) go ahead and add on these results
if ( doingThisPage == 1 )
{
;
;
}
else
{
[self.searchDisplay.collectionView performBatchUpdates:^
{
NSUInteger oldSize = CLOUD.searchResultsRA.count;
;
NSUInteger newSize = CLOUD.searchResultsRA.count;
NSMutableArray *arrayWithIndexPaths = ;
for (NSUInteger i = oldSize; i < newSize; i++)
[arrayWithIndexPaths
addObject:];
;
[self.searchDisplay.collectionView
insertItemsAtIndexPaths:arrayWithIndexPaths];
}
completion:nil];
}
// (step 3) indeed if it's the first page, do a drop-in for fun
if ( doingThisPage == 1 ) ;
// (for a "new search" which is now being displayed, in your UX
// there would be some sort of indication of that fact - do it here)
// (step 4) IF there WERE results .. try another page!
if ( thoseTenResults.count > 0 )
;
// note we are calling this same routine, again!!!
}
];
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在表格或 Collection View 中实现无限加载?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/25601280/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/25601280/
</a>
</p>
页:
[1]