下面的代码成功显示了一个 TableView ,其中显示的数组“tweets”中包含推文 ID。有人可以告诉我如何修改它以显示特定用户句柄或 hastag 的所有推文吗?我想我必须使用 TwitterKit 中的 loadUserWithID 方法,但我不确定如何实现它。非常感谢
import UIKit
import TwitterKit
class TwitterViewController: UITableViewController, TWTRTweetViewDelegate {
let tweetTableReuseIdentifier = "TweetCell"
// Hold all the loaded Tweets
var tweets: [TWTRTweet] = [] {
didSet {
tableView.reloadData()
}
}
let tweetIDs = [
"184701590"] // our favorite bike Tweet
override func viewDidLoad() {
Twitter.sharedInstance().logInGuestWithCompletion { guestSession, error in
if (guestSession != nil) {
Twitter.sharedInstance().APIClient.loadTweetsWithIDs(self.tweetIDs) { tweets, error in
if let ts = tweets as? [TWTRTweet] {
self.tweets = ts
} else {
println("Failed to load tweets: \(error.localizedDescription)")
}
}
}
}
// Setup the table view
tableView.estimatedRowHeight = 150
tableView.rowHeight = UITableViewAutomaticDimension // Explicitly set on iOS 8 if using automatic row height calculation
tableView.allowsSelection = false
tableView.registerClass(TWTRTweetTableViewCell.self, forCellReuseIdentifier: tweetTableReuseIdentifier)
}
// MARK: UITableViewDelegate Methods
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tweets.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let tweet = tweets[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier(tweetTableReuseIdentifier, forIndexPath: indexPath) as TWTRTweetTableViewCell
cell.tweetView.delegate = self
cell.configureWithTweet(tweet)
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let tweet = tweets[indexPath.row]
return TWTRTweetTableViewCell.heightForTweet(tweet, width: CGRectGetWidth(self.view.bounds))
}
最佳答案
TwitterKit 支持直接显示用户时间轴。
class UserTimelineViewController: TWTRTimelineViewController, TWTRTweetViewDelegate {
convenience init() {
let dataSource = TWTRUserTimelineDataSource(screenName: "TomCruise", APIClient: TWTRAPIClient())
self.init(dataSource: dataSource)
self.title = "@\(dataSource.screenName)"
}
func tweetView(tweetView: TWTRTweetView, didSelectTweet tweet: TWTRTweet) {
print("Selected tweet with ID: \(tweet.tweetID)")
}
}
您可以在此处查看更多详细信息:https://dev.twitter.com/twitter-kit/ios/show-timelines
关于ios - 为嵌入式用户推文时间线加载用户推文数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29094353/