快速iOS中的表格视图问题 [英] Table view issues in swift iOS

查看:101
本文介绍了快速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.

 @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.

推荐答案

我认为您的问题出在这里:

I think your issue lies here:

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

  return Dishes.count
}

您将返回每个部分的行数,但是Dishes中的键B上方没有菜. 通常,在numberOfRowsInSection委托方法中,您将执行以下操作:

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.

这篇关于快速iOS中的表格视图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆