【发布时间】:2016-02-16 02:52:54
【问题描述】:
过去几天我一直在学习 swift,但我遇到了一个我已经坚持了很长时间的错误。
我正在尝试获取选定的 indexPath,以便我可以根据他选择的项目推送数据。我已经搜索并尝试了许多在堆栈溢出以及不同网站上发现的不同解决方案,但我仍然无法弄清楚这一点。
代码如下:
@IBOutlet var selectGroceryTable: UITableView!
/* Get size of table */
func tableView(tableView: UITableView, numberOfRowsInSection: Int) ->Int
{
return grocery.count;
}
/* Fill the rows with data */
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let myCell:UITableViewCell = selectGroceryTable.dequeueReusableCellWithIdentifier("groceryListRow", forIndexPath:indexPath) as! UITableViewCell
myCell.textLabel?.text = grocery[indexPath.row];
myCell.imageView?.image = UIImage(named: groceryImage[indexPath.row]);
return myCell;
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
print("Row Selected");
NSLog("Row Selected");
}
没有任何东西像函数没有被调用一样打印出来。但是,我不明白为什么不调用它?
任何帮助将不胜感激!
更新:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
selectGroceryTable.data = self;
selectGroceryTable.delegate = self; //gives error states you can not do this
}
【问题讨论】:
-
你实现了 selectGroceryTable.delegate = self 吗?
-
尝试执行此操作时出现错误。请查看更新的代码
-
可能是因为您没有声明该类符合委托协议。我们都处于你的位置,如果没有一些帮助,很难学会这一点,当我刚开始时,Aaron Hilligas 的大书呆子牧场书籍对我真的很有帮助,祝你好运!
标签: ios iphone swift xcode6 ios9