How to subclass TWTRTweetTableViewCell in Swift Twitterkit?

久未见 提交于 2019-12-08 07:19:05

问题


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?


回答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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/47105778/how-to-subclass-twtrtweettableviewcell-in-swift-twitterkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!