我正在定制的“tableView”内部创建一个“UICollectionViewCell”,而在我的ViewController中,我有一个数组,其中包含要在每一行的TableView中显示的数据。因此,我希望将此数组的数据传递给自定义单元格(包含tableview委托方法)。
,我是这样做的,但帮不上忙。
“GroupCollectionViewCell.m ViewCell.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;
}Note -如果要传递的数组和groupData是自定义单元格内的空白数组,则为groupList。
发布于 2016-06-23 05:02:17
在您的MutableArray中创建一个CollectionViewCell对象,并创建如下方法
@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;
}希望这能帮到你。
发布于 2016-06-23 05:13:54
如果您使用alloc init创建RoadmapViewController并从中获取grouplist,那么很明显,您的vc.grouplist将返回nil。
删除这两行:
RoadmapViewController *vc = [[RoadmapViewController alloc] init];
cell.textLabel.text =[vc.grouplist objectAtIndex:indexPath.row];然后这样做:
cell.textLabel.text =[_groupData objectAtIndex:indexPath.row];在"RoadViewController.m“中添加以下方法:
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(GroupCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
cell.groupData = _groupList;
[collectionView reloadData];
}https://stackoverflow.com/questions/37982627
复制相似问题