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?

Cal
  • 422
  • 6
  • 20
user3748930
  • 241
  • 1
  • 2
  • 5
  • 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). – Sergey Kalinichenko Jun 17 '14 at 14:47

4 Answers4

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.

Cal
  • 422
  • 6
  • 20
A-Live
  • 8,904
  • 2
  • 39
  • 74
  • I want to change the accessory type. How can I change it? (and where?) – user3748930 Jun 17 '14 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 '14 at 14:55
  • Do I have to create a class for the cell? – user3748930 Jun 17 '14 at 14:55
  • i do in this code, but the AccessoryType doesn't change – user3748930 Jun 17 '14 at 14:56
  • Have you tried out my code, because it's not working at my macbook :/ – user3748930 Jun 17 '14 at 15:02
  • Did you also add `override` to your `cellForRowAtIndexPath` method? – lukaswelte Jun 17 '14 at 15:06
  • Yes, I was using it at `UITableViewController` for a faster setup. – A-Live Jun 17 '14 at 15:07
  • wenn ich das override davorschreibe, kommt der Fehler, dass in der superklasse die Methode nicht vorhanden ist. – user3748930 Jun 17 '14 at 15:19
  • if you have to write in english, answer why there is an error when writing the override please :) – user3748930 Jun 17 '14 at 15:27
  • You don't need to override the delegate & datasource protocols methods as they are not implemented at `UIViewController`, hence the error when you try. I added full code, it doesn't really have much different from your code, only added options to select from. Make sure your table view is sized correctly, it might take more width than window is able to display - then you would not see the accessory views. – A-Live Jun 17 '14 at 15:29
  • Thanks dood, you're correct! didn't appear because of the size xD – user3748930 Jun 17 '14 at 15:36
  • @A-Live your solution is nice but the accessory button after the modification cannot be tapped. Is there a way to customize the accessory button and make it clickable – Nicholas May 31 '15 at 06:15
  • 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 01 '15 at 12:26
  • @A-Live thanks but I solved that just adding a custom AccessoryView button and implement its method when it is tapped. Thanks anyway – Nicholas Jun 02 '15 at 07:49
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
}
Derek Hewitt
  • 775
  • 8
  • 16
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

Salif
  • 992
  • 1
  • 8
  • 14
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
coders
  • 2,287
  • 1
  • 12
  • 20