我有一个幻灯片显示,可以从数据库中收集一系列图像。每个图像都有描述和与之关联的其他属性。基本上我想在每次加载新图像时更新UILabel的文本。我怎样才能做到这一点?
以下是我的工作内容:
//SLIDE MENU
- (void)loadVisiblePages {
// First, determine which page is currently visible
CGFloat pageWidth = gridView.frame.size.width;
NSInteger page = (NSInteger)floor((gridView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
//NSArray * descriptArray[20];
// Update the page control
pageControl.currentPage = page;
// Work out which pages you want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
// descriptArray[i] = selectedPhotoDesc;
}
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
[self purgePage:i];
}
}
- (void)loadPage:(NSInteger)page {
NSURL* imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@/%@.jpg", host, photoPath, collection, photoNumber]];
//get photo from server
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData: imageData];
if (page < 0 || page >= 20) {
return;
}
// Load an individual page, first checking if you've already loaded it
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
CGRect frame = gridView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
frame = CGRectInset(frame, 10.0f, 0.0f);
UIImageView *newPageView = [[UIImageView alloc] initWithImage:image];
newPageView.frame = frame;
[gridView addSubview:newPageView];
labelDescription.text = descriptionArray[page];
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// Remove a page from the scroll view and reset the container array
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
labelDescription.text = @"";
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Load the pages that are now on screen
[self loadVisiblePages];
}
我应该提一下,我可以滑动不同的图像,但无法更新文本。就目前而言,我只能获得与第一张图片相对应的文字而不是其他内容。 致谢