【问题标题】:Custom UITableViewCell programmatically with SWIFT使用 SWIFT 以编程方式自定义 UITableViewCell
【发布时间】:2016-07-28 02:37:30
【问题描述】:

附上你可能会找到一张(左)我尝试创建类似(右)UITableView 的图片。它们看起来像按钮,但它们只是可以作为表格视图单元格单击的单元格。

我尝试了许多不同的方法,包括在自定义单元格中添加容器窗口,在自定义单元格中添加 UIImage,但我无法复制这些单元格!

我尝试过使用自定义单元类,我尝试过通过 IB 进行操作,但我太疯狂了,无法重新创建它。

谁能给我一个关于如何创建(内部单元格)文本边界框/正方形的提示?与不同的背景颜色照明?

如果这可以通过 IB 轻松完成,我宁愿这样做,但如果您有一个示例 customcell 类,我可以查看一下,我也将不胜感激!

感谢您抽出宝贵时间查看我的问题。

【问题讨论】:

    标签: swift xcode interface-builder ios9


    【解决方案1】:

    我已经为您制作了接近您要求的样品。看一看 https://github.com/RajanMaheshwari/CustomTableCell

    我想使用UITableView 来执行此操作。 我的方法是采用一个自定义单元格并添加一个 UIView,其中包含一些来自左、右、上和下的约束。 此外,我将为 UITableViewUIView 提供相同的背景颜色,这是超级视图和单元格内容视图,并将 UITableViewseparator 设置为 None 并将 TableCell 的选择设置为 None 以便用户界面看起来像

    接下来在应用每个约束并制作 CustomCell 和制作 IBOutlets 之后,我们将跳转到代码。

    我将在 Custom Cell 的 awakeFromNib 方法中完成所有阴影和轮廓

    这将是我的CustomTableViewCell 课程

    class CustomTableViewCell: UITableViewCell {
    
    @IBOutlet weak var labelBackgroundView: UIView!
    @IBOutlet weak var cellLabel: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    
        labelBackgroundView.layer.borderWidth = 0.5
        labelBackgroundView.layer.borderColor = UIColor.lightGrayColor().CGColor
        labelBackgroundView.layer.shadowColor = UIColor.lightGrayColor().CGColor
        labelBackgroundView.layer.shadowOpacity = 0.8
        labelBackgroundView.layer.shadowRadius = 5.0
        labelBackgroundView.layer.shadowOffset = CGSizeMake(0.0, 2.0)
        labelBackgroundView.layer.masksToBounds = false;
    }
    

    我有两个网点。

    一个是您将在其中显示名称的标签。 其他是你想用一些轮廓和阴影显示的外部视图。

    ViewController 代码将是:

    class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    
    var array = [String]()
    
    @IBOutlet weak var myTableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
    
        array = ["Wealth","Health","Esteem","Relationship"]
    
    }
    
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as! CustomTableViewCell
    
        cell.cellLabel.text = array[indexPath.row]
        cell.labelBackgroundView.tag = indexPath.row
        cell.labelBackgroundView.userInteractionEnabled = true
    
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(cellViewTapped))
        cell.labelBackgroundView.addGestureRecognizer(tapGesture)
    
        return cell
    }
    
    
    func cellViewTapped(sender:UITapGestureRecognizer) {
    
        let view = sender.view
        let index = view?.tag
        print(index!)
        }
    }
    

    在这里我没有使用UITableViewDelegate 中的didSelectIndex,因为我只想点击大纲标签背景视图而不是完整单元格。

    所以最后的结果是这样的

    【讨论】:

    • 这太完美了!我非常接近......我在自定义单元格内的容器视图中使用标签......我之前看到的只是设计顶部的空白......无论如何这在我删除后对我有用我的整个故事板和课程,从头开始!非常感谢!
    • 我的错...以为我早点做过...完成了! :) 再次感谢
    【解决方案2】:

    我认为你走在正确的道路上。我在一个项目中做了类似的事情。我创建了 UIView 的子类(也为视图添加了阴影),并在单元格内添加了具有这种类型的视图。

    class ShadowedView: UIView {
    
    override func awakeFromNib() {
        layer.shadowColor = UIColor(red: 157.0/255.0, green: 157.0/255.0, blue: 157.0/255.0, alpha: 0.5).CGColor
        layer.shadowOpacity = 0.8
        layer.shadowRadius = 5.0
        layer.shadowOffset = CGSizeMake(0.0, 2.0)
    }
    }
    

    不要忘记在单元格内的视图中添加一些约束。

    【讨论】:

    • 有趣的是,每次我在“阴影视图”类上放一些东西时,(例如像这样的东西)我都会得到一个像白色盒子一样的东西,由于某种原因覆盖了我的 tableview 的 90%。 ...但我最终使用了与此类似的代码以及来自@Rajan Maheshwari 的代码。谢谢你们!
    【解决方案3】:

    您可以在“尺寸检查器”中安排您的集合视图布局

    并在单元格中自定义您的图像。

    【讨论】:

    • 它确实看起来更接近我的...但是您使用的是集合视图...我知道它们与表格视图几乎相同,但我宁愿坚持使用直到我对其余部分有更多经验为止:)
    猜你喜欢
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多