【发布时间】:2015-07-05 15:08:14
【问题描述】:
由于我引用的单元格(在 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")
}
可见路径:
(
"<NSIndexPath: 0x7888f7b0> {length = 2, path = 0 - 0}",
"<NSIndexPath: 0x788a4d60> {length = 2, path = 0 - 1}"
)
单元格:
<NSIndexPath: 0x788a3c80> {length = 2, path = 0 - 1}
【问题讨论】:
-
let cell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell -
我已经尝试了你的建议。不幸的是,它没有改变任何东西。
-
FWIW Cocoa* 约定是变量和方法名以小写字母开头,类名以大写字母开头。当我看到:
topCell我最初认为这是一个变量。约定确实有帮助。 -
感谢您的评论。
-
看起来 indexPath 上没有单元格,您正在明确解开它。如果该单元格不可见,则它可能不存在。永远不要明确解开任何可能在任何情况下都为零的东西。来自文档:表示表格单元格的对象,如果单元格 不可见 或 indexPath 超出范围,则为 nil。看得见吗?
标签: ios swift uitableview xib custom-cell