【发布时间】:2016-01-30 03:34:35
【问题描述】:
问题
将返回的 Twitter JSON 提要加载到 TwitterKit 表视图单元格时,不会加载个人资料图像和媒体图像。所有其他数据(用户名、转推状态、描述、链接)已加载并在 twitterTableView 中正确显示,但图像没有。
JSON 提要包含准确的图像 URL。
有没有人遇到过这种情况,或者有允许在视图控制器中嵌套 Twitter 表的解决方案?
版本信息
Xcode v7.0.1
代码库:Swift 2.0
应用兼容性 iOS 8.3+
TwitterCore v1.12.0
TwitterKit v1.12.0
情节提要中的 ViewController
- 查看
- 滚动视图
- InfoView(用于其他信息)
- SocialView(有关 Twitter 个人资料和 Twitter 源的信息)
- 个人资料信息查看
- twitterTable(未分配类,ViewController 作为数据源和委托)
- twitterTableViewCell(分配的 TWTRTweetTableViewCell 类)
元数据工作流程
- 应用调用专有服务器 API
- 服务器:
- 返回缓存的 Twitter JSON
- 使用搜索词调用 Twitter API。缓存 Twitter JSON 响应。返回 Twitter JSON。
- App 解析 JSON,并利用 TWTRTweet.tweetsWithJSONArray 设置 var tweets
- 在 tweets 集上,用 tweets 更新 twitterTable
ViewController 中的相关代码
import UIKit
import Alamofire
import SwiftyJSON
import RealmSwift
import TwitterKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, TWTRTweetViewDelegate {
@IBOutlet weak var socialView: UIView!
@IBOutlet weak var friendCollectionView: UICollectionView!
@IBOutlet weak var twitterTableView: UITableView!
@IBOutlet weak var friendColletionViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var twitterTableViewHeightConstraint: NSLayoutConstraint!
var tweets: [TWTRTweet] = [] {
didSet {
twitterTableView.reloadData()
}
}
var tweetHeights:[Int:CGFloat] = [:]
var isLoadingTweets = false
let tweetTableCellReuseIdentifier = "TweetCell"
var prototypeCell: TWTRTweetTableViewCell?
var isLoadingFriend = false
var friends:[Friend] = [] {
didSet {
friendCollectionView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.loadInfo()
self.loadFriends()
self.loadTweets()
// Create a single prototype cell for height calculations.
self.prototypeCell = TWTRTweetTableViewCell(style: .Default, reuseIdentifier: tweetTableCellReuseIdentifier)
// Register the identifier for TWTRTweetTableViewCell.
self.twitterTableView.registerClass(TWTRTweetTableViewCell.self, forCellReuseIdentifier: tweetTableCellReuseIdentifier)
}
func loadInfo(){
print("loads basic info")
// code removed to simplify post
}
func loadFriends(){
print("loads friendsCollectionView inside Info")
// code removed to simplify post
}
func loadTweets(){
print("loadTweets")
// Do not trigger another request if one is already in progress.
if self.isLoadingTweets {
return
}
self.isLoadingTweets = true
Alamofire.request(APIService.Router.TwitterSearch(searchType: "hashtag", searchTerm: "something", userSocialProfileID: ""))
.responseString { response in
print("request: \(response.0!)")
print("\n*******loadTweets********\n\n\(response.2.value!)\n\n**************\n")
}
.responseJSON { response in
// print("\n*******loadTweets:JSON********\n\n\(response.2.value!)\n\n**************\n")
if let json = response.2.value {
var jsonObj = JSON(json)
if let results = jsonObj["results"].dictionary{
if let jsonTweets = results["statuses"]!.arrayObject {
// print("The tweets are \n\(jsonTweets)")
self.tweets = TWTRTweet.tweetsWithJSONArray(jsonTweets) as! [TWTRTweet]
} else {
print("jsonTweets are not an array of Objects")
}
} else {
print("Results is not a dictionary.")
}
dispatch_async(dispatch_get_main_queue(),{
self.twitterTableViewHeightConstraint.constant = 0
for (_, tweetHeight) in self.tweetHeights {
self.twitterTableViewHeightConstraint.constant += tweetHeight
}
self.isLoadingTweets = false
})
} else {
print("value was not json...or something is wrong with the code")
}
}
}
//MARK: UITABLEVIEW
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tweets.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let tweet = self.tweets[indexPath.row] as TWTRTweet!
if tweets.count > indexPath.row {
prototypeCell?.configureWithTweet(tweet)
}
let tweetHeight = TWTRTweetTableViewCell.heightForTweet(tweet, width: tableView.bounds.width)
self.tweetHeights[indexPath.row] = tweetHeight
return tweetHeight
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(tweetTableCellReuseIdentifier, forIndexPath: indexPath) as! TWTRTweetTableViewCell
// Assign the delegate to control events on Tweets.
cell.tweetView.delegate = self
cell.tweetView.showActionButtons = false
cell.tweetView.linkTextColor = UIColor().cosMediumPurple()
// Retrieve the Tweet model from loaded Tweets.
let tweet = tweets[indexPath.row] as TWTRTweet!
// Configure the cell with the Tweet.
cell.configureWithTweet(tweet)
// Return the Tweet cell.
return cell
}
}
【问题讨论】:
-
感谢您提供出色的错误报告。