菜鸟教程小白 发表于 2022-12-11 20:23:53

swift - 如何使单元格具有渐变背景?


                                            <p><p>我在 ViewController 上插入了一个表格 View ,我想知道如何为表格 View 中的单元格提供渐变背景?我可以单独编辑每个单元格,为每个单元格赋予不同的颜色渐变背景吗?例如,每个单元格都在 <code>trainingLevels</code> 数组中的索引之后标记,我如何才能使每个单元格具有不同的颜色渐变背景?</p>

<p>这是我的代码:</p>

<pre><code>import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var trainingLevels = [&#34;Ball Handling&#34;, &#34;Shooting&#34;, &#34;Defense&#34;, &#34;Advanced Drills&#34;, &#34;Vertimax Drills&#34;, &#34;Full Workouts&#34;, &#34;BHB Products&#34;]


@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) -&gt; Int
{

    return trainingLevels.count
}


public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell
{
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: &#34;bell&#34;)
    cell.textLabel?.text = trainingLevels

    //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.
}
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以创建 UIView 的扩展:</p>

<pre><code>extension UIView{
    func addGradientBackground(firstColor: UIColor, secondColor: UIColor){
      clipsToBounds = true
      let gradientLayer = CAGradientLayer()
      gradientLayer.colors =
      gradientLayer.frame = self.bounds
      gradientLayer.startPoint = CGPoint(x: 0, y: 0)
      gradientLayer.endPoint = CGPoint(x: 0, y: 1)
      print(gradientLayer.frame)
      self.layer.insertSublayer(gradientLayer, at: 0)
    }
}
</code></pre>

<p>在你的牢房里:</p>

<pre><code>override func awakeFromNib() {
    super.awakeFromNib()
    self.addGradientBackground(firstColor: .green, secondColor: .blue)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于swift - 如何使单元格具有渐变背景?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/51448584/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/51448584/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: swift - 如何使单元格具有渐变背景?