Nic*_*ius 5 iphone uitableview custom-cell ios swift
我在这里遇到了非常奇怪的问题。
基本上,用户会看到一个附加了 TableView 的视图。调用后端获取一些数据,并在完成块中将该数据提供给 TableView 进行显示。
在 cellForRow 1 of 4 可重用的自定义单元中,根据某种逻辑将自定义单元出队,然后调用该单元的配置方法。
这就是它变得棘手的地方:一些单元格可能需要进一步的异步调用(以获取更多数据用于其自定义显示),因此需要 tableView.reloadRows 调用......那么,发生的事情最好通过这两个屏幕截图来解释:
第一个单元格中写着“有没有想过你会为克朗代克酒吧做什么?” 被复制到“演员的自我管理”单元格的顶部,作为包含 Twitter 信息的单元格,该单元格确实需要额外的异步调用来获取该数据。
有什么想法吗?我正在 begin/endUpdates 块内运行 tableView.reloadRows 调用,但没有骰子。
行单元格:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let postAtIndexPath = posts[indexPath.row]
let cell: PostCell!
if postAtIndexPath.rawURL == "twitter.com" {
cell = tableView.dequeueReusableCell(withIdentifier: "TwitterCell", for: indexPath) as! TwitterCell
} else if !postAtIndexPath.rawURL.isEmpty {
cell = tableView.dequeueReusableCell(withIdentifier: "LinkPreviewCell", for: indexPath) as! LinkPreviewCell
} else if postAtIndexPath.quotedID > -1 {
cell = tableView.dequeueReusableCell(withIdentifier: "QuoteCell", for: indexPath) as! QuoteCell
} else {
cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostCell
}
cell.isUserInteractionEnabled = true
cell.privacyDelegate = self
cell.shareDelegate = self
cell.likeDelegate = self
cell.linkPreviewDelegate = self
cell.reloadDelegate = self
postToIndexPath[postAtIndexPath.id] = indexPath
if parentView == "MyLikes" || parentView == "Trending" {
cell.privatePostButton.isHidden = true
cell.privatePostLabel.isHidden = true
}
cell.configureFor(post: postAtIndexPath,
liked: likesCache.postIsLiked(postId: postAtIndexPath.id),
postIdToAttributions: postIdToAttributions,
urlStringToOGData: urlStringToOGData,
imageURLStringToData: imageURLStringToData)
cell.contentView.layoutIfNeeded()
return cell
}
Run Code Online (Sandbox Code Playgroud)
异步 Twitter 调用:
private func processForTwitter(og: OpenGraph, urlLessString: NSMutableAttributedString, completion:(() -> ())?) {
let ogURL = og[.url]!
let array = ogURL.components(separatedBy: "/")
let client = TWTRAPIClient()
let tweetID = array[array.count - 1]
if let tweet = twitterCache.tweet(forID: Int(tweetID)!) {
for subView in containerView.subviews {
subView.removeFromSuperview()
}
let tweetView = TWTRTweetView(tweet: tweet as? TWTRTweet, style: .compact)
containerView.addSubview(tweetView)
tweetView.translatesAutoresizingMaskIntoConstraints = false
tweetView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
tweetView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
containerView.leftAnchor.constraint(equalTo: tweetView.leftAnchor).isActive = true
containerView.rightAnchor.constraint(equalTo: tweetView.rightAnchor).isActive = true
containerView.isHidden = false
postText.isHidden = false
} else {
client.loadTweet(withID: tweetID, completion: { [weak self] (t, error) in
if let weakSelf = self {
if let tweet = t {
weakSelf.twitterCache.addTweetToCache(tweet: tweet, forID: Int(tweetID)!)
}
}
if let complete = completion {
complete()
}
})`enter code here`
}
if urlLessString.string.isEmpty {
if let ogTitle = og[.title] {
postText.text = String(htmlEncodedString: ogTitle)
}
} else {
postText.attributedText = urlLessString
}
}
Run Code Online (Sandbox Code Playgroud)
表视图控制器中的完成块:
func reload(postID: Int) {
tableView.beginUpdates()
self.tableView.reloadRows(at: [self.postToIndexPath[postID]!], with: .automatic)
tableView.endUpdates()
}
Run Code Online (Sandbox Code Playgroud)
注意:这种情况并不是 100% 都会发生,它可能与网络有关。
estimatedRowHeight事实证明,和周围的行为很奇怪,或者至少我不明白reloadRows;解决方案,通过一个切向相关的SO问题,是在行高可用时缓存行高,并在heightForRow.
我想它不会在重新加载时重新计算高度或其他什么?真的不明白为什么这有效,但我很高兴它有效!
| 归档时间: |
|
| 查看次数: |
2871 次 |
| 最近记录: |