두 개의 ViewController가 있습니다. 하나는 TableView와 Add Button을 가지고 있는데 두 번째 ViewController는 텍스트 필드와 액션 버튼을 가지고 있습니다. 사용자는 텍스트 필드가있는 두 번째 화면에서 tableview에 텍스트를 삽입해야합니다.한 뷰 컨트롤러에서 다른 뷰 컨트롤러로 전환하는 동안 오류가 발생했습니다.
두 개의보기 컨트롤러에 대해 별도의 신속한 파일을 만들었습니다. 두 번째보기의 경우 첫 번째보기 수업을 가져오고 있습니다. 그러나 추가 단추를 눌러 두 번째보기 컨트롤러로 이동하려고하면 선택적 랩핑 오류가 발생합니다. 다음과 같은 오류가 발생합니다. 치명적인 오류 : unexpectedly found nil while unwrapping an Optional value. 내 첫 번째보기 컨트롤러
tableview.setEditing(true, animated: true)
코드의 tableview 있습니다 : 내 두 번째 뷰 컨트롤러에 대한
class MainScreen : UIViewController, UITableViewDataSource, UITableViewDelegate {
var items = ["Apple", "Fish", "Dates", "Cereal", "Ice cream", "Lamb", "Potatoes", "Chicken", "Bread"]
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableview.setEditing(true, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let iD = "normal"
let cell = tableView.dequeueReusableCellWithIdentifier(iD, forIndexPath: indexPath)
cell.textLabel?.text = items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.None
}
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
if (sourceIndexPath.row != destinationIndexPath.row){
let temp = items[sourceIndexPath.row]
items.removeAtIndex(sourceIndexPath.row)
items.insert(temp, atIndex: destinationIndexPath.row)
}
tableView.reloadData()
}
을}
코드 그리고 디버그 모드에서, 그것은 다음 코드 줄을 강조 텍스트 필드와 내 배열 (항목)에 텍스트를 삽입하는 버튼이 있습니다.
class TextScreen : MainScreen {
@IBOutlet weak var keyboard: UITextField!
@IBAction func insert(sender: AnyObject) {
items.append(keyboard.text!)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
내가 뭘 잘못하고 있는지 모르겠다. 나는 또한 스크린 샷을 첨부했다.
UITableViewDataSource 및 UITableViewDelegate의 필수 기능을 구현해야합니다. – Roee84
@ Roee84 My TableView가 이미 DataSource 및 Delegate에 연결되어 있습니다. 나는 스토리 보드에서 그렇게했다. 이게 무슨 뜻이야? 좀 더 설명해 주시겠습니까? 또한 셀과 모든 것을 설정했지만 코드는 여기에 표시되지 않습니다 –
'tableview' 출력이'nil' 인 것처럼 보입니다. 스토리 보드에 연결 했습니까? – vacawama