5

I thought Twitter Kit was supposed to help developers integrate Twitter in a few lines of code. The online documentation is poor to say the least. I am simply trying to display a single user's timeline in my app within a table view controller. I want read only, guest only access to the timeline. The below copy/paste from the online documentation simply brings up 2 cells populated with a greyed out image and a twitter logo but no tweets. What is wrong? Thanks

import UIKit
import TwitterKit

class TwitterViewController: UITableViewController, TWTRTweetViewDelegate {

    let tweetTableReuseIdentifier = "TweetCell"
    // Hold all the loaded Tweets
    var tweets: [TWTRTweet] = [] {
        didSet {
            tableView.reloadData()
        }
    }
    let tweetIDs = ["20", // @jack's first Tweet
        "510908133917487104"] // our favorite bike Tweet

    override func viewDidLoad() {
        // 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)

        Twitter.sharedInstance().logInGuestWithCompletion { guestSession, error in
            if (guestSession != nil) {
                // make API calls that do not require user auth
            } else {
                println("error: \(error.localizedDescription)");
            }
        }
        // Load Tweets
        Twitter.sharedInstance().APIClient.loadTweetsWithIDs(tweetIDs) { tweets, error in
            if let ts = tweets as? [TWTRTweet] {
                self.tweets = ts
            } else {
                println("Failed to load tweets: \(error.localizedDescription)")
            }
        }
    }

    // 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
        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))
    }
}
Ben Thomas
  • 105
  • 2
  • 12

2 Answers2

2

You need to call: cell.configureWithTweet(tweet)

in tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)

gdub
  • 793
  • 9
  • 16
2

Steven here, one of the Twitter Kit developers.

The best way to do this now is to subclass the TWTRTimelineViewController and set the dataSource property.

class UserTimelineViewController: TWTRTimelineViewController, TWTRTweetViewDelegate {

  convenience init() {
    // Show a timeline of @jack's Tweets
    let dataSource = TWTRUserTimelineDataSource(screenName: "jack", APIClient: TWTRAPIClient())
    self.init(dataSource: dataSource)

    // Set the title for Nav bar
    self.title = "@\(dataSource.screenName)"
  }

  func tweetView(tweetView: TWTRTweetView, didSelectTweet tweet: TWTRTweet) {
    // Log a message whenever a user taps on a tweet
    print("Selected tweet with ID: \(tweet.tweetID)")
  }

}

Screenshot of User Timeline

Steven Hepting
  • 12,394
  • 8
  • 40
  • 50
  • hey Steven, one quick question. How about if I would like to customize the cell view or show tweets in multiple sections what should I use? – Silviu St Oct 30 '16 at 20:57
  • Hi Steven Thank you for taking the time to explain. Your responses are much appreciated. – Ben Thomas Jan 17 '17 at 09:06
  • Customization options for `TWTRTimelineViewController` is extremely limited but it's the easiest way to show a user's complete timeline. For a `UITableView` you would be stuck with OP's method and will need to know the tweet ids of the tweets you want to display and that would likely involve using the JSON APIs and or other third party library. Any other suggestions @StevenHepting? – Anjan Biswas Oct 07 '17 at 20:29