我正在自定义“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/