【问题标题】:Separator appears in uitableview while disabled禁用时分隔符出现在uitableview中
【发布时间】:2015-10-31 00:34:40
【问题描述】:

我正在使用 swift 创建一个 iOS 应用程序。我想构建一个不可滚动的 tableView,它在屏幕上显示数据源中包含的所有信息,因此每个单元格的高度取决于数据中的条目数。例如,如果视图的高度为 500 且 data.count = 10,则每个单元格的高度为 50。当单元格的高度为 ~100.8 时出现问题(对应于我的数据中的 5 个条目,使用我的 iPhone 5)。事实上,即使设置了 tableView.separatorStyle = .None ,这个单元格的高度也会出现一个奇怪的分隔符。

在下面,第一个图像(数据中的 7 个条目)是正常的,而在第二个图像(数据中的 5 个条目)上出现了这些分隔符。

这是我的视图控制器:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    let reuseIdentifier = "Cell"
    var data = ["bobby", "bob", "john", "helena", "clara", "oliver", "steve"]
    var visibleHeight:CGFloat!

    override func viewDidLoad() {
        super.viewDidLoad()
        edgesForExtendedLayout = .None
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
        editModeOff()
        tableView.scrollEnabled = false
        visibleHeight = viewVisibleSize.height
        tableView.separatorStyle = .None
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return data.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath)
        cell.textLabel!.text = data[indexPath.row]
        cell.selectionStyle = .None
        return cell
    }

    func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
        let stringToMove = data[sourceIndexPath.row]
        data.removeAtIndex(sourceIndexPath.row)
        data.insert(stringToMove, atIndex: destinationIndexPath.row)
    }

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete{
            data.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
        }
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        print(visibleHeight/CGFloat(data.count))
        return visibleHeight/CGFloat(data.count)
    }

    func editModeOn(){
        tableView.setEditing(true, animated: true)
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "editModeOff")
    }

    func editModeOff(){
        tableView.setEditing(false, animated: true)
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: "editModeOn")
    }

}

extension ViewController{

    var viewVisibleSize:CGSize{
        var size = view.bounds.size

        if !UIApplication.sharedApplication().statusBarHidden{
            size.height -= UIApplication.sharedApplication().statusBarFrame.height
        }

        if let navigationController = navigationController{
            size.height -= navigationController.navigationBar.bounds.height
        }

        if let tabBarController = tabBarController{
            size.height -= tabBarController.tabBar.bounds.height
        }

        return size
    }


}

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    我总是清除分隔符的颜色:

     tableView.separatorColor = UIColor.clearColor()
    

    【讨论】:

    • 我已经试过了。没用...我尝试将所有可能的颜色(背景、分隔符、单元格...)设置为白色/清晰,但它没有改变任何东西。
    【解决方案2】:

    我通过设置找到了一个肮脏的解决方案:

    cell.contentView.layer.borderWidth = 0.5
    cell.contentView.layer.borderColor = UIColor.whiteColor().CGColor
    

    tableView:cellForRowAtIndexPath: 方法中。但可以肯定的是,有更好的方法......

    【讨论】:

      【解决方案3】:

      在你的viewDidLoad: 你可以试试self.tableView.tableFooterView = UIView()

      【讨论】:

        【解决方案4】:

        这很有趣。您的函数中的问题
        tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
        它对某些浮点数有奇怪的行为,并且会出现一个分隔符。
        我为您提供这样的返回高度:

            func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
                print(self.visibleHeight/CGFloat(self.tableArray.count))
                let rett = self.visibleHeight/CGFloat(self.tableArray.count)
                let convert = NSString(format: "%.0f", rett)
                return CGFloat(convert.floatValue)
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多