ios - 如何将 ViewController 中的数组数据传递给 UICollectionView 自定义单元格?

标签 ios objective-c arrays uitableview uicollectionview

我正在自定义“UICollectionViewCell”中创建一个 tableView,在我的 ViewController 中,我有一个数组,其中包含要显示在 TableView 中每一行的数据。 因此,我想将此数组的数据传递给自定义单元格(包含 tableview 委托(delegate)方法)。

我正在这样做,但没有帮助。

在“GroupCollectionViewCell.m”中

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *groupTableIdentifier = @"DeptGroupTable";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:groupTableIdentifier];

    if (cell == nil) {

    }
    RoadmapViewController *vc = [[RoadmapViewController alloc] init];
    cell.textLabel.text =[vc.grouplist objectAtIndex:indexPath.row];
    return cell;
}

在“RoadViewController.m”中

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{
    GroupCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[GroupCollectionViewCell alloc] init];
        // [cell.groupData addObjectsFromArray:_grouplist];
        //:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    return cell;
}

注意 - groupList 如果我要传递的数组和 groupData 是自定义单元格内的空白数组。

最佳答案

CollectionViewCell 中创建一个 MutableArray 对象和一个类似这样的方法

@property (strong, nonatomic) NSMutableArray *arrObj;

-(void)loadTableData(NSMutableArray*)arr {
     self.arrObj = arr;
     [self.tableView reloadData];
     //Now used this arrObj in your delegate and datasource method
}

之后像这样从 cellForItemAtIndexPath 调用此函数

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

{
    GroupCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[GroupCollectionViewCell alloc] init];
        // [cell.groupData addObjectsFromArray:_grouplist];
        //:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    [cell loadTableData:arr];
    return cell;
}

希望这对您有帮助。

关于ios - 如何将 ViewController 中的数组数据传递给 UICollectionView 自定义单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37982627/

相关文章:

objective-c - 绘制触摸事件

iphone - 从自定义 UITableViewCell 中的 UITextFields 中检索值

ios - 将 Objective-C 代码转换为 Swift : okay to omit release calls?

Java - 对于通用数据类型 Stack<Item>,new Stack<?>[N] 是否等同于 new Stack[N]?

c - 数组 K&R 1.6 个字符?

ios - 如何在使用 Realm 的同时实现 Codable

ios - ALAMOFIRE 发布请求图像下载并在 Collection View 中绑定(bind)

java - [JAVA]数组迭代器的arraylist

ios - 如何选择和突出显示像 UITableViewCell 这样的 UICollectionView?

ios - indexPath 在所有行中返回 0