gpt4 book ai didi

ios - Collection View - 从不同文件夹接收图像 - 如何?

转载 作者:行者123 更新时间:2023-11-29 03:41:19 25 4
gpt4 key购买 nike

我正在创建一个带有两个 Collection View 的选项卡应用程序。我成功地让 2 个选项卡栏与 Collection View 一起使用,但它们都从同一图像文件夹接收数据 - 拇指和完整。因此,当前两个选项卡上的图像是相同的。

我希望找出从不同文件夹中显示不同图像的最佳方法是什么,这样它们就不相同?

如果您需要任何进一步的信息,请告诉我。

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

最佳答案

当使用 imageWithName[[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"JPG"] 时,您需要控制对不同尺寸图像的访问,而不是使用不同的文件夹但具有不同的文件名。如果您想使用不同的文件夹,那么您通常不会使用 NSBundle 来进行加载。

根据您的评论,您甚至不一定需要使用不同的类或重复的类。考虑重用。如果 Controller 的逻辑相同,但图像大小/位置不同,那么您可以向类添加几个属性来设置这些详细信息,然后可以实例化一个类并将其配置为在两种情况下工作。

关于ios - Collection View - 从不同文件夹接收图像 - 如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18401275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com