uitableview - swift iOS 中的 TableView 问题

标签 uitableview swift uisearchbar xcode6.3

我正在尝试创建一个表格 View ,其中包含带有部分的菜肴列表,当我们选择一行时,它应该转到新的表格 View ,其中包含提供该特定食物的热门餐厅列表。

我已经创建了带有部分的 TableView ,但是当我到达 ListView 的末尾时我的应用程序崩溃了。我在下面添加了我的代码以及 TableView 和错误的快照。

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

这是表格 View 的屏幕截图。

Table view

这是当我向下滚动到表格 View 底部时的错误截图。

error while scrolling down

这是同样错误的控制台截图。

error shown in the console

还请告诉我如何为表格 View 添加搜索功能。

最佳答案

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

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

  return Dishes.count
}

您要返回每个部分的行数,但 Dishes 中没有超过键 B 的菜肴。 通常在 numberOfRowsInSection 委托(delegate)方法中,您会执行如下操作:

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
}

显然,您会想找到一种更好的方法来执行此操作,但我希望这对您有所帮助。

关于uitableview - swift iOS 中的 TableView 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30476340/

相关文章:

ios - 如何使 TableView 在重新加载后用新数据加载旧数据并在 ios 应用程序中 25 秒后隐藏旧数据

ios - 我无法根据与谓词的关系从核心数据中获取实体

ios - 如何在 Objective-C 类中分配 Swift 类中定义的静态对象?

ios - iOS 7.1 中 UISearchBar 的 UITextField

ios - 选择时 UISearchController 崩溃

ios - 整个应用程序都使用相同的 UISearchBar?

ios - 如何将 CollectionView 添加到 TableViewCell 并检测 CollectionView 何时完成加载单元格以请求 TableViewCell 高度更新

ios - 根据JSON删除核心数据中的对象

ios - 在 tableViewCell 中添加 tableView

ios - Swift 编写一个以 self 为输入的函数