首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iphone中的PageControl问题

iphone中的PageControl问题
EN

Stack Overflow用户
提问于 2012-11-30 16:29:09
回答 1查看 300关注 0票数 1

我有一个网格视图,当我点击任何图像时,我会发送该网格视图的当前索引,这样我就可以看到该图像。我正在从doucment加载图像。下面是我的viewDidload代码。但我的问题是。当我单击任何网格视图项目时。假设它的索引是4,我在currentpage=4中设置了它,所以当我打开这个页面时。它向我展示了第四张图片。但是我的页面控制器当前页面是4,但我可以看到索引为1的图像。

代码语言:javascript
运行
复制
- (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上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-30 16:50:02

使用gridview的selectedIndex设置页面索引如下图所示...

代码语言:javascript
运行
复制
- (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,如下所示

代码语言:javascript
运行
复制
- (void)viewDidLoad {
    ////another your code
    [self changePage] 
}

我希望这能帮助你..

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13641473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档