UITableViewCell项目选择

时间:2016-07-21 11:23:37

标签: ios swift uitableview

我正在{"cols":[{"label":"first_header","type":"string"}, {"label":"second_header","type":"number"}],"rows":[{"c":[{"v":"cwood1r"}, {"v":5}]},{"c":[{"v":"sward2h"},{"v":2}]},{"c":[{"v":"etorres19"},{"v":2}]}, {"c":[{"v":"mfranklinr"},{"v":3}]},{"c":[{"v":"mspencerd"},{"v":21}]},{"c": [{"v":"jknight1y"},{"v":5}]},{"c":[{"v":"hhoward3"},{"v":4}]},{"c": [{"v":"pross6"},{"v":8}]},{"c":[{"v":"swagner7"},{"v":23}]},{"c": [{"v":"ajohnston1a"},{"v":8}]},{"c":[{"v":"dalexander1q"},{"v":2}]},{"c": [{"v":"rmendoza1u"},{"v":2}]},{"c":[{"v":"mmurphy1f"},{"v":5}]},{"c": [{"v":"rthomasp"},{"v":8}]},{"c":[{"v":"wsullivan5q"},{"v":1}]},{"c": [{"v":"bperkins3f"},{"v":3}]},{"c":[{"v":"csimpson1j"},{"v":4}]},{"c": [{"v":"mortiz2e"},{"v":10}]},{"c":[{"v":"sriley1h"},{"v":2}]},{"c": [{"v":"tbryantf"},{"v":10}]},{"c":[{"v":"esimmons15"},{"v":5}]},{"c": [{"v":"psullivan35"},{"v":2}]},{"c":[{"v":"jwatson9"},{"v":7}]},{"c": [{"v":"jcampbell1v"},{"v":2}]},{"c":[{"v":"rford14"},{"v":5}]},{"c": [{"v":"jnichols4m"},{"v":1}]},{"c":[{"v":"agreenm"},{"v":1}]},{"c": [{"v":"rmorris1"},{"v":4}]},{"c":[{"v":"pboyd16"},{"v":1}]},{"c": [{"v":"jdixon2"},{"v":12}]},{"c":[{"v":"kbrownv"},{"v":8}]},{"c": [{"v":"slarson2l"},{"v":2}]},{"c":[{"v":"lrileyn"},{"v":9}]},{"c": [{"v":"mholmes1t"},{"v":5}]},{"c":[{"v":"phill4"},{"v":5}]},{"c": [{"v":"pwalkerh"},{"v":2}]},{"c":[{"v":"thawkins1g"},{"v":6}]},{"c": [{"v":"crussell0"},{"v":27}]},{"c":[{"v":"cweaver3s"},{"v":2}]},{"c": [{"v":"fgonzalesb"},{"v":8}]},{"c":[{"v":"elopez10"},{"v":3}]},{"c": [{"v":"pnichols4f"},{"v":1}]},{"c":[{"v":"swallace1i"},{"v":4}]}]} 中添加UIButton,我正在尝试更改表格视图单元格选择中的UITableViewUIButton background colorUIButton title color反之亦然。

我的代码是

UIButton image color

//在UIButton Action上 我的代码是

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
         let propertyCell = tablePropertyList.cellForRowAtIndexPath(indexPath) as! PropertyListTableCell

        propertyCell.buttonDownload.selected = true
        propertyCell.buttonDownload.setTitleColor(utility!.uicolorFromHex(0xf8f8f8), forState: UIControlState.Selected)
        let image:UIImage = UIImage(named: "DownloadSelected")!

        propertyCell.buttonDownload.setImage(image, forState: UIControlState.Selected)
        propertyCell.buttonDownload.backgroundColor = utility!.uicolorFromHex(0x006747)
        propertyCell.buttonDownload.layer.borderColor = utility!.uicolorFromHex(0x006747).CGColor

    }

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
    {
        let propertyCell = tablePropertyList.cellForRowAtIndexPath(indexPath) as! PropertyListTableCell

        propertyCell.buttonDownload.selected = false
        propertyCell.buttonDownload.setTitleColor(utility!.uicolorFromHex(0x006747), forState: UIControlState.Normal)
        let image:UIImage = UIImage(named: "Download")!

        propertyCell.buttonDownload.setImage(image, forState: UIControlState.Normal)
        propertyCell.buttonDownload.backgroundColor = utility!.uicolorFromHex(0xf8f8f8)
        propertyCell.buttonDownload.layer.borderColor = utility!.uicolorFromHex(0xf8f8f8).CGColor

 }

2 个答案:

答案 0 :(得分:0)

单元类的awakeFromNib方法可用于改变titlecolor,基于状态的按钮图像。请参考以下代码

   class PropertyListTableCell : UITableViewCell
    {
     func awakeFromNib()
         {
        propertyCell.buttonDownload.setTitleColor(utility!.uicolorFromHex(0x006747), forState: UIControlState.Normal)

      propertyCell.buttonDownload.setTitleColor(utility!.uicolorFromHex(0xf8f8f8), forState: UIControlState.Selected)

        }
    }

func downloadViewAction(sender: UIButton)
{
    sender.selected = true //automatically changes titlecolor of button
}

答案 1 :(得分:0)

如果我做得对。每个PropertyListTableCell内都有一个按钮,您希望根据单元格的选择状态更改按钮的显示方式。 如果是这样,则以这种方式覆盖setSelected(_:animated:)类中的PropertyListTableCell函数,无论何时选择单元格或取消选择按钮外观都会自动更改。

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: true);

    if (selected) {
        self.buttonDownload.backgroundColor = utility!.uicolorFromHex(0x006747)
        self.buttonDownload.layer.borderColor = utility!.uicolorFromHex(0x006747).CGColor
    }
    else {
        propertyCell.buttonDownload.backgroundColor = utility!.uicolorFromHex(0xf8f8f8)
        propertyCell.buttonDownload.layer.borderColor = utility!.uicolorFromHex(0xf8f8f8).CGColor
    }

    self.buttonDownload.selected = true;
}

如上面评论的Savitha,按钮图像和标题颜色只能在awakeFromNib中定义一次(例如)。

并且无需更改按钮动作功能中的任何视觉效果,因为在选择单元格时已完成:

func downloadViewAction(sender: UIButton) {
    let cell = sender as! as! PropertyListTableCell;
    let indexPath = tableView.indexPathForCell(cell);
    tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: .None);

     print("inside ownload action view")
     let selectedproperty = propertyArray[path.row]
     self.showActivityIndicator(splitView.view, message: "Downloading properties for "+selectedproperty)
}