ビューコントローラにテーブルビューを挿入しました。テーブルビューのセルにどのようにグラデーションの背景を付けることができますか?各セルを個別に編集して、各セルに異なる色のグラデーションの背景を付けることはできますか?たとえば、各セルにtrainingLevels
配列内のインデックスの後にラベルが付けられている場合、それぞれの色のグラデーションの背景がどこにあるかをどのようにして作成できますか?あなたが最初のグラデーションを作成し、以下のようなあなたのセルにサブレイヤを挿入する必要が細胞をグラデーションの背景にする方法は?
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var trainingLevels = ["Ball Handling", "Shooting", "Defense", "Advanced Drills", "Vertimax Drills", "Full Workouts", "BHB Products"]
@IBOutlet weak var tableView: TableView!
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.clear
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return trainingLevels.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "bell")
cell.textLabel?.text = trainingLevels[indexPath.row]
//cell details
cell.backgroundColor = UIColor.black
cell.textLabel?.textColor = UIColor.white
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
サブクラス 'UITableViewCell'と勾配層を介して背景を設定します。 – vadian
各セルにグラデーションを設定する必要がありますか?背景にグラデーションカラーを設定するだけで、それぞれのセルで同じ効果が得られます。私によれば、各セルにグラデーションを設定すると遅くなることがあります。 –