Table view issues in swift iOS

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.

  • Application sticks on OSSpinLockLockSlow
  • Xcode 6.3 code completion too slow
  • Interface Builder appears blurry on retina display in Xcode 6.3
  • XCode 6.3 adding margins to tableviewcell
  • Xcode 6.3 is crashing on Project Rename
  • How to remove 100s of warnings “implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int'” I got after updating to arm64?
  •  @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.

    Table view

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

    error while scrolling down

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

    error shown in the console

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

    2 Solutions Collect From Internet About “Table view issues in swift iOS”

    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.

    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
    }