2016-10-11 11 views
0

この問題は画像として発生します。 at the top of tableView has some empty area インタフェースはviewController(navigationControllerに埋め込まれています)です.1つのtableViewをit.Andに配置し、constraint.Butを実行するとコードが実行され、問題が発生します。解決するために2つのメソッドを使用したい変更。tableViewの上部に空き領域があります

私はsuperViewにtableViewの制約を調整します。私は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 
} 

} 

答えて

0

あなたはまた、あなたのviewDidLoad方法でedgesForExtendedLayoutを設定してみてくださいすることができます

self.edgesForExtendedLayout = [] 
+0

それはこの問題を解決することはできません。逆に新しい問題を引き起こします.LineはindexPathの行の間で消えて、ちらついて行をクリックします – MarcSteven

0

あなたがしようとtop space = 0

+0

ええ、私は制約がsuperView.topにゼロであることを知っています。 – MarcSteven

+0

plsのビデオはこちら[link](https://www.dropbox.com/s/9x0953g6fvjrnwa/issue%20video.mov?dl=0) – MarcSteven

0

をストーリーボードからtableViewの制約を入れて設定する必要があります実装する

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

何も変わりません..... – MarcSteven

+0

私はこの問題がトップです私はそれを調整し、プロジェクトを実行する、それは大丈夫です。 – MarcSteven

関連する問題