Swift: gradient on a cell of a tableview

后端 未结 3 1808
轻奢々
轻奢々 2021-01-28 16:32

On tableView\'s cells, I\'m trying to define a gradient with the following Swift code:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: N         


        
3条回答
  •  天涯浪人
    2021-01-28 17:01

    The gradient is a visual ornamentation. It is specific to the cell class. How the cell looks is the responsibility of the cell. It should be defined in the cell class then. It absolutely does not belong to the cellForRowAtIndexPath which should be solely about choosing the right cell for the right row. But you are configuring the internals of the cell in that method and this, combined with the way how queueing works, causes all the issues.

    1) Create a subclass of UITableViewCell (programatically, IB..it doesn't really matter)

    2) Add the layer in awakeFromNib or layoutSubviews to the backroundView property.

    3) register the cell in viewDidLoad in your tableViewController.

提交回复
热议问题