ios - How to send clicked item of Table View to other view controller when datasource and delegate are separate from View Controller -
i beginner ios coming android background , learned table view (for me it's android listview). trying separate data source & delegate view controller. found tutorials on how stuck @ figuring out how send clicked item view controller. code below:
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 "detail" view controller , typecasting detailviewcontroller if let detailviewcontroller = storyboard.instantiateviewcontroller(withidentifier: "picturedetailview") as? picturedetailviewcontroller { //2 - success! set selecteimage property detailviewcontroller.selectedimgname = picturemodels[indexpath.row].picturename //3 - push onto navigation controller navigationcontroller?.pushviewcontroller(detailviewcontroller, animated: true) } } }
error in: func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath){ }
. since "storyboard" & "navigationcontroller" not available in picturesdatasource class, how can send clicked item(picture name) detailsviewcontroller
there stackoverflow answers separating data source , delegate did not solve problem.
using: xcode 8.3 beta 6
you can include reference main view controller @ table view events handler. below playground code derived example:
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) } } }
Comments
Post a Comment