Xcodian Solangi
Xcodian Solangi

Reputation: 2408

Drop down list menu programmatically in Swift Xcode

I have made a drop down gender which is working fine however I have another drop down on same viewcontroller which shows drop down but when I select the item it gives error fatal error: unexpectedly found nil while unwrapping an Optional value I have tried out but confused. help me to solve the bloodList dropdown.

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)")
}
}

Upvotes: 1

Views: 13350

Answers (3)

Xcodian Solangi
Xcodian Solangi

Reputation: 2408

I waited for an answer but did not find :( that's ok . I thought a number of times and then come to this code yahoooo!!! then i believed i am really a programmer. Actually i did not defined the condition for each table view so i was in nightmare. This code is pure programatically and does not require any irritating third party library or awkward pods

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)")
    }  
}

Upvotes: 0

Sneha
Sneha

Reputation: 2216

You cannot change default delegate methods of your tableView, give conditions in the delegate methods like below :

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

Also make similar changes in the remaining delegate methods.

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)")
   }
}

Upvotes: 2

karthikeyan
karthikeyan

Reputation: 3888

Don't create like this

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

They are all in build delegate methods of UITableview, you should override it, you cannot change it.

Instead of creating method, you try with Bool variable in respective button action.

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

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

}

Like change the condition

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

Upvotes: 1

Related Questions