Swift tableView单元格配件类型 [英] Swift tableView cell set accessory type

查看:96
本文介绍了Swift tableView单元格配件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)!")

    }
}

我的问题是附件类型和selectionStyle不会更改。
tableView.separatorStyle以及cell.textlabel.text确实得到更改。
我该如何解决?

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?

推荐答案


UITableViewCell.SelectionStyle.blue

单元格在选定时具有默认背景色。

The cell has a default background color when selected.

在iOS 7中,选择颜色不再是蓝色。使用
UITableViewCell.SelectionStyle.default代替。

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

对于 accessoryType ,只要您以后不要在其他地方更改它,它就可以正常工作。确保表宽度正确,否则附件视图可能不在屏幕上。

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)!")

    }
}

请注意每次请求单元格时都设置表的 separatorStyle 并不是一个好的解决方案,而是在 tableView 已加载:位于 viewDidLoad

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.

这篇关于Swift tableView单元格配件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆