2016-06-12 41 views
0

无法投型 'Google_Books_1.BookTableViewCell' 的值(0x105557600)为 'NSIndexPath'(0x1061cb438)无法投类型TableViewCell的价值 'NSIndexPath'

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("BookCell", forIndexPath: indexPath) as!BookTableViewCell 
    ... 
} 


func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
     // let contact = ContactList![indexPath.row] 
     performSegueWithIdentifier("BookDetailSegue", sender: indexPath) 
    } 
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
     // let contact = ContactList![indexPath.row] 
     performSegueWithIdentifier("BookDetailSegue", sender: indexPath) 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "BookDetailSegue" { 
      let vc = segue.destinationViewController as! BookDetailViewController 

      let indexPath = sender as! NSIndexPath 
      vc.book = self.bookList[indexPath.row] **//error is here** 
      vc.index = indexPath.row 
     } 
    } 

如何处理这样的错误?

+0

你确实知道你正在使用2个didDeselectRowAtIndexPath函数吗?你不是故意使用didSelectRowAtIndexPath吗? – Devster101

回答

0

声明变量的实例

var indexPath: NSIndexPath?

然后将所选indexPath像波纹管:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
      // let contact = ContactList![indexPath.row] 
      self.indexPath = indexPath 
      performSegueWithIdentifier("BookDetailSegue", sender: indexPath) 
     } 

现在访问indexPath这样的:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
      if segue.identifier == "BookDetailSegue" { 
       let vc = segue.destinationViewController as! BookDetailViewController 

       vc.book = self.bookList[self.indexPath.row!] **//error is here** 
       vc.index = indexPath.row 
      } 
     } 

希望这有助于你。

+0

它不起作用。我猜它不进入功能tableView(tableView:UITableView,didDeselectRowAtIndexPath indexPath:NSIndexPath)方法 – Beibut

+0

嗨,检查我编辑的答案,并删除方法didDeSelect,并确保prepareForSegue方法被调用。 – iMuzahid

0

为什么不把自己放在你的tableView的indexPathForSelectedRow属性上呢?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "BookDetailSegue" { 
     let vc = segue.destinationViewController as! BookDetailViewController 
     if let indexPath = tableView.indexPathForSelectedRow { 
      vc.book = self.bookList[indexPath.row] **//error is here** 
      vc.index = indexPath.row 
     } 
    } 
} 
+0

它的工作原理。非常感谢你。 – Beibut

相关问题