22
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet
    var tableView: UITableView
    var items: String[] = ["We", "Heart", "Swift"]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell")
    }


    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as UITableViewCell

        cell.textLabel.text = self.items[indexPath.row]
        cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton
        cell.selectionStyle = UITableViewCellSelectionStyle.Blue
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        return cell
    }

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        println("You selected cell #\(indexPath.row)!")

    }
}

My problem is that the accessoryType and the selectionStyle don't get changed. The tableView.separatorStyle does get changed as well as the cell.textlabel.text. How can I fix that?

1
  • Note: tableView:cellForRowAtIndexPath: is probably not the right place for setting tableView.separatorStyle. I would move that line of code to viewWillAppear, or even try setting it in the interface builder (I don't have access to the latest Xcode, so I cannot give it a quick try). Jun 17, 2014 at 14:47

4 Answers 4

33

UITableViewCell.SelectionStyle.blue

The cell has a default background color when selected.

In iOS 7, the selection color is no longer blue. Use UITableViewCell.SelectionStyle.default instead.

As for the accessoryType, it should work fine as long as you don't change it later somewhere else. Make sure that the table width is correct, otherwise accessory views might be offscreen.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet
    var tableView: UITableView

    var items: String[] = ["We", "Heart", "Swift"]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell")
    }


    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as UITableViewCell
        cell.textLabel.text = self.items[indexPath.row]
        cell.selectionStyle = UITableView.CellSelectionStyle.blue

        /*
        enum UITableViewCellAccessoryType : Int {
        case none // don't show any accessory view
        case disclosureIndicator // regular chevron. doesn't track
        case detailDisclosureButton // info button w/ chevron. tracks
        case checkmark // checkmark. doesn't track
        case detailButton // info button. tracks
        }
        */

        // Standard options
        cell.accessoryType = UITableViewCell.AccessoryType.none
        cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
        cell.accessoryType = UITableViewCell.AccessoryType.detailDisclosureButton
        cell.accessoryType = UITableViewCell.AccessoryType.checkmark
        cell.accessoryType = UITableViewCell.AccessoryType.detailButton

        // Custom view options
        cell.accessoryType = UITableViewCell.AccessoryType.none
        cell.accessoryView = UIView(frame: CGRectMake(0, 0, 20, 20))
        cell.accessoryView.backgroundColor = UIColor.blueColor()

        return cell
    }

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        println("You selected cell #\(indexPath.row)!")

    }
}

Note that it isn't a good solution to set separatorStyle of the table each time the cell is requested, instead do it once when the tableView is loaded: at viewDidLoad.

14
  • I want to change the accessory type. How can I change it? (and where?) Jun 17, 2014 at 14:55
  • You can chose any option available at the UITableViewCellAccessoryType enum. If you want a custom accessory view, chef out the property accessoryView of UITableViewCell .
    – A-Live
    Jun 17, 2014 at 14:55
  • Do I have to create a class for the cell? Jun 17, 2014 at 14:55
  • i do in this code, but the AccessoryType doesn't change Jun 17, 2014 at 14:56
  • 1
    @Nicholas this is more of a general solution rather than mine, same goes for a "clickable" accessory view: for a standard type DetailDisclosureButton you'd need to implement the delegate method - tableView:accessoryButtonTappedForRowWithIndexPath:, for custom accessory views (the ones that you initialize and add yourself) you would use appropriate tools depending on the control type.
    – A-Live
    Jun 1, 2015 at 12:26
10

I didn't have any luck setting it in the cellForRowAtIndexPath method, moving it to willDisplayCell fixed the issue with it not showing up.

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.accessoryType = .DisclosureIndicator
}
1

I would like to share my experience about this, I had the same issue, cell.accessoryType = IUTableViewCellAccessoryType.Checkmark And I noticed my tableview didn't have constraints, so I added missing constraints then it worked for me

0

Below will set your accessoryView as an icon named "sentIcon". Just in case!!!

        let sentImage = UIImage(named: "sentIcon")
        let sentImageView = UIImageView(image: sentImage)
        sentImageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        sentImageView.tintColor = .lightGray
        cell.accessoryView = sentImageView

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.