1

画像をクリックするとグリッドビューがあり、そのグリッドビューの現在のインデックスを送信して、その画像を見ることができます。ドキュメントから画像を読み込んでいます。以下は私のviewDidloadコードです。しかし、私の問題は何ですか。グリッドビュー項目をクリックすると。そのインデックスが4であるとしましょう。ここでcurrentpage = 4に設定します。だから、このページを開いたとき。それは私に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;

}

これは私のグリッドビューで、(1,1) 画像をクリックします。

ここに画像の説明を入力

これが私のページ コントロール ビューです。しかし、それは私が選択した画像ではありません。これは (0,1) 画像ですが、ページ コントロールでインデックス 2 にあることを確認してください。

ここに画像の説明を入力

4

1 に答える 1

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:secondViewクラスメソッドを呼び出すだけですchangePage

- (void)viewDidLoad {
    ////another your code
    [self changePage] 
}

これがあなたを助けることを願っています..

于 2012-11-30T08:50:02.193 に答える