2017-01-04 9 views
0

私はTableViewを持っており、テーブルは配列から取り込まれます。セルのタイプはTableViewCell.xibです。私はセル内のラベルの色を変更したい。TableViewCellを設定する行選択のラベルの色Swift 3

は、ここで私が行うことができる何をしたいのか、私のTableViewController


struct cell_data { 
    let label1: String! 
} 

class TableViewController: UITableViewController { 

    let cellDataArray = [cell_data]([cell_data(label1: "This text is for label 1")]) 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return cellDataArray.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell 

     cell.label_1.text = cellDataArray[indexPath.row].label1 
     cell.selectionStyle = .none 

     let whiteRoundedView : UIView = UIView(frame: CGRect(x:10, y:5, width: self.view.frame.size.width - 20, height: cell.frame.size.height - 7)) 
     whiteRoundedView.layer.backgroundColor = UIColor.green.cgColor 
     whiteRoundedView.layer.masksToBounds = false 
     whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1) 
     whiteRoundedView.layer.shadowOpacity = 0.3 

     cell.contentView.addSubview(whiteRoundedView) 
     cell.contentView.sendSubview(toBack: whiteRoundedView) 

     return cell 
    } 

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     let cell = tableView.cellForRow(at: indexPath) 


    } 

    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
     let cell = tableView.cellForRow(at: indexPath) 


    } 
} 

だ私は、細胞内のUIViewに影響を与えることなく、行を選択すると、セルのラベルの色を変更しています。つまり、行を選択するとUIViewの高さを変更すると、再ロードが試行されましたが、UIViewの高さを元の設定に設定してセルを再ロードしています。

ラベルの色を設定したら、その行を選択解除すると元の色に戻したいと思います。

私は十分に明確にしたいと思う。前もって感謝します。

+0

あなたが色あなたにラベルを変更することはできません試してみてくださいtableView:didDelectlectRowAt:didSelectRowAt:でそれをリセットしたいですか? – ubiAle

+0

@ubiAleいいえ。didSelectRowAtでラベルを変更すると、行をリロードする必要があります。つまり、UIViewをリロードして、選択したときに行ったすべての変更を失うことになります。 – samuria

+0

なぜ行をリロードする必要がありますか? – ubiAle

答えて

1

ラベルの色を更新するためにセルを再読み込みする必要はありません。次のコードを使用してTableViewCellのメソッドをオーバーライドして試すことができます:

override func setSelected(selected: Bool, animated: Bool) { 
super.setSelected(selected, animated: animated) 
if (selected) { 
    //Change your label color for selected state 
} else { 
    //Change your label color for unselected state 
} 
} 
+0

OPはまた、セル内のビューの高さを上げたい。そのためには、**セルをリロードする必要があります! – Rikh

+0

これは、拡張可能なセルのようなものですか? その場合、セルを再読み込みして内側ビューの高さとセルの高さを上げることができます。 selectedIndexPathを保存し、セルが再ロードされた後にプログラムでセルを選択しようとします。 –

+0

@Rikh 'tableView.updateBegin'のために高さが現在のコードで更新されていないことを忘れた – samuria

0

これを試してみてください。

func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) 

    { 
     let cell = tableView.cellForRowAtIndexPath(indexPath)! as! customTableViewCell // name of your custom cell class 
     cell.label_1.backgroundColor = UIColor.whiteColor()//give your selection color 

    } 
0

この

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    let cell = tableView.cellForRow(at: indexPath) as yourCell 
    cell.yourlable.textcolor = yourColor; 

} 
関連する問題