我在swift 3中的tvOS项目中有我的tableview的代码,我想要彩色单元格,滚动它不焦点!请你解释一下这是怎么回事?
这是我的代码
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tvChannelsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
// Show TV Channels names
cell.textLabel?.text = ((tvChannelsArray[(indexPath as NSIndexPath).row] as AnyObject).object(forKey: "channel") as! String)
cell.imageView?.image = UIImage(named: "\(cell.textLabel!.text!)")
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
channelsTableView.isHidden = false;
return 100
}
答案 0 :(得分:0)
在tableview委托中实现UISsrollViewDelegate方法ScrollViewDidScroll
func scrollViewDidScroll(_ scrollView: UIScrollView) {
for cell in tableView.visibleCells {
if let currentCellPath = tableView.indexPath(for: cell),
let selectedCellPath = tableView.indexPathForSelectedRow {
guard currentCellPath != selectedCellPath else {
continue
}
}
let red = Float(cell.frame.origin.y / scrollView.contentSize.height)
cell.contentView.backgroundColor = UIColor(colorLiteralRed: red, green: 0.5, blue: 0.25, alpha: 1)
}
}