当数据源和委托与View Controller分离时,如何将表视图的单击项发送到其他视图控制器

时间:2017-09-10 12:58:19

标签: ios swift uitableview

我是来自Android背景的iOS初学者,刚刚学习了桌面视图(对我而言,它是一个Android ListView)。我正在尝试分离数据源和来自视图控制器的委托。我找到了一些关于如何这样做的教程,但坚持找出如何将点击的项目发送到另一个视图控制器。代码如下:

class PictureTableViewController: UIViewController {


    @IBOutlet weak var pictureTableView: UITableView!

    private let picsDataSource: PicturesDataSource

    required init?(coder aDecoder: NSCoder) {
        self.picsDataSource = PicturesDataSource()
        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        pictureTableView.dataSource = picsDataSource
        pictureTableView.reloadData()
        pictureTableView.delegate = picsDataSource
    }
}

class PicturesDataSource: NSObject, UITableViewDataSource, UITableViewDelegate{

    private var pictureModels = [PictureModel]()

    override init(){
        let picModelsDataController = PictureModelsDataController()
        pictureModels = picModelsDataController.pictureModels
    }


    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return pictureModels.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PictureCell.self)) as! PictureCell

        let picModel = pictureModels[indexPath.row]
        cell.pictureName = picModel.pictureName
        cell.imageItem = picModel.imageItem

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


        //1 - try loading the "Detail" view controller and typecasting it to be DetailViewController
        if let detailViewController = storyboard.instantiateViewController(withIdentifier: "PictureDetailView") as? PictureDetailViewController {

            //2 - success! set its selecteImage property
            detailViewController.selectedImgName = pictureModels[indexPath.row].pictureName

            //3 - now push it onto the navigation controller
            navigationController?.pushViewController(detailViewController, animated: true)
        }
    }

}     

Error in: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ }自"故事板" &安培; " navigationController"在PicturesDataSource类中不可用,如何将单击的项目(图片名称)发送到DetailsViewController

有关于分离数据源和委托的StackOverflow答案,但没有解决我的问题。

  

使用:Xcode 8.3 beta 6

3 个答案:

答案 0 :(得分:1)

您可以在表视图事件处理程序中包含对主视图控制器的引用。下面是我从你的例子中得到的游乐场代码:

import UIKit

// MARK: - Model

struct Picture {
  let title: String
  let image: UIImage
}

struct PictureModelsDataSource {
  let pictures = [
    Picture(title: "exampleTitle", image: UIImage(named: "exampleImage")!),
    Picture(title: "exampleTitle", image: UIImage(named: "exampleImage")!)
  ]
}

// MARK - View

class PictureCell: UITableViewCell {
  @IBOutlet weak var pictureTitleLabel: UILabel!
  @IBOutlet weak var pictureImage: UIImageView!
}

// MARK: - Controller

class PictureTableViewController: UIViewController {

  // MARK: - Properties

  @IBOutlet weak var pictureTableView: UITableView!

  private var pictureListController: PictureListController?

  // MARK: - View lifecycle

  override func viewDidLoad() {
    super.viewDidLoad()
    pictureListController = PictureListController()
    pictureListController?.viewController = self
    pictureTableView.dataSource = pictureListController
    pictureTableView.delegate = pictureListController
    pictureTableView.reloadData()
  }
}

class PictureDetailViewController: UIViewController {
  var selectedPictureTitle: String?
}

class PictureListController: NSObject, UITableViewDataSource, UITableViewDelegate {

  // MARK: - Properties

  weak var viewController: PictureTableViewController?

  private let pictures: [Picture] = {
    let pictureModelsDataSource = PictureModelsDataSource()
    return pictureModelsDataSource.pictures
  }()

  // MARK: - View setup

  func numberOfSections(in tableView: UITableView) -> Int {
    return 1
  }

  // MARK: - Event handling

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return pictures.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PictureCell.self)) as? PictureCell else {
      return UITableViewCell()
    }

    let picture = pictures[indexPath.row]
    cell.pictureTitleLabel.text = picture.title
    cell.pictureImage.image = picture.image

    return cell
  }

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let pictureTitle = pictures[indexPath.row].title
    let storyboard = UIStoryboard(name: "exampleStoryboard", bundle: nil)
    if let pictureDetailViewController = storyboard.instantiateViewController(withIdentifier: "PictureDetailView") as? PictureDetailViewController {
      pictureDetailViewController.selectedPictureTitle = pictureTitle
      viewController?.navigationController?.pushViewController(pictureDetailViewController, animated: true)
    }
  }
}

答案 1 :(得分:1)

使用此

可以获得StoryBoard对象
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

现在你的第二个问题是关于如何获得导航控制器。这意味着如何在您的案例中获得currentViewController。这可以通过以下代码获得

 func getCurrentViewController() -> UIViewController? {

 if let rootController = UIApplication.shared.keyWindow?.rootViewController {
    var currentController: UIViewController! = rootController
       while( currentController.presentedViewController != nil ) {
                currentController = currentController.presentedViewController
            }
            return currentController
        }
        return nil
    }

现在,您的didSelectRowAt代码将如下所示

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

 if let detailViewController = storyboard.instantiateViewController(withIdentifier: "PictureDetailView") as? PictureDetailViewController 
   detailViewController.selectedImgName = pictureModels[indexPath.row].pictureName
    self.getCurrentViewController()!.pushViewController(detailViewController, animated: true)  
}

答案 2 :(得分:-1)

您正在尝试遵守MVC,但您对实际数据源的含义感到困惑。

PicturesDataSource不是您的数据来源。这是代码告诉您的表格如何设置自己。

PictureModelsDataController()是从中获取实际填充该表的数据的源。

您发布的所有代码都应该在同一个类中:

class PictureTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

更改这些行:

pictureTableView.dataSource = picsDataSource
pictureTableView.delegate = picsDataSource

pictureTableView.dataSource = self  // Note use of self because this is now the dataSource, not another class
pictureTableView.delegate = self  // Note use of self because this is now the delegate, not another class

并删除:

private let picsDataSource: PicturesDataSource

required init?(coder aDecoder: NSCoder) {
    self.picsDataSource = PicturesDataSource()
    super.init(coder: aDecoder)
}