【问题标题】:spacing between UITableViewCellsUITableViewCells 之间的间距
【发布时间】:2015-04-20 08:27:21
【问题描述】:

我正在快速创建一个 ios 应用程序,并希望在 Facebook 等单元格之间添加间距(如下图)。

我正在为帖子使用自定义笔尖。我知道使用 UITableViewController。我想我会使用分隔符样式,但它没有达到效果。我看了好几个小时,却找不到一个有意义的快速教程!有人可以解释他们是如何在应用程序中使用 swift 做到的吗?谢谢!

【问题讨论】:

  • 在单元格的内容视图中有一个包装视图。插入所有边缘,例如 10 像素。然后将您的内容放在该视图中。或者使用集合视图。
  • 简单的方法是让您的笔尖视图高度增加 10 像素...背景清晰

标签: ios facebook uitableview swift uitabbarcontroller


【解决方案1】:

这是我的结果解决方案:(基于 Jorge Casariego 的回答)

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
}

表格行高:165 磅

页眉部分高度,页脚部分高度:10 磅

结果

为 Swift 3 语法编辑:

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
}

创建素材卡片视图的最简单方法:

github.com/SwiftCardView

创建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
    }
}

现在只需将 CardView 类添加到您的 UIView。

【讨论】:

  • 当您要编辑行时,背景视图不会相应移动。
  • 我在 swift 3 中使用与您相同的代码,并同意将其更改为 swift 3 语法,但我无法接近您的单元格类型...
  • @return0 我已经添加了转换后的代码。相同的代码无需任何更改即可工作。
  • 应该是UIColor.clear 而不是transparentColor
  • 我想改变背景颜色?你能帮我吗
【解决方案2】:

Swift 4 代码

override var frame: CGRect {
        get {
            return super.frame
        }
        set (newFrame) {
            var frame =  newFrame
            frame.origin.y += 4
            frame.size.height -= 2 * 5
            super.frame = frame
        }
    }

【讨论】:

    【解决方案3】:

    有两种可能的解决方案:
    1) 垂直布局使用UICollectionView。这是一个非常灵活的解决方案,您可以写信给您own layout 进行收藏。 Some amazing results with custom layout
    2) 就像@sikhapol 评论的那样。但是你会遇到高亮单元格的问题。在我的项目中,我使用content 出口视图创建公共单元格,女巫包含所有内容视图。

    class ShadowContentCell: UITableViewCell {
        @IBOutlet var content: UIView!
        private var contentBg: UIImageView!
        private var contentOverlay: UIImageView!
    
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            self.frame = super.frame
        }
    
        override func awakeFromNib() {
            super.awakeFromNib()
    
            // custom higlight color
            selectedBackgroundView = UIView()
            selectedBackgroundView.backgroundColor = nil
            contentBg = UIImageView(frame: content.bounds)
            contentBg.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
            contentBg.image = UIImage(color: content.backgroundColor)
            content.addSubview(contentBg)
            content.sendSubviewToBack(contentBg)
    
            contentOverlay = UIImageView(frame: content.bounds)
            contentOverlay.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
            contentOverlay.highlightedImage = UIImage(color: UIColor(white: 0.000, alpha: 0.2))
            content.addSubview(contentOverlay)
        }
    
        override func setHighlighted(highlighted: Bool, animated: Bool) {
            contentOverlay.highlighted = highlighted
            if animated {
                let transition = CATransition()
                transition.duration = 0.3
                transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
                transition.type = kCATransitionFade
    
                contentBg.layer.addAnimation(transition, forKey: nil)
            }
        }
        override func setSelected(selected: Bool, animated: Bool) {
        }
    }
    

    【讨论】:

    • 视图控制器没有帧错误?!我该如何解决这个问题?
    【解决方案4】:

    我至少花了一个小时研究这个主题。最后,我想出了一个使用透明边框的想法:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "faqCell", for: indexPath)
    
        // ...
    
        cell.layer.borderWidth = CGFloat(TABLE_CELLSPACING)
        cell.layer.borderColor = tableView.backgroundColor?.cgColor
    
        return cell
    }
    

    这在 Swift 3/Xcode 8 中完美运行。

    Image: This is how it looks like on my side

    【讨论】:

    • 几个小时终于找到你了.. T-H-A-N-K Y-O-U
    • 这是迄今为止我发现的最方便的解决方案。然而有趣的是它不适用于清晰的颜色选项。有谁知道它的原因吗?
    • 这是个好主意,但它最终为我剪切了文本,所以我无法使用它。
    【解决方案5】:

    这里有很多正确的答案。

    我发现的最简单的方法是在 Interface Builder 的单元格 contentView 中创建一个容器视图。然后将所有单元格有意义的内容放入其中并约束到该视图。

    根据您的填充要求约束容器视图。

    类似:

    然后在您的 tableViewCellForRow 添加以下内容(假设您的容器视图出口称为 containerView)。

        currentCell.containerView?.layer.cornerRadius = 8 // Your choice here.
        currentCell.containerView?.clipsToBounds = true
    

    // 如果您愿意,可以添加边框、阴影等。

    看起来像

    【讨论】:

      【解决方案6】:

      Jorge Casariego Swift 代码的 Objective-C 版本:

      
      //play with the 'y' parameter of the whiteRoundedView to change the spacing 
      -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
      {
          cell.contentView.backgroundColor = [UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1.0];
          UIView  *whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(5, 2, self.view.frame.size.width-10, cell.contentView.frame.size.height)];
          CGFloat colors[]={1.0,1.0,1.0,1.0};//cell color white
          whiteRoundedView.layer.backgroundColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), colors);
          whiteRoundedView.layer.masksToBounds = false;
          whiteRoundedView.layer.cornerRadius = 5.0;
          whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1);
          whiteRoundedView.layer.shadowOpacity = 0.2;
          [cell.contentView addSubview:whiteRoundedView];
          [cell.contentView sendSubviewToBack:whiteRoundedView];
      }
      -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          //assign the right cell identifier
          UITableViewCell *cell = [[self tableView]dequeueReusableCellWithIdentifier:CellIdentifier];
          return cell.bounds.size.height;
      }
      

      【讨论】:

        【解决方案7】:

        使用 Swift 2,您可以通过这种方式在 UITableViewCells 之间设置间距:

        在你的 TableViewController 中复制这个:

            // In this case I returning 140.0. You can change this value depending of your cell
            override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
                return 140.0
            }
        
            override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
                cell.contentView.backgroundColor = UIColor.clearColor()
        
                let whiteRoundedView : UIView = UIView(frame: CGRectMake(0, 10, self.view.frame.size.width, 120))
        
                whiteRoundedView.layer.backgroundColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), [1.0, 1.0, 1.0, 1.0])
                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)
            }
        

        这是结果:

        【讨论】:

        • 每次显示单元格时都会调用 cell.contentView.addSubview(whiteRoundedView) 和 cell.contentView.sendSubviewToBack(whiteRoundedView) 。您不会以这种方式删除它们和垃圾邮件阴影和视图
        【解决方案8】:

        我创建了一个 UITableViewCell 的子类和一个 nib 文件来配合它。将 UIView 拖到单元格的 contentView 中,并首先使其与 contentView 大小相同。这将充当容器并成为“新”内容视图。你可以把你需要的东西放在那里,就像你通常在 UITableViewCell 的 contentView 中一样。

        现在您可以调整容器视图的高度,以便在单元格底部留下所需的间距。最后将contentView的背景色改为clearColor,效果就完成了。

        【讨论】:

          【解决方案9】:

          如果您想要正确/合法的间距,而不是技巧,您可以尝试 UICollectionView 而不是 UITableView。

          UICollectionView 比 UITableView 更通用,你几乎可以在上面自定义东西。

          【讨论】:

          • 同意^_^。我们也可以在一个单元格中放置一个 UIView 作为两个单元格之间的空间。
          • 虽然使用集合视图是解决此问题的好方法,但请记住,您没有本机的滑动删除选项。如果需要,您可以修改表格视图中单元格之间的间距,或者修改集合视图中的滑动以删除。 :)
          • @Boris 我不认为在 CollectionView 中滑动删除是一种 hack,它是一个功能。否则TableView没有“滑动删除”,只能打开编辑模式,“滑动显示按钮”然后点击删除。
          【解决方案10】:

          这些方法将帮助您实现间距:-

          - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
          {
          return 1;
          }
          - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
          return 15;
          }
          
          - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
          UIView *invisibleView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds .size.width, 15)];
          [invisibleView setBackgroundColor:[UIColor clearColor]];
          return invisibleView;
          
          }
          

          【讨论】:

          • numberOfRowsInSection = 1 ????????????那为什么我需要一个空间?
          猜你喜欢
          • 1970-01-01
          • 2016-04-02
          • 2022-01-27
          • 1970-01-01
          • 2014-12-26
          • 2013-10-05
          • 2021-04-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多