2017-01-14 2 views
2

Это может быть немного запутанным, но у меня есть контроллер представления, который имеет представление таблицы, которое содержит пользовательскую ячейку таблицы и внутри этой ячейки таблицы, у меня есть collectionView причем каждая ячейка содержит одно изображение. Я пытаюсь настроить образ моего CollectionCell, используя переменную userToView, показанную ниже.Передача данных из ViewController в ячейку коллекции в ячейке таблицы

Функция Делегат ниже, где ячейка таблицы конфигурируется:

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 
    } 
... 

} 

Функция Делегат ниже, где это изображение создается:

OtherUserPhotosTableViewCell.swift

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 
} 

Мой заказ CollectionViewCell можно увидеть ниже:

OtherUserPhotosCollectionViewCell.swift

class OtherUserPhotosCollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var imageView: UIImageView! 
} 

Я только действительно было привыкания к Swift & развития IOS за последние пару недель, так что любая помощь очень признателен. Спасибо за ваше время!

+0

, что делает 'переменная userToView' содержит? откуда вы получаете изображения? – KrishnaCA

+0

Правильно, я буду использовать массив UIimages –

ответ

0

Ответил, предполагая, что у вас есть UIImage сек уже доступны для вас

Если у вас есть один 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 
} 

В OtherUserAccountViewController.swift

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 
} 

Если у вас есть несколько UICollectionViewCells

В OtherUserPhotosTableViewCell.swift

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 
} 

В OtherUserAccountViewController.swift

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 
} 
+0

Большое вам спасибо! –