Pavel Zagorskyy
Pavel Zagorskyy

Reputation: 463

configure tableview cell in previous controller

I have got controller 1 with an imageview, uitextview and button. On button click there is an action to show controller with table view. I need to configure this image view and uitextview.text in my table view, but i don't understand how?

I tried to get access

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    var header = tableView.dequeueReusableCellWithIdentifier("customHeader") as CustomHeaderCell
    var controller = PreparePhotoViewController()
    header.headerLabel.text = controller.textView.text//exc BAD ACCESS - textView is an outlet of controller, it is weak
    header.headerPhoto.image = UIImage(named: "heart.png")

    return header
}

but it has bad ACCESS exc, cause I think that uitextview with weak property is nil. So any advices how I can do my idea?

Upvotes: 0

Views: 73

Answers (2)

Jonauz
Jonauz

Reputation: 4133

I'm not sure about your approach, but I can see issue in your posted code. You are using wrong dequeue function. Change tableView.dequeueReusableCellWithIdentifier to tableView.dequeueReusableHeaderFooterViewWithIdentifier().

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    var header = tableView.dequeueReusableHeaderFooterViewWithIdentifier("customHeader") as CustomHeaderCell
    ...
    return header
}

And you would probably want to use more accurate name CustomHeaderView, not CustomHeaderCell.

Upvotes: 0

Joseph Duffy
Joseph Duffy

Reputation: 4826

Add a property to the view controller you are going to be showing, such as:

var textToDisplay: String?

Then, before you push this new view, set that property, e.g.:

let viewController = [...]
viewController.textToDisplay = "Hello World"
self.presentViewController(viewController, animated: true, completion: nil)

Then, in your pushed view controller's viewDidLoad method, set the value of the UILabel's text property to your new self.textToDisplay value.

Hopefully that's the sort of thing you're thinking of?

Upvotes: 1

Related Questions