【问题标题】:tableview xcode swift with rounded borders带有圆形边框的tableview xcode swift
【发布时间】:2018-04-14 19:15:35
【问题描述】:

我创建了一个带有自定义原型单元格的表格视图,但我需要塑造边框,这是我现在拥有的图像

the one i get when i run the app

this is the prototype cell

请注意,我为 tableviewcell 创建了一个新类,在其中添加了要从列表中更改的文本字段

我想设置拐角半径 我尝试添加此代码,

layer.cornerRadius = 10 

layer.masksToBounds = true

在用户定义的运行时属性中,就像我之前对按钮所做的那样,但它不起作用

代码如下:

import UIKit

class ListOffersViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    let profil = ["Eric","Eric","Eric","Eric","Eric","Eric"]

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return profil.count
    }


    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ListOffersViewControllerTableViewCell

        cell.profilName.text = profil[indexPath.row]
        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

【问题讨论】:

  • 您希望哪个视图设置圆角? UITavleViewCell?
  • 我想让每个单元格都带有圆角和阴影,我看过其他一些帖子,但有一些代码需要添加,我不知道在哪里添加它跨度>
  • @LLIAJLbHOu 我看过这个stackoverflow.com/questions/33793211/…,但我不知道在哪里添加这段代码
  • 看我的回答。我把它移到 willDisplay 函数中。

标签: ios swift xcode tableview


【解决方案1】:

使用 UITableViewCell 时要专心。您可以使用 contentView 制作的 UI 功能。 这是一个例子。

class ViewController: UITableViewController {

    let identifier = "roundedCell"

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)

        cell.textLabel?.text = "\(indexPath)"

        return cell
    }

    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.backgroundColor = .red
        cell.contentView.backgroundColor = .blue
        cell.contentView.layer.cornerRadius = 10.0
    }

}

【讨论】:

  • 当我添加新功能时,它使用蓝色背景色,我想将其设置为白色,所以当我什么也没发生时,我认为是因为没有阴影,对吧?
  • 设置 cell.backgroundColor = .clear 和 cell.contentView.backgroundColor = .white
  • 当然可以设置影子。但是用 cell.contentView.layer
  • 你能解释一下如何添加阴影
  • 我添加了这段代码,但我只能看到内部内容的阴影,比如按钮、texfields..而不是单元格的边框 cell.contentView.backgroundColor = UIColor.clear cell.contentView.layer .shadowColor = UIColor.darkGray.cgColor cell.contentView.layer.shadowOffset = CGSize(width: 2.0, height: 2.0) cell.contentView.layer.shadowOpacity = 1.0 cell.contentView.layer.shadowRadius = 2
【解决方案2】:

尝试在单元格上添加 UIView 并设置约束,使其与单元格大小相同。将所有设计对象放在该视图中。将该视图连接到您的单元格文件,然后将角半径添加到该视图。确保将单元格视图背景设置为透明,并为新的 UIView 添加颜色。

【讨论】:

  • 加个代码有什么方法吗,我现在用的和你建议的有什么区别?
  • 你这是什么意思?您是在谈论以编程方式添加 UIView 吗?
  • 我遵循了本教程youtube.com/watch?v=uBesaTUJZi0 我不知道 UIView 是如何工作的,因为我是视频中的初学者,他们添加了一个原型单元格,并通过添加一些文本字段和图像对其进行了自定义..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-27
  • 1970-01-01
  • 2015-09-28
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
相关资源
最近更新 更多