Swift tableView cell set accessory type
Asked Answered
D

4

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?

Drawplate answered 17/6, 2014 at 14:29 Comment(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).Schaub
P
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.

Portwin answered 17/6, 2014 at 14:51 Comment(14)
I want to change the accessory type. How can I change it? (and where?)Drawplate
You can chose any option available at the UITableViewCellAccessoryType enum. If you want a custom accessory view, chef out the property accessoryView of UITableViewCell .Portwin
Do I have to create a class for the cell?Drawplate
i do in this code, but the AccessoryType doesn't changeDrawplate
Have you tried out my code, because it's not working at my macbook :/Drawplate
Did you also add override to your cellForRowAtIndexPath method?Ruisdael
Yes, I was using it at UITableViewController for a faster setup.Portwin
wenn ich das override davorschreibe, kommt der Fehler, dass in der superklasse die Methode nicht vorhanden ist.Drawplate
if you have to write in english, answer why there is an error when writing the override please :)Drawplate
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.Portwin
Thanks dood, you're correct! didn't appear because of the size xDDrawplate
@Portwin 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 clickableInhaul
@Inhaul 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.Portwin
@Portwin thanks but I solved that just adding a custom AccessoryView button and implement its method when it is tapped. Thanks anywayInhaul
H
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
}
Hexone answered 15/5, 2016 at 5:4 Comment(0)
C
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

Chor answered 19/2, 2015 at 10:32 Comment(0)
L
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
Libbielibbna answered 11/3, 2019 at 0:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.