【问题标题】:Change tableView separator insets更改 tableView 分隔符插图
【发布时间】:2015-11-17 15:54:46
【问题描述】:

我在自定义单元格时遇到了一点问题;

如您所见,分隔线没有到达单元格的左边框,我想这样做。我找到了这些:

谁能帮我翻译一下swift代码?

【问题讨论】:

    标签: ios uitableview delegates cell


    【解决方案1】:

    在您的 cellForRowAtIndePath 方法中添加这些行:

        if cell.respondsToSelector("setSeparatorInset:") {
            cell.separatorInset = UIEdgeInsetsZero
        }
        if cell.respondsToSelector("setLayoutMargins:") {
            cell.layoutMargins = UIEdgeInsetsZero
        }
        if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
            cell.preservesSuperviewLayoutMargins = false
        }
    

    【讨论】:

      【解决方案2】:

      斯威夫特 3:

      func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
          if (cell.responds(to: #selector(setter: UITableViewCell.separatorInset))) {
              cell.separatorInset = UIEdgeInsets.zero
          }
      
          if (cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins))) {
              cell.preservesSuperviewLayoutMargins = false
          }
      
          if (cell.responds(to: #selector(setter: UIView.layoutMargins))) {
              cell.layoutMargins = UIEdgeInsets.zero
          }
      }
      

      【讨论】:

        【解决方案3】:

        这是旧版本,它不再工作了。

        cell.layoutMargins = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false
        

        你可以使用它(对于 swift,在 Objective C 中你可以调用同样的东西)

        cell.separatorInset = UIEdgeInsets.zero
        

        【讨论】:

          【解决方案4】:

          用于在 Swift 中转换答案 (Separator lines for UITableViewCellStyleSubtitle cells not taking the full width)

          func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
              var cell = tableView.dequeueReusableCellWithIdentifier("yourcell") as! UITableViewCell
          
              if (cell.respondsToSelector("setPreservesSuperviewLayoutMargins:")){
                  cell.layoutMargins = UIEdgeInsetsZero
                  cell.preservesSuperviewLayoutMargins = false
              }
          }
          

          通过检查版本号:

          let floatVersion = (UIDevice.currentDevice().systemVersion as NSString).floatValue
          
          if (floatVersion > 8.0){
              cell.layoutMargins = UIEdgeInsetsZero
              cell.preservesSuperviewLayoutMargins = false
          }
          

          【讨论】:

          • @FabioCelli 您是否将分隔符自定义插入添加到 0?
          • 我已经做了,但是没用;现在关闭和重新打开 Xcode 工作(???)。
          • @FabioCelli 很好。您只需将我的 obj-c 答案转换为 swift 即可,它应该可以正常工作:) 无论如何很高兴听到它正在工作。
          • @iAshish,仅供参考,Apple 不希望我们再使用 respondToSelector,您可以在 WWDC 的 stateoftheunion 视频中找到有关它的信息
          • @Loegic 好的,我可以通过版本号检查,因为 iOS 7 没有preservesSuperviewLayoutMargins。更新了答案。
          【解决方案5】:

          第二个链接应该可以工作。

          快速版本:

          if cell.respondsToSelector(Selector("setSeparatorInset:")) {
              cell.separatorInset = UIEdgeInsetsZero
          }
          
          if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
              cell.preservesSuperviewLayoutMargins = false
          }
          
          if cell.respondsToSelector(Selector("setLayoutMargins:")) {
              cell.layoutMargins = UIEdgeInsetsZero
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-02-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-04-09
            • 2011-11-10
            相关资源
            最近更新 更多