iOS - 如何在不同的UITableViewCell中重用UIView

时间:2017-01-16 12:06:15

标签: ios swift uitableview uiview swift3

我正在使用UItableViewController,其中可以有五种类型的UITableViewCell。每种细胞的主要部分是常见的。我可以为所有五个单元格实现这些部分,但我想制作一个可重用的视图并在所有单元格中使用它。设计和实施此要求的最佳方法是什么?

例如,在Facebook Feed中,可以有多种类型的帖子。但每个帖子底部的喜欢/评论/分享面板是相同的。如何在Xcode上的单个位置实现这些不同类型的单元格保持/ comment / share面板。

提前致谢

5 个答案:

答案 0 :(得分:1)

为了重用部分单元格,我建议创建一个单元格,在其中添加单元格之间共有的每个部分,然后将该单元格子类化为主单元格

class MyCommonCell: UITableViewCell {

//declare variables init views and other things your tableview cells will have in common

override init() { //do your initialisation here 
   }
}


class MyCell: MyCommonCell { 
//add here the different items from the cell following the same architecture as above
}

编辑以回答评论:我的问题特定于使用常见的复杂uiview,按钮,操作/目标等。我该如何实现它?

为了做到这一点,你需要创建一个扩展UIView

的类
class MyExampleClass: UIView {}

然后创建一个包含视图的新.xib文件,然后您可以将此视图链接到新的MyExampleClass并向其添加插座

答案 1 :(得分:1)

使用情节提要

您可以做的是将包含所有常用功能的UIView子类化。

例如:

class ViewWithBorder : UIView{

   //any view that you apply this class on in storyboard will
   //have the following properties assigned to it
    override func awakeFromNib() {

        super.awakeFromNib()

        self.layer.borderColor = UIColor.blue.cgColor

        self.layer.borderWidth = 1.0

       //programmatically add buttons and whatever you desire to this view here
    }
}

现在在故事板中,您创建了一个Prototype单元格,只需在单元格中添加UIView并将其类设置为viewWithBorder。在awakeFromNib中分配给它的所有属性都将在运行时添加到该视图中。

注意:如果您以编程方式创建视图而不使用情节提要,则必须覆盖initWithCoder

为新UIView创建 xib ,这将在所有视图中通用,只需在cellForRow中将其添加到您的单元格中即可。

答案 2 :(得分:1)

假设您想要在左侧实现一种带有彩色圆点的单元格:

首先,定义您的自定义单元格类:

import UIKit

@IBDesignable class MyTableViewCell : UITableViewCell {
    @IBInspectable var dotColor: UIColor = UIColor.gray

    override func draw(_ rect: CGRect) {
        super.draw(rect)

        let radius = rect.height / 8
        let center = CGPoint(x: 20, y: rect.midY)

        let context = UIGraphicsGetCurrentContext()!
        context.addArc(center: center, radius: radius, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)
        context.setFillColor(dotColor.cgColor)
        context.fillPath()
    }
}

现在转到Interface Builder并通过将其类更改为MyTableViewCell来设计原型单元格:

Interface Builder

答案 3 :(得分:0)

我认为您的问题的回答应该取决于要求。

如果你想要FB中的工具面板

  

例如,在Facebook Feed中,可以有多种类型的帖子。但每个帖子底部的喜欢/评论/分享面板是相同的。如何在Xcode上的单个位置实现这些不同类型的单元格保持/ comment / share面板。

在我看来,你应该用面板创建小细胞。不要创造"大细胞"。 Cell应该尽可能小,重用将比big cell更快。它也是最简单的"大细胞"与子类化。

所以你会有这样的事情

VideoPost cell
like/comment/share panel cell
ImagePost cell
like/comment/share panel cell
AudioPost cell
like/comment/share panel cell
....

您可以在将来轻松重复使用它。希望它可以帮到你。

答案 4 :(得分:0)

首先,创建一个空的xib并将UIView组件添加到其中。创建一个类(类型:UIView Let假设" CommonView")用xib添加该类,并做必要的连接,Outlet和Action。

Add
var uiview:CommonView?

GOTO :  heightForRowAt{
Set height of cell based on needed calculation.
return height 
}
Then Goto 
cellForRowAt{
 var cell: TableViewCell! = tableView.dequeueReusableCell.............
// Code for your Xib

 self.uiview?.frame = CGRect(x: cell.dynamicLabel.frame.origin.x  , y: cell.dynamicLabel.frame.origin.y + size + (self.uiview?.frame.size.height)! ,width: (self.uiview?.frame.width)!, height: (self.uiview?.frame.height)!)


self.uiview?.uielementAddedToViewXib///////


cell.contentView.addSubview(self.uiview!)


return cell
}

请检查Code here 请让我知道它的工作与否。