1

下面的代码成功地显示了一个表格视图,其中显示了数组“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))
}
4

1 回答 1

0

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

于 2015-05-12T01:14:26.657 回答