首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将ViewController内部的数组数据传递给UICollectionView自定义单元?

如何将ViewController内部的数组数据传递给UICollectionView自定义单元?
EN

Stack Overflow用户
提问于 2016-06-23 04:54:23
回答 2查看 1.1K关注 0票数 0

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

,我是这样做的,但帮不上忙。

“GroupCollectionViewCell.m ViewCell.m”中的

代码语言:javascript
运行
复制
-(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"

代码语言:javascript
运行
复制
- (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;
}

Note -如果要传递的数组和groupData是自定义单元格内的空白数组,则为groupList。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-23 05:02:17

在您的MutableArray中创建一个CollectionViewCell对象,并创建如下方法

代码语言:javascript
运行
复制
@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调用这个函数

代码语言:javascript
运行
复制
- (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;
}

希望这能帮到你。

票数 1
EN

Stack Overflow用户

发布于 2016-06-23 05:13:54

如果您使用alloc init创建RoadmapViewController并从中获取grouplist,那么很明显,您的vc.grouplist将返回nil

删除这两行:

代码语言:javascript
运行
复制
RoadmapViewController *vc = [[RoadmapViewController alloc] init];
cell.textLabel.text =[vc.grouplist objectAtIndex:indexPath.row];

然后这样做:

代码语言:javascript
运行
复制
cell.textLabel.text =[_groupData objectAtIndex:indexPath.row];

在"RoadViewController.m“中添加以下方法:

代码语言:javascript
运行
复制
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(GroupCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{        
    cell.groupData = _groupList;
    [collectionView reloadData];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37982627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档