uitableviewswiftios8alertviewtextlabel

how to pass boolean from an alertview to a custom tableview cell in swift


***UPDATED: Yay! it works now!***I've been looking around for hours but have not found exactly what I need to figure this out. There are a few answers out there using objective c but I am trying out Swift and I can't seem to get the code to translate. (Bet you can't tell how new I am to this!) I have a simple tableView that shows a list of transportation types from an array. When each cell is pressed, an alert pops up to ask the user if they have used this type of transportation before. They can answer yes or no. If the answer is yes, then I would like to unhide one of the labels in my tableView. Seems like it should be so simple but I'm tearing my hair out. The tableview works perfectly, the alerts work perfectly; I just can't seem to unhide the label in my tableView. I figured I would need to reload the tableView somewhere but I keep getting errors saying that what I was doing was not recommended.

These are the outlets in my custom cell class:

class TransportCell: UITableViewCell {

@IBOutlet weak var transportationTitleLabel: UILabel!
@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var transportationImage: UIImageView!
}

Here is my tableviewcontroller:

class TransportationTableViewController: UITableViewController {

struct TransportItem
{
    var label: String
    var subTitle: String
    var isHidden: Bool
}

let TransportationItems = [TransportItem(label: "Bus", subTitle: "I have used this type before.", isHidden: true),
    TransportItem(label: "Helicopter", subTitle: "I have used this type before.", isHidden: false),
    TransportItem(label: "Truck", subTitle: "I have used this type before.", isHidden: true),
    TransportItem(label: "Boat", subTitle: "I have used this type before.", isHidden: true),
    TransportItem(label: "Bicycle", subTitle: "I have used this type before.", isHidden: true),
    TransportItem(label: "Motorcycle", subTitle: "I have used this type before.", isHidden: true),
    TransportItem(label: "Plane", subTitle: "I have used this type before.", isHidden: true),
    TransportItem(label: "Train", subTitle: "I have used this type before.", isHidden: true),
    TransportItem(label: "Car", subTitle: "I have used this type before.", isHidden: true),
    TransportItem(label: "Scooter", subTitle: "I have used this type before.", isHidden: true),
    TransportItem(label: "Caravan", subTitle: "I have used this type before.", isHidden: true)]



override func viewDidLoad() {
    super.viewDidLoad()
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return TransportationItems.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:TransportCell = tableView.dequeueReusableCellWithIdentifier("transportCell", forIndexPath: indexPath) as TransportCell

    var item = TransportationItems[indexPath.row]

    cell.transportationTitleLabel.text = item.label
    cell.typeLabel.text = item.subTitle
    cell.typeLabel.hidden = item.isHidden

    var imageName = UIImage(named:item.label)
    cell.transportationImage.image = imageName

    return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    var item = self.TransportationItems[indexPath.row]

    let alert = UIAlertController(title: "Used this transport?", message: "You selected \(item.label)", preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel, handler: nil))
    alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { alertAction in

        println(item.label)
        println(item.subTitle)

        var customCell = self.tableView.cellForRowAtIndexPath(indexPath) as TransportCell
        customCell.typeLabel.hidden = false


    }))
    self.presentViewController(alert, animated: true, completion: nil)
}

Solution

  • Modify your datasource (transportationItems) to carry struct instead of String:

    struct TransportItem {
        var label: String
        var hidden: Bool
    }
    
    let transportationItems = [
                               TransportItem(label: "Bus", hidden: true),
                               ........
                               TransportItem(label: "Caravan", hidden: true)
                               ]
    

    And then go to cellForRowAtIndexPath, and do:

    var item = transportationItems[indexPath.row]
    cell.typeLabel?.text = item.label
    cell.typeLabel?.hidden = item.hidden
    

    At last:

    // THIS IS WHERE YOU LOST
    // REMOVE THE CODE YOU HAVE, AND DO THIS
    var item = self.transportationItems[indexPath.row]
    item.hidden = false
    self.tableView.reloadData()