Ios 将数据从ViewController传递到表格单元格内的集合单元格

Ios 将数据从ViewController传递到表格单元格内的集合单元格,ios,swift3,Ios,Swift3,这可能有点混乱,但我有一个视图控制器,它有一个包含自定义表单元格的表视图,在这个表单元格中,我有一个collectionView,每个单元格都有一个图像。我试图使用下面的userToView变量配置collectionCell的映像 下面的委托函数是配置表格单元格的位置: OtherUserAccountViewController.swift class OtherUserAccountViewController: UIViewController, UITableViewDelegate,

这可能有点混乱,但我有一个视图控制器,它有一个包含自定义表单元格的表视图,在这个表单元格中,我有一个collectionView,每个单元格都有一个图像。我试图使用下面的userToView变量配置collectionCell的映像

下面的委托函数是配置表格单元格的位置:

OtherUserAccountViewController.swift

class OtherUserAccountViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var userToView = User() 

...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:OtherUserPhotosTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: photosCellReuseIdentifier) as! OtherUserPhotosTableViewCell
        cell.selectionStyle = .none

        return cell
    }
...

}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "otherUserPhotosCollectionCell", for: indexPath) as! OtherUserPhotosCollectionViewCell
    //Configure cell with photo from parent ViewController
    return cell
}
class OtherUserPhotosCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!
}
下面的委托函数是设置此图像的位置:

OtherUserPhotosTableViewCell.swift

class OtherUserAccountViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var userToView = User() 

...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:OtherUserPhotosTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: photosCellReuseIdentifier) as! OtherUserPhotosTableViewCell
        cell.selectionStyle = .none

        return cell
    }
...

}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "otherUserPhotosCollectionCell", for: indexPath) as! OtherUserPhotosCollectionViewCell
    //Configure cell with photo from parent ViewController
    return cell
}
class OtherUserPhotosCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!
}
我的自定义CollectionViewCell如下所示:

OtherUserPhotosCollectionViewCell.swift

class OtherUserAccountViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var userToView = User() 

...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:OtherUserPhotosTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: photosCellReuseIdentifier) as! OtherUserPhotosTableViewCell
        cell.selectionStyle = .none

        return cell
    }
...

}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "otherUserPhotosCollectionCell", for: indexPath) as! OtherUserPhotosCollectionViewCell
    //Configure cell with photo from parent ViewController
    return cell
}
class OtherUserPhotosCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!
}

在过去的几周里,我才真正习惯了Swift&iOS的开发,因此非常感谢您的帮助。谢谢你抽出时间

假设您已有可用的
UIImage
s

假设您有一个UICollectionViewCell

OtherUserPhotosTableViewCell.swift中

var photoFromTableView: UIImage?    

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "otherUserPhotosCollectionCell", for: indexPath) as! OtherUserPhotosCollectionViewCell
    //Configure cell with photo from parent ViewController
    if let photo:UIImage = photoFromTableView {
        cell.imageView.image = photo
    }
    return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:OtherUserPhotosTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: photosCellReuseIdentifier) as! OtherUserPhotosTableViewCell
    cell.selectionStyle = .none
    cell.photoFromTableView = //Image you want to pass
    return cell
}
var photosArray: [UIImage]?    

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "otherUserPhotosCollectionCell", for: indexPath) as! OtherUserPhotosCollectionViewCell
    //Configure cell with photo from parent ViewController
    if let photo_array:[UIImage] = photosArray {
        cell.imageView.image = photo_array[IndexPath.row]
    }
    return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:OtherUserPhotosTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: photosCellReuseIdentifier) as! OtherUserPhotosTableViewCell
    cell.selectionStyle = .none
    cell.photosArray = //Array of Images you want to pass
    return cell
}
OtherUserAccountViewController.swift中

var photoFromTableView: UIImage?    

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "otherUserPhotosCollectionCell", for: indexPath) as! OtherUserPhotosCollectionViewCell
    //Configure cell with photo from parent ViewController
    if let photo:UIImage = photoFromTableView {
        cell.imageView.image = photo
    }
    return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:OtherUserPhotosTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: photosCellReuseIdentifier) as! OtherUserPhotosTableViewCell
    cell.selectionStyle = .none
    cell.photoFromTableView = //Image you want to pass
    return cell
}
var photosArray: [UIImage]?    

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "otherUserPhotosCollectionCell", for: indexPath) as! OtherUserPhotosCollectionViewCell
    //Configure cell with photo from parent ViewController
    if let photo_array:[UIImage] = photosArray {
        cell.imageView.image = photo_array[IndexPath.row]
    }
    return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:OtherUserPhotosTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: photosCellReuseIdentifier) as! OtherUserPhotosTableViewCell
    cell.selectionStyle = .none
    cell.photosArray = //Array of Images you want to pass
    return cell
}
假设您有多个UICollectionViewCell

OtherUserPhotosTableViewCell.swift中

var photoFromTableView: UIImage?    

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "otherUserPhotosCollectionCell", for: indexPath) as! OtherUserPhotosCollectionViewCell
    //Configure cell with photo from parent ViewController
    if let photo:UIImage = photoFromTableView {
        cell.imageView.image = photo
    }
    return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:OtherUserPhotosTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: photosCellReuseIdentifier) as! OtherUserPhotosTableViewCell
    cell.selectionStyle = .none
    cell.photoFromTableView = //Image you want to pass
    return cell
}
var photosArray: [UIImage]?    

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "otherUserPhotosCollectionCell", for: indexPath) as! OtherUserPhotosCollectionViewCell
    //Configure cell with photo from parent ViewController
    if let photo_array:[UIImage] = photosArray {
        cell.imageView.image = photo_array[IndexPath.row]
    }
    return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:OtherUserPhotosTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: photosCellReuseIdentifier) as! OtherUserPhotosTableViewCell
    cell.selectionStyle = .none
    cell.photosArray = //Array of Images you want to pass
    return cell
}
OtherUserAccountViewController.swift中

var photoFromTableView: UIImage?    

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "otherUserPhotosCollectionCell", for: indexPath) as! OtherUserPhotosCollectionViewCell
    //Configure cell with photo from parent ViewController
    if let photo:UIImage = photoFromTableView {
        cell.imageView.image = photo
    }
    return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:OtherUserPhotosTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: photosCellReuseIdentifier) as! OtherUserPhotosTableViewCell
    cell.selectionStyle = .none
    cell.photoFromTableView = //Image you want to pass
    return cell
}
var photosArray: [UIImage]?    

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "otherUserPhotosCollectionCell", for: indexPath) as! OtherUserPhotosCollectionViewCell
    //Configure cell with photo from parent ViewController
    if let photo_array:[UIImage] = photosArray {
        cell.imageView.image = photo_array[IndexPath.row]
    }
    return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:OtherUserPhotosTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: photosCellReuseIdentifier) as! OtherUserPhotosTableViewCell
    cell.selectionStyle = .none
    cell.photosArray = //Array of Images you want to pass
    return cell
}

userToView
变量包含什么?你从哪里获得图像?正确,我将使用UIImages数组非常感谢!!