我正在尝试创建一个表格 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 的屏幕截图。
这是当我向下滚动到表格 View 底部时的错误截图。
这是同样错误的控制台截图。
还请告诉我如何为表格 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/