由于我引用的单元格(在DownloadProfilePicture中)可见,我得到了主题中陈述的错误,原因不明。基本上,我正在尝试复制/改编所做的工作 LazyTableViewImages
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(topCell.self, forCellReuseIdentifier: "topCell")
self.tableView.registerClass(contentCell.self, forCellReuseIdentifier: "contentCell")
tableView.registerNib(UINib(nibName: "topCell", bundle: nil), forCellReuseIdentifier: "topCell")
tableView.registerNib(UINib(nibName: "contentCell", bundle: nil), forCellReuseIdentifier: "contentCell")
self.tableView.delegate = self
self.tableView.dataSource = self
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let nodeCount = arrayOfPosts.count;
let cell = UITableViewCell()
tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell
if (nodeCount > 0)
{
if (indexPath.row % 2 == 0){
// Set up the cell representing the app
var 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)
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
}
func downloadProfileImage(post: Post, indexPath: NSIndexPath){
println(self.tableView.indexPathsForVisibleRows())
println(indexPath)
let cell:topCell = self.tableView.cellForRowAtIndexPath(indexPath) as! topCell
cell.profileImage.image = UIImage(named: "img1")
}
visiblePaths:
(
"<NSIndexPath: 0x7888f7b0> {length = 2, path = 0 - 0}",
"<NSIndexPath: 0x788a4d60> {length = 2, path = 0 - 1}"
)
细胞:
<NSIndexPath: 0x788a3c80> {length = 2, path = 0 - 1}
答案 0 :(得分:2)
在方法中,表视图单元格被创建两次。 尝试这样的结构,必须保证返回表视图单元格
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let nodeCount = arrayOfPosts.count;
let cell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell
if (nodeCount > 0) {
if (indexPath.row % 2 == 0){
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)
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
}