2017-04-13 12 views
1

我做了一個下拉gender這是工作正常,但我有另一個下拉相同viewcontroller它顯示下拉,但是當我選擇它給出錯誤的項目fatal error: unexpectedly found nil while unwrapping an Optional value我已經嘗試過但困惑。幫我解決bloodList下拉。下拉列表菜單以編程方式在斯威夫特Xcode

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var genderButton: UIButton! 
@IBAction func genderButton(_ sender: Any) { 
    self.PressDrop() 
    view.addSubview(genderTable) 
} 
@IBOutlet weak var genderTable: UITableView! 

var flag = 1 
var dropDownList = [String]() 
@IBOutlet weak var bloodButton: UIButton! 
var bloodList = [String]() 
@IBAction func bloodButton(_ sender: Any) { 
    self.PressBlood() 
    view.addSubview(bloodTable) 
} 
@IBOutlet weak var bloodTable: UITableView! 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    dropDownList = ["Male", "Female", "Other"] 
    genderTable.delegate = self 
    genderTable.dataSource = self 
    genderTable.isHidden = true 
    view.addSubview(genderTable) 
    genderTable.layer.cornerRadius = 10 

    bloodList = ["A+", "A-", "AB+", "AB-"] 
    bloodTable.delegate = self 
    bloodTable.dataSource = self 
    bloodTable.isHidden = true 
    view.addSubview(bloodTable) 
    bloodTable.layer.cornerRadius = 10 
} 

func PressDrop() { 
    if flag == 0 { 
     UIView.setAnimationDuration(0.5) 
     self.genderTable.isHidden = true 
     self.flag = 1 
    } 
    else{ 
     UIView.setAnimationDuration(0.5) 
     self.genderTable.isHidden = false 
     self.flag = 0 
    } 
} 

func PressBlood() { 
    if flag == 0 { 
     UIView.setAnimationDuration(0.5) 
     self.bloodTable.isHidden = true 
     self.flag = 1 
    } 
    else{ 
     UIView.setAnimationDuration(0.5) 
     self.bloodTable.isHidden = false 
     self.flag = 0 
    } 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

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

} 

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

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 50 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = genderTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
    cell.textLabel!.text = dropDownList[indexPath.row] 
    return cell 
} 

func tableViewTwo(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = bloodTable.dequeueReusableCell(withIdentifier: "bloodCell", for: indexPath) 
    cell.textLabel!.text = bloodList[indexPath.row] 
    return cell 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    let selectedData = dropDownList[indexPath.row] 
    genderButton.setTitle(selectedData, for: .normal) 
    self.genderTable.isHidden = true 
    self.flag = 
    let indexPath = genderTable.indexPathForSelectedRow 
    let currentCell = genderTable.cellForRow(at: indexPath!)! as UITableViewCell 
    let finalresult = currentCell.textLabel!.text! 
    print("\(finalresult)") 
} 

private func tableViewTwo(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let selectedBlood = bloodList[indexPath.row] 
    bloodButton.setTitle(selectedBlood, for: .normal) 
    self.bloodTable.isHidden = true 
    self.flag = 1 
    let indexPathTwo = bloodTable.indexPathForSelectedRow 
    let currentCellBlood = bloodTable.cellForRow(at: indexPathTwo!)! as UITableViewCell 
    let finalresultBlood = currentCellBlood.textLabel!.text! 
    print("\(finalresultBlood)") 
} 
} 
+0

設置斷點,並嘗試找出其中可空性錯誤是。只是反芻你所有的代碼是沒有用的。 – Stonz2

回答

0

我在等待一個答案,但沒找到:(沒關係,我想了很多次,然後來到這個代碼yahoooo!然後我相信我真的是程序員。其實我沒有定義的條件爲每個表視圖,以便我在噩夢。這個代碼是純粹的編程和不需要任何刺激性第三黨庫或尷尬

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if tableView == tableViewB { 
    return dropDownList.count 
    } 
    else { 
    return genderL.count 
    } 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if tableView == tableViewB { 
    let cell = tableViewB.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
    cell.textLabel!.text = dropDownList[indexPath.row] 
    return cell  
    } 
    else { 
     let cell = genderT.dequeueReusableCell(withIdentifier: "gender", for: indexPath) 
     cell.textLabel!.text = genderL[indexPath.row] 
     return cell 
    } 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if tableView == tableViewB { 
    let selectedData = dropDownList[indexPath.row] 
    buttonB.setTitle(selectedData, for: .normal) 
    self.tableViewB.isHidden = true 
    self.flag = 1 
    let indexPath = tableViewB.indexPathForSelectedRow 
    let currentCell = tableViewB.cellForRow(at: indexPath!)! as UITableViewCell 
    let finalresult = currentCell.textLabel!.text! 
    print("\(finalresult)") 
    } 
    else { 
     let selectedDataG = genderL[indexPath.row] 
     genderB.setTitle(selectedDataG, for: .normal) 
     self.genderT.isHidden = true 
     self.flag = 1 
     let indexPath = genderT.indexPathForSelectedRow 
     let currentCell = genderT.cellForRow(at: indexPath!)! as UITableViewCell 
     let finalresult = currentCell.textLabel!.text! 
     print("\(finalresult)") 
    } 
} 
1

不要創建這樣

func tableViewTwo(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell (This will consider as new method to your current class) 

他們都在構建委託方法的UITableView,你應該重寫它,你不能改變它。

而不是創建方法,你嘗試與布爾變量在各自的按鈕操作。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

If isDrop == true{ 
    return dropDownList.count 
}else{ 
return bloodList.count 
} 

} 

樣改變的條件

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
+0

使用未解析的標識符'isDrop' –

2

你不能改變你的tableView的默認委託方法,在委託方法像下面給出的條件:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if tableView == genderTable { 
      return dropDownList.count 
     } 
     return bloodList.count 
    } 

另外,還要在類似的變化剩餘的委託方法。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if tableView == genderTable { 
     let cell = genderTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
     cell.textLabel!.text = dropDownList[indexPath.row] 
     return cell 
} else { 
     let cell = bloodTable.dequeueReusableCell(withIdentifier: "bloodCell", for: indexPath) 
     cell.textLabel!.text = bloodList[indexPath.row] 
     return cell 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     if tableView == genderTable { 
     let selectedData = dropDownList[indexPath.row] 
     genderButton.setTitle(selectedData, for: .normal) 
     self.genderTable.isHidden = true 
     let currentCell = genderTable.cellForRow(at: indexPath)! as UITableViewCell 
     let finalresult = currentCell.textLabel!.text! 
     print("\(finalresult)") 
    } else { 
     let selectedBlood = bloodList[indexPath.row] 
     bloodButton.setTitle(selectedBlood, for: .normal) 
     self.bloodTable.isHidden = true 
     let currentCellBlood = bloodTable.cellForRow(at: indexPath)! as UITableViewCell 
     let finalresultBlood = currentCellBlood.textLabel!.text! 
     print("\(finalresultBlood)") 
    } 
} 
+0

'let indexPath = genderTable.indexPathForSelectedRow'給出錯誤'expected expression' –

+1

當您選擇單元格時,您想要什麼? – Sneha

+0

我想要在控制檯上打印的值 –