我正在创建一个应用程序,其中我有一个用户添加数据的UITableView,我希望用户能够点击单元格并查看有关他们选择的单元格的更多详细信息。我从cell到我的cellDetailController设置了一个segue,稍后我将添加他们选择的单元格的所有细节。当我运行应用程序并尝试点击要离开的单元格时,它只选择单元格并且没有任何反应。我在这做错了什么?这是我的故事板设置:
(我无法上传图片,所以这里是一个链接) http://tinypic.com/r/2ivcwsj/8
这是我连接到tableView的类的代码:
import UIKit
typealias eventTuple = (name: String, date: String)
var eventList : [eventTuple] = []
class TableViewController: UIViewController, UITableViewDelegate {
@IBOutlet weak var eventTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
eventTable.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return eventList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
cell.textLabel?.text = eventList[indexPath.row].name
cell.detailTextLabel?.text = eventList[indexPath.row].date
return cell
}
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
*/
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "showDetailSegue") {
}
}
}
答案 0 :(得分:7)
从tableViewController(viewController顶部的黄色图标)拖动segue,而不是从tableCell拖动。然后给该segue一个标识符。
覆盖didSelectRowAtIndexPath
并通过performSegueWithIdentifier()
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// pass any object as parameter, i.e. the tapped row
performSegueWithIdentifier("showDetailSegue", sender: indexPath.row)
}
答案 1 :(得分:0)
没有必要覆盖didSelectRowAtIndexPath
来实现此功能。请参阅Apple的Working with Table Views。我遇到了同样的问题,但这是由于TableViewController中的错误。罪魁祸首是tableView
方法之一:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell`
如果您使用的是自定义UITableViewCell
类,则tableView()
应返回自定义类的实例。在我的情况下,我忘了在此方法中向下转换dequeueReusableCell()
返回的值。对dequeueReusableCell()
的调用应该如下所示:
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? CustomTableViewCell else {
fatalError("The dequeued cell is not an instance of CustomTableViewCell.")
}