0

我希望有人可以在这里帮助我,我不知道还能做什么。我有一个 UIScrollView 加载一组 44 图像,然后允许用户翻阅它们。我的代码正在工作,然后突然停止了。我不知道我做了什么改变,所以任何帮助将不胜感激:

前一个视图是一个 UITableView,它传递一个字符串以及所选单元格的名称。这是 ScrollView 中的 ViewDidLoad 方法:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Answer"
                                                                    style:UIBarButtonItemStyleBordered
                                                                   target:self
                                                                   action:@selector(answerPressed:)];

    self.navigationItem.rightBarButtonItem = rightButton;

    if([questionSetName isEqualToString:@"Limitations"]){

        [self limitationsView];
    }
}

这是限制视图:

-(void)limitationsView{

    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -44, self.view.frame.size.width, self.view.frame.size.height)];

    scroll.pagingEnabled = YES;

    NSArray *unsortedArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"Images/Limitations"];

    NSMutableArray *limitationImages = [[NSMutableArray alloc] init];

    for (int x = 0; x<[unsortedArray count]; x++) {

        NSString *fileName = [[unsortedArray objectAtIndex:x] lastPathComponent];

        [limitationImages insertObject:fileName atIndex:x];
    }

    NSArray *sortedArray = [limitationImages sortedArrayUsingFunction:finderSortWithLocal
                                                              context:(__bridge void *)([NSLocale currentLocale])];

    for(int i = 0; i< [sortedArray count]; i++){

        CGFloat xOrigin = i * self.view.bounds.size.width;

        UIImageView *limitationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];;

        [limitationImageView setImage:[UIImage imageNamed:[sortedArray objectAtIndex:i]]];

        limitationImageView.contentMode = UIViewContentModeScaleAspectFit;

        [scroll addSubview:limitationImageView];

    }

    scroll.contentSize = CGSizeMake(self.view.frame.size.width * [sortedArray count], self.view.frame.size.height);

    [self.view addSubview:scroll];


}

当我到达 LimitationsView 方法的末尾时,sortedArray 包含所有 44 个图像和正确的名称,如果我尝试设置滚动视图的背景颜色,我可以成功地做到这一点。但是我的图像没有加载,我不知道为什么?

4

1 回答 1

1

我建议使用

NSLog(@"image:%@",[UIImage imageNamed:[limitationsArray objectAtIndex:i]]); 

to see if the image is null at that point. If that's the case, then there is a problem with the strings you are storing in that array i.e., they are not identical to the actual names of your images

于 2012-10-10T16:45:06.380 回答