2016-07-21 16 views
0

UITableViewUIButtonを追加しました。テーブルビューのセル選択でUIButton background color,UIButton title colorおよびUIButton image colorを変更しようとしています。逆も同様です。UITableViewCell項目の選択

私のコードは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 

} 

//ある 私のコードは、セルクラスの

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) 
     } 
    } 
+1

は、あなたの質問を得ていない、あなたはそうすることに直面しているどのような問題は、伝えることができますか? –

答えて

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 
} 
+0

いいえ、スクロール –

+0

で期待通りの結果が得られていないのですか?セルにawakeFromNibを使用していますか? – Savitha

+0

いいえ、ストーリーボード –

0

私が正しくなったら、 PropertyListTableCellのボタンがあり、そのセルの選択状態に応じてボタンの表示方法を変更する必要があります。 セルが選択または選択解除されると、ボタンの外観が自動的に変更されるたびに、PropertyListTableCellクラスのsetSelected(_:animated:)関数をこのように上書きします。

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) 
}