我已经为您制作了接近您要求的样品。看一看
https://github.com/RajanMaheshwari/CustomTableCell
我想使用UITableView 来执行此操作。
我的方法是采用一个自定义单元格并添加一个 UIView,其中包含一些来自左、右、上和下的约束。
此外,我将为 UITableView、UIView 提供相同的背景颜色,这是超级视图和单元格内容视图,并将 UITableView 的 separator 设置为 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,因为我只想点击大纲标签背景视图而不是完整单元格。
所以最后的结果是这样的