1

I am trying to find out if it is possible to subclass TWTRTweetTableViewCell from the TwitterKit library. So far I have my custom cell class inherit from TWTRTweetTableViewCell. The xib has a UIView in it which has an outlet to the cell class and the UIView class is set to TWTRTweetView. Like this-

class UserTweetViewCell: TWTRTweetTableViewCell {
    @IBOutlet weak var tweetViewCustom: TWTRTweetView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

The cell's class in property inspector is set to UserTweetViewCell and the UIVIew's class in the cell is set to TWTRTweetView.

In the main view controller I have this

tableView.register(UserTweetViewCell.self, forCellReuseIdentifier: tweetTableReuseIdentifier)

and then

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let tweet = tweetsarr[indexPath.row]

        let cell = tableView.dequeueReusableCell(withIdentifier: tweetTableReuseIdentifier, for: indexPath) as! UserTweetViewCell
        cell.tweetViewCustom.showActionButtons = false
        cell.tweetViewCustom.linkTextColor = UIColor(red:0.12, green:0.53, blue:0.90, alpha:1.0)
        cell.tweetViewCustom.configure(with: tweet as? TWTRTweet)
        cell.tweetViewCustom.theme = .light
        cell.tweetViewCustom.delegate = self

        return cell
    }

However, i get an error at line cell.tweetViewCustom.showActionButtons = false and the error is Unexpectedly found nil while unwrapping an Optional value. What am I missing here?

Anjan Biswas
  • 7,746
  • 5
  • 47
  • 77
  • Quick check that "uiserTimeline" is not a typo of userTimeline? or why it is "uiserTimeline" instead of tableView.dequeueReusableCell(...)? – Louis Leung Nov 04 '17 at 00:12
  • Yes its a typo , `uiserTimeline` is just the TableView Outlet in my view controller class, it would be the same with `tableView.dequeueReusableCell(...)`. I've updated the question to avoid confusion. – Anjan Biswas Nov 04 '17 at 03:51
  • @Annjawn Is it possible to show retweet and like counts in tweetView? – Balasubramanian Dec 01 '17 at 12:07
  • @Annjawn I'm also facing exact issue. [See this](http://prntscr.com/hhs6tz) How you solve this? – Balasubramanian Dec 01 '17 at 16:07
  • @Balasubramanian no, there is no inbuilt way of showing like counts inside TWTRTweetView. You will have to do your own customization to show likes count, retweet count, reply button etc. – Anjan Biswas Dec 01 '17 at 16:08
  • [In this post](https://stackoverflow.com/questions/29877453/show-retweet-counts-using-twitter-fabric-android-sdks-tweetviewadpater) they have got it. But its in android. Like you, I have inherited cell and tweetView class. But couldn't able to achieve it. – Balasubramanian Dec 01 '17 at 16:10
  • It's possible in iOS too, but you will have to code it. I attempted showing these buttons right below TWTRTweetView and was using auto layout, and for some reason it was messing up the TWTRTweetView. I am going to find a way anyways I will let you know when i do it. – Anjan Biswas Dec 01 '17 at 16:15
  • Thanks mate. I'm also trying it. Will update you once I get. – Balasubramanian Dec 01 '17 at 16:18
  • I wanted to Subclass TWTRTweetTableViewCell so that I could add the likes count, retweets count, reply button etc. so far it hasn't worked. So next I am going to give it a try Subclassing TWTRTweetView and use that in the tableview cell instead. I think I have tried it once with partial success. The challenge is the tweet height. – Anjan Biswas Dec 01 '17 at 16:30

2 Answers2

1

I finally did it and it's working like a charm. The trick is not to subclass TWTRTweetTableViewCell but instead just subclass a regular UITableViewCell and use a TWTRTweetView inside of it. Which is basically what TWTRTweetTableViewCell does, it has tweetView property which is essentially an IBOutlet of type TWTRTweetView. The custom cell Nib should contain a UIView with TWTRTweetView set as it's class in the identity inspector. Here goes the code-

class CustomTweetCell: UITableViewCell{
    @IBOutlet weak var customTweetView: TWTRTweetView!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }

    func configureCell(with tweet: TWTRTweet){
        self.customTweetView.showActionButtons = false
        self.customTweetView.configure(with: tweet)
    }

}

For the cell's height, the following needs to be done for the tableview-

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let tweet = tweets[indexPath.row]
        let tweetheight = TWTRTweetTableViewCell.height(for: tweet as! TWTRTweet, style: .compact, width: self.view.frame.width, showingActions: false) + 30 //this 30 should be the height of any additional views that you put in the cell Nib file
        return tweetheight
    }

NOTE: Its extremely important to have autolayout constraints enabled within the tableview cell with the TWTRTweetView and any other views that you may have and also make sure the Table view cell row height is set to Default or blank in the cell's Size inspector.Failing to do so will mess up the tweet view height and will cause undesirable results.

enter image description here

Anjan Biswas
  • 7,746
  • 5
  • 47
  • 77
0

I wanted to Subclass TWTRTweetTableViewCell so that I could add the likes count, retweets count, reply button etc. so far it hasn't worked. So next I am going to give it a try Subclassing TWTRTweetView and use that in the tableview cell instead. I think I have tried it once with partial success. The challenge is the tweet height

This is how I am calculating the tweet height in Objective-c:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
TWTRTweet * tweet = self.tweets[indexPath.row]; 
    if (self.tweets.count > indexPath.row) {
       [self.prototypeCell configureWithTweet:tweet];
    }
    CGFloat tweetHeight = [CustomTweetTableViewCell heightForTweet:tweet style:TWTRTweetViewStyleCompact width:[tableView bounds].size.width showingActions:YES];

    self.tweetHeights[indexPath.row] = [NSNumber numberWithFloat:tweetHeight];

    return tweetHeight;
}
Balasubramanian
  • 5,274
  • 6
  • 33
  • 62
  • 1
    Cool. I am on swift but I will give this method a try. Just to be clear. I have been able to return the actual tweets height just like you did but auto layout is messing up the view. I think I will have to play around with the intrinsic content size a bit. – Anjan Biswas Dec 01 '17 at 16:46
  • 1
    Alright, I have successfully achieved it. See my answer. – Anjan Biswas Dec 03 '17 at 06:12