Search code examples
iosswiftuitableviewsegue

Swift: Disclosure Indicator not showing if Segue


Im using ViewController with a Tableview within.

If I activate "Disclosure Indicator" and do a segue, the Disclosure Icon is not showing.

If I comment the line: self.myTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myTableCell"), the Disclosure Indicator is showing, but the segue is not working anymore.

Here are my code snippets:

override func viewDidLoad() {
    self.myTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myTableCell")
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "myTableCell"
    var cell = self.myTable.dequeueReusableCellWithIdentifier(cellIdentifier)

    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)
    }

    let resultCarForList_data = resultCarNameForList[indexPath.row]
    cell!.textLabel?.text = resultCarForList_data

    return cell!
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    selectedCarID = resultCarIDList[indexPath.row]
    performSegueWithIdentifier("klickCarDetail", sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if(segue.identifier == "klickCarDetail") {
        let DestViewController : CarDetail = segue.destinationViewController as!CarDetail
        DestViewController.passedCarID = selectedCarID
    }
}

Where can the problem be?


Solution

  • To show the disclosure indicator, set the accessoryType property:

    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
    }
    

    For Swift 4:

    cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
    

    As for why the segue isn't working: you can use two types of segues in this case. One is to make the UITableViewCell the origin of the segue (in the Storyboard); in that case, Interface Builder will automatically add the disclosure indicator. A call to performSegueWithIdentifier("klickCarDetail", sender: self) is not necessary in this case (nor is the call to registerClass); the segue will be triggered automatically when the user selects the row. The other one, which you are using currently, is to use a manual segue from one view controller to another.