Reputation: 402
I get an error after an attempt to scroll: Could not cast value of type 'Reviews.topCell' (0xaeb68) to 'Reviews.contentCell' (0xaea98). (see last method) I think that I reference correct cells, although the problem might be in the cellForRowAtIndexPath method as the execution hangs on it after attempted scroll.
Full source code: 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
}
}
}
}
Upvotes: 0
Views: 2065
Reputation: 285290
in the loadImagesForOnscreenRows()
method there is probably an else
missing
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
}
}
and please conform to the naming convention:
property
, method
names start with lowercase letter
Class
, Struct
, Enum
, Protocol
names start with capital letter
Upvotes: 1