所以我通过将调整大小的图像放入其中来进行第一次UIScrollview,但是当我到达第一组结果并添加第二组等时,视图将重置为开头。我不确定它是否正在添加图像,因为x值正在向前计数,因此UIScrollview应该调整大小,所以我只能假设它只是将下一组图像放在旧图像之上。
我在这里做错了什么?
-(void)addScrollViewImages {
// setup our UIScrollview
self.scrollView.delegate = self;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width,self.scrollView.frame.size.height);
// hit our server for the data
NSString *encodedKeywords =[self.keyword stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
self.results = [self fetchImageResults:encodedKeywords];
// loop over some image results
for (id image in self.results)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(self.x, 0, self.screenWidth, self.screenHight)];
NSString *imageUrl = image[@"url"];
NSData *image = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
// resize the image to the device
[imageView setImage:[self imageScaleCropToSize:[UIImage imageWithData:image]]];
[self.scrollView addSubview: imageView];
self.x = (self.x + imageView.frame.size.width);
// resize frame width
if(self.x > self.view.frame.size.width) self.scrollView.contentSize = CGSizeMake(self.x, self.scrollView.frame.size.height);
}
}
// This just gets the json data and processes it a little
-(NSArray*)fetchImageResults:(NSString*)keyword {
//This should be fairly quick
NSError *error = nil;
NSURL *apiURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://example.com/api/v1/search?q=%@", keyword]];
NSData *jsonData = [NSData dataWithContentsOfURL:apiURL];
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
return result[@"results"];
}
// Scroll index management
- (void)scrollViewDidScroll:(UIScrollView *)sender {
float indexNum = self.scrollView.contentOffset.x / self.scrollView.frame.size.width;
if(floor(indexNum) == indexNum) {
int num = (int)indexNum;
NSLog(@"viewing %@",self.results[num]);
// if we are at the end, load more
if(num == (self.results.count-1) ) {
dispatch_async(dispatch_get_main_queue(), ^{
[self addScrollViewImages];
});
}
}
}
答案 0 :(得分:0)
你没有使用UITableView
就自杀了。只需使用原型UITableView
制作UITableViewCells
即可。对单元格imageViewTableCell
进行子类化,并使其唯一属性为UIImageView
,填充其大小。
其余的只是使用UITableViewDelegate
和UITableViewDataSource
。
答案 1 :(得分:0)
我明白了。
首先,我从未在viewDidLoad方法的开头启动结果NSMutableArray
self.results = [[NSMutableArray alloc] init];
一旦我改变了,我所要做的就是更新我的滚动方法
// Scroll index management
- (void)scrollViewDidScroll:(UIScrollView *)sender {
float indexNum = self.scrollView.contentOffset.x / self.scrollView.frame.size.width;
float currentPosition = self.scrollView.contentOffset.x;
if(floor(indexNum) == indexNum) {
int num = (int)indexNum;
// if we are at the end, load more
if(num == ([self.results count]-1)) {
dispatch_async(dispatch_get_main_queue(), ^{
[self addScrollViewImages];
[self.scrollView setContentOffset:CGPointMake(currentPosition, 0.0)];
});
}
}
}