如何在Swift 3中使这个UITableView清晰(透明)

RDo*_*wns 16 uitableview ios swift swift3

如何UITableView在Swift 3中清除它并清除它.

我已经完成了之前的线程,但我仍然得到了白色背景.

从我的代码中可以看出,我尝试了各种方法:

override func viewDidLoad() {

    self.communitiesTableView.delegate = self
    self.communitiesTableView.dataSource = self

    let background = CAGradientLayer().bespokeColor()
    background.frame = self.view.bounds
  //      view.addSubview(background)
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
Run Code Online (Sandbox Code Playgroud)

并在我的单元格表功能:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let title = self.communities[indexPath.row]
    let cell = UITableViewCell()


    cell.textLabel?.text = title
    cell.textLabel?.font = UIFont(name: "Avenir", size: 12)
    cell.textLabel?.textColor = UIColor.red
    cell.textLabel?.backgroundColor = UIColor.clear


    cell.contentView.backgroundColor = UIColor.clear
    communitiesTableView.backgroundColor = UIColor.clear
    cell.layer.backgroundColor = UIColor.clear.cgColor

    return cell

}
Run Code Online (Sandbox Code Playgroud)

这个gif显示了这个问题 - 请注意显示表格的黑线只是没有填充(因为没有人登录)

但是很明显,然后变成白色.我哪里错了?

这是我发现的另一篇文章:

Apple文件说

...在iOS 7中,单元格默认为白色背景; 在早期版本的iOS中,单元格继承封闭表视图的背景颜色.如果要更改单元格的背景颜色,请在表视图委托的tableView:willDisplayCell:forRowAtIndexPath:方法中执行此操作.

您可能需要使用willDisplayCell UITableView委托方法为表视图提供透明背景.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath

{
  [cell setBackgroundColor:[UIColor clearColor]];
}
Run Code Online (Sandbox Code Playgroud)

我如何应用上面的代码,就像它在iOS 7中所说的那样?

Joe*_*Joe 47

注意:下面的代码已经过测试Swift 3.

方法1:

从您选择您tableViewCell storyboard并转到Attributes InspectorView改变Backgroundclear

方法2:尝试下面的代码cellForRowAt

  cell.layer.backgroundColor = UIColor.clear.cgColor
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

注意:如果上面的方法不起作用.按下清除项目构建shift + option + command + k

更新:cellForRowAt从以下代码更新您的...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
    cell.textLabel?.text = communities[indexPath.row]
    cell.textLabel?.font = UIFont(name: "Avenir", size: 12)
    cell.textLabel?.textColor = UIColor.red // set to any colour 
    cell.layer.backgroundColor = UIColor.clear.cgColor

    return cell
}
Run Code Online (Sandbox Code Playgroud)


小智 10

你可以试试这个

viewDidLoad中:

tableView.backgroundColor = UIColor.clear
Run Code Online (Sandbox Code Playgroud)

cellForRowAt中:

cell.contentView.backgroundColor = UIColor.clear
Run Code Online (Sandbox Code Playgroud)

这对我有用.

  • 这对我有用 - 添加 - cell.backgroundColor = UIColor.clear (2认同)

ant*_*-ka 7

您必须清除表格和单元格背景.

里面的tableView函数:

tableView.backgroundColor = .clear
cell.backgroundColor = .clear
tableView.tableFooterView = UIView()
Run Code Online (Sandbox Code Playgroud)


小智 5

为此,您必须实现一个新的 tableview 方法:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell,
               forRowAt indexPath: IndexPath) {
    cell.backgroundColor = UIColor.clear
}
Run Code Online (Sandbox Code Playgroud)


El-*_*tos 5

前两个答案对我不起作用,因此我到处都添加了清晰的背景,白色背景消失了。

cell.layer.backgroundColor = UIColor.clear.cgColor
cell.backgroundColor = .clear
tableView.layer.backgroundColor = UIColor.clear.cgColor
tableView.backgroundColor = .clear
Run Code Online (Sandbox Code Playgroud)

SWIFT 4.2