ios - UITableViewCell 项目选择

标签 ios swift uitableview

我正在UITableView中添加UIButton,我正在尝试更改UIButton背景颜色UIButton标题颜色 TableView 单元格选择上的 code> 和 UIButton 图像颜色 ,反之亦然。

我的代码是

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

 }

//UIButton 操作 我的代码是

func downloadViewAction(sender: UIButton)
    {
        sender.selected = true
        sender.setTitleColor(utility!.uicolorFromHex(0xf8f8f8), forState: UIControlState.Selected)
        let image:UIImage = UIImage(named: "DownloadSelected")!

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

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

最佳答案

cell 类的 awakeFromNib 方法可用于根据状态更改标题颜色、按钮图像。请引用以下代码

   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
}

关于ios - UITableViewCell 项目选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38502964/

相关文章:

ios - hidesBarsOnSwipe 不起作用

iphone - AVAudioRecorder 中的音量

swift - 如何为我的枚举返回空白值

Swift如何导入rsa key

在 tableview 上调用 selectRowAtIndexPath 时出现 Swift 错误

ios - 如何在 iOS 的视频 (.mov) 中添加元数据

iphone - 将 CC3Node 的位置设置在触摸位置

swift - 使用未解析的标识符 "Form"

ios - 断言没有重用表单元格的索引路径

performance - UITableView 加载更多内容而无需重新加载完整表格