Load tweets from a #hashtag in tabelview cells using logInGuestWithCompletion

ios
embeddedtweet
swift
embeddedtimeline

#1

I feel like I’m so close, but it’s not quite working. I would love your advice. These are my two questions.

Question 1: Why aren’t the cells loading?

Question 2: How do I adapt the tweetIDs to load hashtag queries?

What I’m Trying to Acheive:
In my app, I have a Viewcontroller with a UITableView. The first two cells of the tableview are my own custom cells. I would like the rest of the cells (indexPath.row >= 2) to load current tweets from a specific hashtag or hashtags. Also, I’m not having the user login through Twitter (yet!), so I believe I need to use the logInGuestWithCompletion.

Here is the pertinent code I’m using in my ViewController. It’s pretty much straight from the documentation for now:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, TWTRTweetViewDelegate {

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

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    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 { session, error in
          if (session != nil) {
                Twitter.sharedInstance().APIClient.loadTweetsWithIDs(self.tweetIDs) { tweets, error in
                    if let ts = tweets as? [TWTRTweet] {
                        self.tweets = ts
                        println("Tweets count: \(self.tweets.count)")
                    } else {
                        println("Failed to load tweets: \(error!.localizedDescription)")
                    }
                }
          }
     }
}

I also call the required tableview functions:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tweets.count + 2   //This is for me to add my own 2 cells
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // I  set up my 2 cells in here too, but left that out for space.
            let tweet = tweets[indexPath.row - 2]
            let cell = tableView.dequeueReusableCellWithIdentifier(tweetTableReuseIdentifier, forIndexPath: indexPath) as! TWTRTweetTableViewCell
            cell.tweetView.delegate = self
            return cell
        }
    }
    
     func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
            // I  set up my 2 cells in here too, but left that out for space.
            let tweet = tweets[indexPath.row - 2]
            return TWTRTweetTableViewCell.heightForTweet(tweet, width: CGRectGetWidth(self.view.bounds))
        }
    }

With this code my cells load, but it only returns 2 twitter cells and their content doesn’t load. I get 2 empty imageviews and 2 mini twitter icons. I can see that it is formatted and the cells are different sizes, so some information is coming across, but there is no text or pictures. Also, the debugger shows no errors.

Question 1: Why aren’t the cells loading?

Question 2: How do I adapt the tweetIDs to load hashtag queries?

Below is a screen grab of the faulty TweetCells

All thoughts are appreciated. Thank you.


#2

Did you try to check what you get from tweets.count. My guess its empty.

Also do you just want to load a fixed number of tweets or create a timeline?

I use TWTRTimelineViewController so I can create a timeline, with it you can use TWTRSearchTimelineDataSource() to query for a specific search term.

heres my code:

import UIKit
import TwitterKit

class StreamTableViewController: TWTRTimelineViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Request Twitter Feed
    Twitter.sharedInstance().logInGuestWithCompletion { guestSession, error in
        
        if guestSession != nil {
            
            let client = Twitter.sharedInstance().APIClient
            self.dataSource = TWTRSearchTimelineDataSource(searchQuery: "$YHOO", APIClient: client)
        
        } else {
            
            print("error: \(error.localizedDescription)");
        }
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

#3

Thanks for the reply.

My println for the self.tweets.count gives me 2. So it appears that it’s getting something.

And yes, I think I want to create a timeline… at least, I know I don’t want to load a fixed number of tweets.

I have used the TWTRTimelineViewController as you suggested (with the search query) and that does work. However, it keeps me from being able to load my two custom cells above it. If there was a way to add my custom cells, I’d be all for it. I’m just not sure how that would work.

One work around that I tried was to put the TWTRTimelineViewController in a containerview. And that sort of solved my problem, but restricted the scrolling twitter timeline to the lower portion of the page with my custom header fixed at the top. It wasn’t the best user experience. Especially on smaller phones.

Thanks for this. I am open to other implementations or variations on the things I’ve tried, if you have any ideas.


#4

Unfortunately the TWTRTimelineViewController needs to be set as a class for a Controller. And gives you a limited space for customization. I have also opened 6 issues with regards to things I found here:

PS I’m very new to Fabric and not sure I like it. Just started using it yesterday and was boggled by the amount of setbacks I encountered. Also this community is very limited, with not much engagement on other questions and not many replies from Twitter team. So I tweeted Fabric directly on twitter the link to my issues and they said they will look into it.


#5

That’s all good to know. I’m brand new to Fabric also. Started with it last night as a way to add value to a couple pages in my app. I’m clearly still figuring out my way around it. I’ve also emailed support, but have a feeling that a Friday before a holiday might not be the best time. If I don’t hear anything back, I’ll try again next week.

I’m going to check out your topic. Perhaps it will give me some insight or let me know about potential future problems.

If I manage to figure something out, I’ll make sure to post it here.

I think I’ll try looking at TWTRTimelineViewController again and see if I can get it to play nicely with other elements.

Thanks.


#6