Table view issues in swift iOS

孤人 提交于 2019-12-09 07:14:15

问题


I am trying to create a table view in which it has list of dishes with sections and when we select a row it should go to new table view which consist list of popular restaurants which serves that specific food.

I have created the table view with sections, but my app crashes when I reach at end of the list view. I have added my code below with the snapshot of table view and error.

 @IBOutlet weak var dishtable: UITableView!

@IBOutlet weak var namlbl: UILabel!
var Dishes = ["POPULAR Dishes": ["Biryani", "Tandori Chicken","Butter Chicken", "Vada Pav"],"A": ["Aloo baingan", "Aloo ki Tikki", "Amritsari fish"], "B": ["Baigan bharta", "Biryani"]];


override func viewDidLoad() {
    super.viewDidLoad()
    self.dishtable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    dishtable.dataSource = self
     dishtable.delegate = self
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func prefersStatusBarHidden() -> Bool {
    return true
}

let sections:Array<AnyObject> = ["POPULAR Dishes","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var usernames = [String]()

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let cellID = "cell"

    let cell: UITableViewCell = self.dishtable.dequeueReusableCellWithIdentifier(cellID) as! UITableViewCell
    println("value : \(indexPath.section)")
    println("value 1: \(indexPath.row)")

    cell.textLabel!.text = Dishes[sections[indexPath.section] as! String]![indexPath.row]

    return cell

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    println("Dishes section count : \(section)")

    return Dishes.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{

    return 27
}

func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

}


func tableView(tableView: UITableView,
    sectionForSectionIndexTitle title: String,
    atIndex index: Int) -> Int{

        return index
}

func tableView(tableView: UITableView,
    titleForHeaderInSection section: Int) -> String?{

        return self.sections[section] as? String
}

This is screenshot for the table view.

This is the screenshot of the error when I scroll down to bottom of the table view.

This is the screenshot of the console for the same error.

Please also let me know how can we add the search feature for the table view.


回答1:


I think your issue lies here:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
  println("Dishes section count : \(section)")

  return Dishes.count
}

You are returning a count of rows for each section, yet there are no dishes past key B in Dishes. Normally in a numberOfRowsInSection delegate method you would do something like this:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
  println("Dishes section count : \(section)")

  if section == 0 {
    return Dishes["POPULAR Dishes"].count
  }
  else if section == 1 {
    return Dishes["A"].count
  }
  return 0
}

Obviously you'll want to find a nicer way to do this, but I hope this helps.




回答2:


The problem is in numberOfRowsInSection function.

As in your case numberOfSectionsInTableView = 27 so u need to return individual numberOfRowsInSection.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0:
        return Dishes["POPULAR Dishes"].count
    case 1:
        return Dishes["A"].count
    case 2:
        return Dishes["B"].count

    // upto case 26      
    default:
        println("fetal error")
    }
    return 1
}


来源:https://stackoverflow.com/questions/30476340/table-view-issues-in-swift-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!