tableView的顶部有一些空白区域
问题出现在图片中。 at the top of tableView has some empty area 接口是那个viewController(嵌入在navigationController中)。我把一个tableView放在它上面,然后构造它的约束。但是运行代码,发生问题。我想尝试用两种方法解决它,但是没有任何变化。tableView的顶部有一些空白区域
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
}
}
你也可以尝试设置edgesForExtendedLayout
在viewDidLoad
方法:
self.edgesForExtendedLayout = []
你应该把约束为tableView
从情节串连图板,并设置top space = 0
是的我知道限制为零superView.top。 – MarcSteven
请看这里的视频[链接](https://www.dropbox.com/s/9x0953g6fvjrnwa/issue%20video.mov?dl=0) – MarcSteven
尝试实施
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return .min
}
没有什么变化..... – MarcSteven
我发现问题是顶部tableView的约束,我调整它,然后运行项目,没关系。 – MarcSteven
它不能解决这个问题。相反,它会导致一个新的问题。行消失之间indexPath的行,闪烁点击行 – MarcSteven