当我单击任何图像时,我有一个网格视图,我发送该网格视图的当前索引,以便我可以看到该图像。我正在从文档中加载图像。下面是我的 viewDidload 代码。但我的问题是什么。当我单击任何 gridview 项目时。假设它的索引是 4。我在 currentpage=4 中设置它。所以当我打开这个页面时。它向我展示了第四张图片。但我的页面控制器当前页面是 4,但我可以看到我的图像位于索引 1。
- (void)viewDidLoad {
[super viewDidLoad];
pageControlBeingUsed = NO;
UIImageView *subview;
//NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor orangeColor],[UIColor blueColor], nil];
for (int i = 0; i < (NSInteger)totalImagesInGrid; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
subview = [[UIImageView alloc] initWithFrame:frame];
//subview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[tableDataArray objectAtIndex:i]]];
NSString *name = [tableDataArray objectAtIndex:(NSUInteger)i];
NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:name];
UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]];
subview.image = loadedImage;
[self.scrollView addSubview:subview];
[subview release];
}
//float count = [totalImagesInGrid floatValue];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 13, self.scrollView.frame.size.height);
self.pageControl.currentPage = (NSInteger)currentImage;
NSLog(@"currentPage %d",(NSInteger)currentImage);
self.pageControl.numberOfPages = (NSInteger)totalImagesInGrid;
NSLog(@"(NSInteger)totalImagesInGrid %d",(NSInteger)totalImagesInGrid);
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = (NSInteger)currentImage + page;
NSLog(@"Page %d",page);
}
NSLog(@"scrollViewDidScroll");
}
- (IBAction)changePage {
// Update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
这是我的gridview,我点击(1,1)图像。
这是我的页面控制视图。但这不是我选择的图像。这是 (0,1) 图像,但在页面控件中看到它位于索引 2 上。