私は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の高さを元の設定に設定してセルを再ロードしています。
ラベルの色を設定したら、その行を選択解除すると元の色に戻したいと思います。
私は十分に明確にしたいと思う。前もって感謝します。
あなたが色あなたにラベルを変更することはできません試してみてくださいtableView:didDelectlectRowAt:didSelectRowAt:でそれをリセットしたいですか? – ubiAle
@ubiAleいいえ。didSelectRowAtでラベルを変更すると、行をリロードする必要があります。つまり、UIViewをリロードして、選択したときに行ったすべての変更を失うことになります。 – samuria
なぜ行をリロードする必要がありますか? – ubiAle