UITableViewCells 之间的间距 [英] spacing between UITableViewCells
问题描述
我正在快速创建一个 ios 应用程序,并希望在像 Facebook 这样的单元格之间添加间距(如下图).
我正在为帖子使用自定义笔尖.我知道使用 UITableViewController.我想我会使用分隔符样式,但它没有达到效果.我看了几个小时,找不到一个简单的 swift 教程!有人可以解释一下他们是如何使用 swift 在那里的应用程序中做到的吗?谢谢!
这是我的解决方案,结果是:(基于 Jorge Casariego 的回答)
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!自定义应用单元cell.contentView.backgroundColor = UIColor.clear让 whiteRoundedView : UIView = UIView(frame: CGRectMake(10, 8, self.view.frame.size.width - 20, 149))whiteRoundedView.layer.backgroundColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), [1.0, 1.0, 1.0, 0.8])whiteRoundedView.layer.masksToBounds = falsewhiteRoundedView.layer.cornerRadius = 2.0whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1)whiteRoundedView.layer.shadowOpacity = 0.2cell.contentView.addSubview(whiteRoundedView)cell.contentView.sendSubviewToBack(whiteRoundedView)返回单元格}
表格行高:165 点
页眉部分高度、页脚部分高度:10点
<块引用>和结果
针对 Swift 3 语法进行
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as!自定义应用单元cell.contentView.backgroundColor = UIColor.clear让 whiteRoundedView : UIView = UIView(frame: CGRect(x: 10, y: 8, width: self.view.frame.size.width - 20, height: 120))whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9])whiteRoundedView.layer.masksToBounds = falsewhiteRoundedView.layer.cornerRadius = 2.0whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)whiteRoundedView.layer.shadowOpacity = 0.2cell.contentView.addSubview(whiteRoundedView)cell.contentView.sendSubview(toBack: whiteRoundedView)返回单元格}
<小时><块引用>
创建材料卡片视图的最简单方法:
创建CardView.swift
@IBDesignable类 CardView: UIView {@IBInspectable var cornerRadius: CGFloat = 2@IBInspectable var shadowOffsetWidth: Int = 0@IBInspectable var shadowOffsetHeight: Int = 3@IBInspectable var shadowColor:UIColor?= .黑色@IBInspectable var shadowOpacity: Float = 0.5覆盖 func layoutSubviews() {layer.cornerRadius = 角半径让 shadowPath = UIBezierPath(roundedRect: bounds,cornerRadius:cornerRadius)layer.masksToBounds = falselayer.shadowColor = shadowColor?.cgColorlayer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight)layer.shadowOpacity = shadowOpacitylayer.shadowPath = shadowPath.cgPath}}
现在只需将 CardView 类添加到您的 UIView.
I am creating a ios app in swift and want to add spacing between cells like Facebook (pic bellow).
I am using a custom nib for the posts. I know to use UITableViewController. I figure I would use a separator style but it does not achieve the effect. I goggled around for hours and can't find a single tutorial in swift that makes sense! could some one explain how they did it in there app using swift? thanks!
This is my solution with result: (based on Jorge Casariego's answer)
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomApplicationCell
cell.contentView.backgroundColor = UIColor.clear
let whiteRoundedView : UIView = UIView(frame: CGRectMake(10, 8, self.view.frame.size.width - 20, 149))
whiteRoundedView.layer.backgroundColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), [1.0, 1.0, 1.0, 0.8])
whiteRoundedView.layer.masksToBounds = false
whiteRoundedView.layer.cornerRadius = 2.0
whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1)
whiteRoundedView.layer.shadowOpacity = 0.2
cell.contentView.addSubview(whiteRoundedView)
cell.contentView.sendSubviewToBack(whiteRoundedView)
return cell
}
table row height: 165 point
header section height, footer section height: 10 point
and result
Edit For Swift 3 Syntax:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomApplicationCell
cell.contentView.backgroundColor = UIColor.clear
let whiteRoundedView : UIView = UIView(frame: CGRect(x: 10, y: 8, width: self.view.frame.size.width - 20, height: 120))
whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9])
whiteRoundedView.layer.masksToBounds = false
whiteRoundedView.layer.cornerRadius = 2.0
whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)
whiteRoundedView.layer.shadowOpacity = 0.2
cell.contentView.addSubview(whiteRoundedView)
cell.contentView.sendSubview(toBack: whiteRoundedView)
return cell
}
Simplest way for creating material card view:
Create CardView.swift
@IBDesignable
class CardView: UIView {
@IBInspectable var cornerRadius: CGFloat = 2
@IBInspectable var shadowOffsetWidth: Int = 0
@IBInspectable var shadowOffsetHeight: Int = 3
@IBInspectable var shadowColor: UIColor? = .black
@IBInspectable var shadowOpacity: Float = 0.5
override func layoutSubviews() {
layer.cornerRadius = cornerRadius
let shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
layer.masksToBounds = false
layer.shadowColor = shadowColor?.cgColor
layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight)
layer.shadowOpacity = shadowOpacity
layer.shadowPath = shadowPath.cgPath
}
}
Now just add CardView class to your UIView.
这篇关于UITableViewCells 之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!