tableView的顶部有一些空白区域

时间:2016-10-11 07:10:23

标签: ios swift uitableview uinavigationcontroller

问题出现在pic上。 at the top of tableView has some empty area 接口是viewController(嵌入在navigationController中)。我在其上放置一个tableView。并构造其约束。但是运行代码,问题就出现了。我想尝试使用两种方法来解决它,但没有任何改变。< / p>

   self.automaticallyAdjustsScrollViewInsets = true

我将tableView的约束调整为superView,我将top的约束设置为-100,效果很好。 代码如下:

import UIKit
import CoreData


class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
var people:[NSManagedObject] = []

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.title = "My book"
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")

}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
        return
    }
    let managedContext = appDelegate.persistentContainer.viewContext
    let fetchRequest = NSFetchRequest<NSManagedObject>(entityName:"Person")
    do {
        people = try managedContext.fetch(fetchRequest)
    }catch let error as NSError {
        print("Cannot fetch \(error),\(error.userInfo)")

    }
}
@IBAction func addName(_ sender: AnyObject) {
    let alert = UIAlertController(title: "New name", message: "Add a name", preferredStyle: .alert)
    let saveAction = UIAlertAction(title: "Save", style: .default) { [unowned self] action in
        guard let textField = alert.textFields?.first,
            let nameToSave  = textField.text else {return
    }
        self.save(name: nameToSave)
        self.tableView.reloadData()

}
    let cancelAction = UIAlertAction(title: "Cancel", style: .default)

    alert.addTextField()
    alert.addAction(saveAction)
    alert.addAction(cancelAction)
    present(alert,animated: true)

}
    func save(name:String) {
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
            return
        }
        let managedContext = appDelegate.persistentContainer.viewContext
        let entity = NSEntityDescription.entity(forEntityName: "Person", in: managedContext)
        let person = NSManagedObject(entity: entity!, insertInto: managedContext)
        person.setValue(name, forKey: "name")
        do {
            try managedContext.save()
            people.append(person)
        }catch let error as NSError {
            print("Could not save\(error),\(error.userInfo)")
        }
    }

}



extension ViewController :UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return people.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let person = people[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.text = person.value(forKey: "name") as? String
    return cell
}

}

3 个答案:

答案 0 :(得分:0)

您还可以尝试在edgesForExtendedLayout方法中设置viewDidLoad

self.edgesForExtendedLayout = []

答案 1 :(得分:0)

您应该从故事板中为tableView添加约束并设置top space = 0

答案 2 :(得分:0)

尝试实施

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return .min 
}