当我点击任何图像时,我有一个gridview,我发送该gridview的当前索引,以便我可以看到该图像。我正在从doucment加载图像。 下面是我的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上。
答案 0 :(得分:0)
使用gridview的selectedIndex设置页面索引,例如参见下面的...
- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)colIndex{
///set Image with index of colIndex..
int selectedIndex = (rowIndex*4)+colIndex;
NSLog(@"\n Selected Image Index is %d",selectedIndex);
}
并且在SecondView类的viewDidLoad:
中,只需在方法的最后一个方法调用你的第二个View类方法changePage
- (void)viewDidLoad {
////another your code
[self changePage]
}
我希望这可以帮到你..