2015-07-05 38 views
0

尝试滚动后出现错误: 无法将'Reviews.topCell'(0xaeb68)类型的值转换为'Reviews.contentCell'(0xaea98)。 (请参阅最后一个方法) 我认为我引用了正确的单元格,但问题可能在于 cellForRowAtIndexPath方法,因为在尝试滚动后执行挂起了它。无法投射swift类型的值

完整的源代码:http://pastebin.com/3BLUs5JY

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let nodeCount = arrayOfPosts.count; 
    // if (indexPath.row < 0) 
    // { 
      // let cell:topCell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell 
      // cell.userName.text = "Loading..." 
     // } 
     // else 
     // { 
      // Leave cells empty if there's no data yet 
     // if (nodeCount > 0) 
      // { 
       if (indexPath.row % 2 == 0){ 
        // Set up the cell representing the app 
        let cell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell 
        let post = arrayOfPosts[indexPath.row] 
        cell.userName.text = post.userName 
        cell.timeSincePosted.text = post.timeSincePosted 
        // Only load cached images; defer new downloads until scrolling ends 
        if (post.profileImage == nil) 
        { 
         if (!tableView.dragging && !tableView.decelerating) 
         { 
          downloadProfileImage(post, indexPath: indexPath) 
          cell.profileImage.image = post.profileImage 
          return cell 
         } 
         // if a download is deferred or in progress, return a placeholder image 
         cell.profileImage.image = UIImage(named: "titanic.jpg") 
              } 
        else 
        { 
          cell.profileImage.image = post.profileImage 
        } 
        return cell 
       } 
       // Set up the cell representing the app 
       let cell = tableView.dequeueReusableCellWithIdentifier("contentCell", forIndexPath: indexPath) as! contentCell 
       let post = arrayOfPosts[indexPath.row] 
       // Only load cached images; defer new downloads until scrolling ends 
       if (post.posterImage == nil) 
       { 
        if (!tableView.dragging && !tableView.decelerating) 
        { 


         downloadPosterImage(post, indexPath: indexPath) 
         cell.posterImage.image = post.posterImage 
         return cell 
         /* 
         let url = NSURL(string: "http://www.freemovieposters.net/posters/titanic_1997_6121_poster.jpg") 
         let data = NSData(contentsOfURL: NSURL(string: "http://www.impawards.com/1997/posters/titanic_ver7.jpg")!) //make sure your image in this url does exist, otherwise unwrap in a if let check 
         cell.posterImage.image = UIImage(data: data!) 
         return cell 
         */ 
        } 
        // if a download is deferred or in progress, return a placeholder image 


        cell.posterImage.image = UIImage(named: "img1.jpg") 
       } 
       else 
       { 
        cell.posterImage.image = post.posterImage 
      } 
     return cell 
     } 
Could not cast value of type 'Reviews.topCell' (0xaeb68) to 'Reviews.contentCell' (0xaea98).  


    func loadImagesForOnscreenRows(){ 
       if (arrayOfPosts.count > 0){ 
      let visiblePaths:NSArray = tableView.indexPathsForVisibleRows()! 
       for indexPath in visiblePaths { 
      let post = arrayOfPosts[indexPath.row] 

        if (indexPath.row % 2 == 0){ 

         let cell:topCell = self.tableView.cellForRowAtIndexPath(indexPath as! NSIndexPath) as! topCell 
         if (cell.profileImage != post.profileImage){ 
          downloadProfileImage(post, indexPath: indexPath as! NSIndexPath) 
          cell.profileImage.image = post.profileImage 
         } 
        } 
        let cell: contentCell = tableView.cellForRowAtIndexPath(indexPath as! NSIndexPath) as! contentCell 
        if (cell.posterImage != post.posterImage){ 
        downloadPosterImage(post, indexPath: indexPath as! NSIndexPath) 
        cell.posterImage.image = post.posterImage 
         } 
        } 
       } 
      } 

回答

1

loadImagesForOnscreenRows()方法有可能是一个else失踪

if (indexPath.row % 2 == 0){ 
    let cell:topCell = self.tableView.cellForRowAtIndexPath(indexPath as! NSIndexPath) as! topCell 
    if (cell.profileImage != post.profileImage) { 
     downloadProfileImage(post, indexPath: indexPath as! NSIndexPath) 
     cell.profileImage.image = post.profileImage 
    } 
} else { // <-- 
    let cell: contentCell = tableView.cellForRowAtIndexPath(indexPath as! NSIndexPath) as! contentCell 
    if (cell.posterImage != post.posterImage){ 
     downloadPosterImage(post, indexPath: indexPath as! NSIndexPath) 
     cell.posterImage.image = post.posterImage 
    } 
} 

,并请符合命名约定:

propertymethod名开始用小写字母

ClassStructEnum,Protocol名称以大写字母开头

相关问题