Swift tableView cell set 附件类型

标签 swift uitableview cell accessorytype

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

    }
}

我的问题是 accessoryType 和 selectionStyle 没有改变。 tableView.separatorStyle 和 cell.textlabel.text 确实发生了变化。 我该如何解决?

最佳答案

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.

至于accessoryType,只要您以后不在其他地方更改它,它应该可以正常工作。确保表格宽度正确,否则附件 View 可能会在屏幕外。

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 时设置一次:at viewDidLoad.

关于Swift tableView cell set 附件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24266467/

相关文章:

ios - 错误: reason: '-[UITableViewCellSelectedBackground setImage:]

swift - 动画多个原型(prototype) tableview 单元格

ios - TableView 不显示超过 10 行

arrays - for 循环中的 MATLAB 和元胞数组处理

swift - 如何向自定义 UITableViewCell 添加约束

ios - uitableview 高度不会在 swift ios 中动态变化?

swift - 带有协议(protocol)对象的纯 swift 集

java - GWT 简单寻呼机帮助

swift - Ios swift相机图像选择器旋转问题

ios - 2 使用 UIPageViewController 的页面 View