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.