gpt4 book ai didi

ios - 在 UITableviewcell 中使用 Collectionview

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

我还没有找到解决这个问题的方法。我已将视频附加到我的应用程序以查看其运行情况。

问题发布在正在发生的事情和应该发生的事情的底部。

https://www.mediacloud.cc/a/public/85156c9d4867ec82a42002e21954ee70.php?lang=en

我正在使用单例方法在我的两个 View Controller 之间传递数据,因为它们没有直接连接。

我正在使用的 4 个相关类是 MyAppDelegate、CameraViewController、TableViewController 和 CaptureSession。

我的 appDelegate.h 类

@property (nonatomic, retain) NSMutableArray *emailProperty;

@property (nonatomic, retain) NSMutableArray *imageProperty;

@property (nonatomic, retain) NSMutableDictionary *dictionaryProperty;

@property (nonatomic, retain) NSString *emailStringProperty;

@property (nonatomic, retain) UIImage *assetImageProperty;

@property (nonatomic, retain) UIImage *captureImageProperty;

+ (id)sharedManager;

我的 appDelegate.m 类

@synthesize emailProperty;

@synthesize imageProperty;

@synthesize dictionaryProperty;

@synthesize emailStringProperty;

@synthesize assetImageProperty;

@synthesize captureImageProperty;

#pragma mark Singleton Methods

+ (id)sharedManager {

static MediaCloudAppDelegate *sharedMyManager = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}

- (id)init {
if (self = [super init]) {

emailProperty = [NSMutableArray array];

imageProperty = [NSMutableArray array];

dictionaryProperty = [NSMutableDictionary dictionary];

}
return self;
}

CameraViewController.m 类

当用户在 CameraViewController 中时,他在触摸事件后立即按下“拍照”,弹出 UIalertview 要求他输入他的电子邮件。这是处理用户输入的代码。

_emailString1 = [alertView textFieldAtIndex:0].text;


MediaCloudAppDelegate *sharedManager = [MediaCloudAppDelegate sharedManager];

sharedManager.emailStringProperty = _emailString1;

[sharedManager.emailProperty addObject:sharedManager.emailStringProperty];

用户输入电子邮件并单击“确定”后,他们现在可以拍照了。

因为我正在使用 AVCapturesSession 来拍照。这是代码。

CaptureSession.m 类

NSData *imageData = [AVCaptureStillImageOutput 
jpegStillImageNSDataRepresentation:imageSampleBuffer];

UIImage *image = [[UIImage alloc] initWithData:imageData];

[self setStillImage:image];


MediaCloudAppDelegate *sharedManager = [MediaCloudAppDelegate sharedManager];


sharedManager.captureImageProperty = image;


[sharedManager.dictionaryProperty setObject:sharedManager.captureImageProperty
forKey:sharedManager.emailStringProperty];


[sharedManager.imageProperty addObject:sharedManager.dictionaryProperty];

NSLog(@"image %@", sharedManager.imageProperty);

拍照后,我记录输出。

例如。假设用户电子邮件是 bob@email.com 并拍了一张他自己的照片。

My NSLog(@"image %@", sharedManager.imageProperty);是图片

    {
bob@email.com = "UIImage: 0x1568d8c0";
}

现在要显示应用程序拍摄的所有照片和电子邮件,我们转到 TableViewController。

TableViewController.m 类

我的 TableView 设计是这样的。在一个单元格中,我有一个 UICollectionView,在右边我有一个 UILabel。

我这样显示电子邮件数组。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

MediaCloudAppDelegate *sharedManager = [MediaCloudAppDelegate sharedManager];
// Return the number of rows in the section.
return sharedManager.emailProperty.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString *CellIdentifier = @"Cell";

EmailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier

forIndexPath:indexPath];


MediaCloudAppDelegate *sharedManager = [MediaCloudAppDelegate sharedManager];


cell.emailLabel.text = [sharedManager.emailProperty objectAtIndex:indexPath.row];


return cell;
}

为了在单元格内的 Collection View 中显示照片,我这样做了。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView  
cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier
forIndexPath:indexPath];

UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];

MediaCloudAppDelegate *sharedManager = [MediaCloudAppDelegate sharedManager];



sharedManager.dictionaryProperty = [sharedManager.imageProperty objectAtIndex:indexPath.row];

imageView.image = [sharedManager.dictionaryProperty
objectForKey:sharedManager.emailStringProperty];


return cell;


}

现在一切正常,唯一的问题是每个单元格中的每个 Collection View 显示整个照片数组,而不是与唯一电子邮件字符串关联的照片。

所以如果我有行。

第一行是鲍勃第 2 行是杰夫第 3 行是戴夫

我们有每个人的照片。 Bob 行中的 Collection View 应该只显示 Bob 的照片。而 Jeff 行中的 Collection View 应该只显示 Jeff 等的照片。现在的问题是。 Bob 所在行的 Collection View 显示了所有三个人的照片,其他两个人的照片也是如此。

而且我不知道如何让它发挥作用。

提前感谢您的帮助!

最佳答案

您应该将要在每个 tableView 单元格中显示的图像存储为一个数组,然后将该数组传递给一个单独的 collectionView 实例。您不能只使用一个数组来存储所有图像并期望各种 collectionViews 挑选并选择要显示的图像。 (好吧,你可以,但要难得多)

cellForRowAtIndexPath 中传递信息的方式将取决于自定义 Cell 对象的组织方式。

实现此目的的一种方法是将每个数组存储在字典中,以用户电子邮件为键。另一种方法是将所有数组存储在另一个主数组中,由 indexPath.row 索引。

如果不清楚,请告诉我。因此,当您捕获图像时,将其添加到一个数组并将其存储在一个数组中,然后将其添加到您的字典中:

sharedManager.captureImageProperty = image;

NSMutableArray *array = sharedManager.dictionaryProperty[sharedManager.emailStringProperty];
if (!array) array = [NSMutableArray new];
[array addObject: sharedManager.captureImageProperty];

[sharedManager.dictionaryProperty setObject:array
forKey:sharedManager.emailStringProperty];

然后从字典中取出数组并将其传递给每个单元格的 collectionView。

在您的 collectionView cellForItemAtIndexPath 中,实现以下内容:

sharedManager.dictionaryProperty = [sharedManager.imageProperty objectAtIndex:indexPath.item];

//新建

NSArray *array = [sharedManager.dictionaryProperty objectForKey:sharedManager.emailStringProperty];
imageView.image = array[indexPath.item];

关于ios - 在 UITableviewcell 中使用 Collectionview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21961432/

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