无法转换 swift 类型的值
Could not cast value of type swift
尝试滚动后出现错误:
无法将类型 'Reviews.topCell' (0xaeb68) 的值转换为 'Reviews.contentCell' (0xaea98)。 (见最后一种方法)
我认为我引用了正确的单元格,尽管问题可能出在
cellForRowAtIndexPath 方法,因为尝试滚动后执行挂起。
完整源代码: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
}
}
}
}
在 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
}
}
请遵守命名规范:
property
、method
名称以小写字母开头
Class
、Struct
、Enum
、Protocol
名称以大写字母开头
尝试滚动后出现错误: 无法将类型 'Reviews.topCell' (0xaeb68) 的值转换为 'Reviews.contentCell' (0xaea98)。 (见最后一种方法) 我认为我引用了正确的单元格,尽管问题可能出在 cellForRowAtIndexPath 方法,因为尝试滚动后执行挂起。
完整源代码: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
}
}
}
}
在 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
}
}
请遵守命名规范:
property
、method
名称以小写字母开头
Class
、Struct
、Enum
、Protocol
名称以大写字母开头