0

我有一个 iPhone 应用程序,我需要在其中加载大量图像(即大约 50 个),它有一个小矩形(略高于缩略图),每行包含 3 个,一页有 4 列,我需要水平滚动加载接下来的 12 个图像,依此类推。我还需要将其下的页码显示为 1,2 等。像附图一样

任何人都可以引导我朝着正确的方向在滚动视图中实现这一目标吗?我真的一团糟,如何将图像视图添加到这样的滚动视图中,每个图像视图也有自己的按钮。

4

2 回答 2

0

我解决这个问题的方法如下:(我不会提供很多细节,因为它们取决于您的特定需求和要求)。

.h 文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>

@property (unsafe_unretained, nonatomic) IBOutlet UIScrollView *scrollView;//assuming the scrollView is connected in a xib file

-(id)initWithImages:(NSArray*)images;//that could be whatever format you get your images(UIImage, urls to download, file names or custom views. Here we assume that the array contains instances of UIImage)

@end

.m 文件:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSArray *images;
@property (nonatomic, assign) CGFloat pageWidth;
@property (nonatomic, assign) int currentPage;

@end

@implementation ViewController


-(id)initWithImages:(NSArray*)images{

    self = [super init];

    if(self){

        self.images = images;

    }
    return self;

}

-(void)viewDidLoad{
    [super viewDidLoad];

    [self.scrollView setDelegate:self];
    [self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width*self.imageURLsArray.count,
                                               self.scrollView.frame.size.height)];
    [self.scrollView setPagingEnabled:YES];
    [self.scrollView setShowsVerticalScrollIndicator:NO];

    self.pageWidth = self.scrollView.frame.size.width;


    for(int i = 0; i < self.images.count; i++){

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width*i,
                                                                              self.scrollView.frame.origin.y,
                                                                              self.scrollView.frame.size.width,
                                                                              self.scrollView.frame.size.height)];
        imageView.image = [self.images objectAtIndex:i];
        [self.scrollView addSubview:imageView];

    }

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int page = floor(scrollView.contentOffset.x - self.pageWidth/2)/self.pageWidth + 1;
    if(self.currentPage != page){
        NSLog(@"Scrolled to new page: %d", page);
        self.currentPage = page;
//do additional things based on the fact that you have scrolled to a new page (like changing the page number)
    }
 }
@end
于 2013-03-01T10:24:47.843 回答
0

使用 UItableview 创建带有两个图像视图的自定义单元格,并将该自定义单元格加载到索引路径的单元格,然后创建一个 nsarray 并将图像名称传递到该数组中,然后将该图像转换为数据格式,例如

nsdata *data = [NSdata dataWithcontents of url [array object at index:indexpath.row]]; cell.imageview.image = [uiimage imagewithdata:data]; 为此,您应该知道创建自定义单元格。

于 2013-03-01T12:42:56.057 回答