2013-08-23 145 views
0

我创建了一个带有两个集合视图的tabbar应用程序。我成功地有2个标签栏使用集合视图,但他们都从相同的图像文件夹接收数据 - 拇指&完整。所以,这两个标签上的图像目前是相同的。收藏视图 - 从不同文件夹接收图像 - 如何?

我希望找出最好的办法是从不同的文件夹中获取不同的图像,所以它们不一样?

如果您需要更多信息,请告诉我。

NSString *kDetailedViewControllerID = @"DetailView"; 
NSString *kCellID = @"cellID";       




@implementation ViewController 

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section; 
{ 
    return 29; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; 
{ 

    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath]; 



    NSString *imageToLoad = [NSString stringWithFormat:@"%d.JPG", indexPath.row]; 
    cell.image.image = [UIImage imageNamed:imageToLoad]; 

    return cell; 
} 



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"showDetail"]) 
    { 
     NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0]; 

     // load the image, to prevent it from being cached we use 'initWithContentsOfFile' 
     NSString *imageNameToLoad = [NSString stringWithFormat:@"%d_full", selectedIndexPath.row]; 
     NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"JPG"]; 
     UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage]; 

     DetailViewController *detailViewController = [segue destinationViewController]; 
     detailViewController.image = image; 
    } 
} 

@end 
+0

显示您正在执行的操作以获取图像并将图像保存到2个文件夹的代码。为什么你不能只为图像设置适当的路径? – Wain

+0

代码被编辑到问题中,我不确定是否需要创建新类或者是否可以复制代码并相应地更改字符串。 – aaip

回答

1

当使用imageWithName[[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"JPG"],我们希望你来控制对不同尺寸的不是不同的文件夹,但不同的文件名的图像。如果你想使用不同的文件夹,那么你通常不会使用NSBundle来进行加载。

从您的评论,你甚至不一定需要使用不同的类或重复。考虑重用。如果你的控制器的逻辑是相同的,但是图像大小/位置不同,那么你可以向类中添加一些属性来设置这些细节,然后可以实例化和配置一个类以在两种情况下工作。

+0

为了说明问题,我可以将stringWithFormat从@“%d_full”至@“%d_full2”,同时确保图像名称相应对应,同时仍将文件类型保持为JPG。所以结果将不会从集合视图中的文件夹中接收到,而是从名称类型改为在segue中匹配,以便名称不会重复两次? – aaip

+0

在你的问题告诉我的范围内,是的,这听起来是正确的。 – Wain