uitableview - Passing information to a custom TableViewCell - Swift -
the problem this: tried many ways pass down information viewcontroller1 secondviewcontroller. in first viewcontroller1 have tableview, , in tableview, each cell receive image , label. want is, in secondviewcontroller, write in textfield , pick random image , display in viewcontroller1.
could tell me how it? tried delegates, dequeuereusablecell , data won't display in cell.
thanks in advance!
edit
here's code:
viewcontroller 1:
import uikit
class firstviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource, senddatatofvcdelegate {
@iboutlet var tableview: uitableview! var labelslistarray = [""] override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func myvcdidfinish(text: string) { labelslistarray.append(text) tableview.reloaddata() } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return labelslistarray.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let row = indexpath.row let textlabel = labelslistarray[row] var cell = tableview.dequeuereusablecellwithidentifier("imageview") as? customcelltableviewcell cell!.celllabel!.text = textlabel return cell! }
}
viewcontroller 2:
import uikit
protocol senddatatofvcdelegate { func myvcdidfinish(text: string) }
class secondviewcontroller: uiviewcontroller {
var delegate:senddatatofvcdelegate? @iboutlet var textfield: uitextfield! override func viewdidload() { super.viewdidload() // additional setup after loading view. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } @ibaction func addbutton(sender: anyobject) { if textfield == nil { return } let name = textfield.text println("\(name)") if delegate == nil { return } delegate?.myvcdidfinish(name) if let navigation = self.navigationcontroller { navigation.popviewcontrolleranimated(true) } }
}
tableviewcell :
import uikit
class customcelltableviewcell: uitableviewcell {
@iboutlet var cellimage: uiimageview! @iboutlet var celllabel: uilabel! override func awakefromnib() { super.awakefromnib() // initialization code } override func setselected(selected: bool, animated: bool) { super.setselected(selected, animated: animated) // configure view selected state }
}
what doing wrong? line "popviewcontrolleranimated" going first view controller.
thanks help!!
so custom tableviewcell should have it's own class (for example imagetableviewcell
) takes care of image set when loading tableview:
@iboutlet var titlelabel: uilabel! @iboutlet var imageview: uiimageview!
and in tableview:
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell = tableview.dequeuereusablecellwithidentifier("imageview") as? imagetableviewcell cell!.titlelabel!.text = textfrom2vc cell!.imageview.image = imagefrom2vc return cell! }
and have these other view delegate. in second vc have
protocol secondvcdelegate{ func myvcdidfinish(controller:secondvc, text: string, image: uiimage) }
which call
var delegate:secondvcdelegate.myvcdidfinish(self, text: "this random text", image: (your image) )
and in first (which confirms protocol secondvcdelegate) have
func myvcdidfinish(controller: secondvc, text: string, image: uiimage) { textfrom2vc = text imagefrom2vc = image controller.navigationcontroller?.popviewcontrolleranimated(true) self.tableview.reloaddata() }
Comments
Post a Comment