Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UICollectionView在UITableViewCell内如何将数据传递到Uicollectionviewcell_Swift - Fatal编程技术网

Swift UICollectionView在UITableViewCell内如何将数据传递到Uicollectionviewcell

Swift UICollectionView在UITableViewCell内如何将数据传递到Uicollectionviewcell,swift,Swift,我有一个tableview控制器,里面有一个collectionview,在tableviewcell中我实现了一个水平滚动的集合视图,数据看起来不正确 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = friendsCollectionView.dequeueReus

我有一个tableview控制器,里面有一个collectionview,在tableviewcell中我实现了一个水平滚动的集合视图,数据看起来不正确

   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = friendsCollectionView.dequeueReusableCell(withReuseIdentifier: "friends", for: indexPath) as! friendCollectioncell
    friendsCollectionView.delegate = self
    friendsCollectionView.dataSource = self

    let ava = Myfriends[indexPath.item]["ava"] as! String
    let firstname = Myfriends[indexPath.row]["firstName"] as! String
    let lastname = Myfriends[indexPath.row]["lastName"] as! String


    return cell
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return Myfriends.count
   }
问题出在 返回Myfriends.count 始终为空,因此不会显示任何内容,但如果更改为1或2。它工作正常

在uitableviewcontroller中:

var myFriends: [Dictionary<String, Any>] = []
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // section 1 which includes 1 cell that shows my friends
    if indexPath.section == 0 {

        // accessing cell in main.storyboard that has id "friends". This cell stores my friends
        let cell = tableView.dequeueReusableCell(withIdentifier: "friends", for: indexPath) as! friends



        cell.Myfriends = myFriends


       }


        return cell

        // section 2 (or any other sections) that shows all the posts of the user
    } else {
return cell 
}
var myFriends:[字典]=[]
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
//第1节,其中包括一个显示我朋友的单元格
如果indexath.section==0{
//正在访问main.storyboard中id为“friends”的单元格。此单元格存储我的朋友
让cell=tableView.dequeueReusableCell(标识符为:“friends”,for:indexPath)作为!friends
cell.Myfriends=Myfriends
}
返回单元
//显示用户所有帖子的第2部分(或任何其他部分)
}否则{
返回单元
}
提前感谢您

编辑:
如果我理解正确:

问题在于Myfriends.count始终为空,所以不会显示任何内容,但如果更改为1或2。它工作正常

可能在设置此
cell.Myfriends=Myfriends
之前会调用您的
collectionView(uuquo:numberofitemsinssection:)

在您的好友tableViewCell中:

class friends: UITableViewCell .. {
    ...
    var Myfriends: [Friend] {
        didSet {
            friendsCollectionView.reloadData()
        }
    }
}

Imho您的
集合视图(\uuOfItemsInSection:)
根本不被调用。
此代码

friendsCollectionView.delegate = self
friendsCollectionView.dataSource = self
在您的
collectionView(uu1;:cellForItemAt:)中,
是无用的,因为如果调用此方法,数据源(可能还有委托)已正确设置。
相反,请将您的tableView(u:cellForRowAt:)更改为

或者在初始化collectionView的friends单元格中设置friends collectionView数据源和委托

如果这对你没有帮助,检查一下你在哪里以及如何填充你的myFriends字典


除此之外,我建议您创建一个struct Friend:

struct Friend {
    let ava: String
    let firstName: String
    let lastName: String
}
并将您的
var myFriends
更改为
var myFriends:[friends]=[]

这将使访问friendsCollectionCell的值更容易、更安全:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = friendsCollectionView...

    let friend = MyFriends[indexPath.item]
    let ava = friend.ava
    ...

    return cell
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = friendsCollectionView...

    let friend = MyFriends[indexPath.item]
    let ava = friend.ava
    ...

    return cell
}