2015-03-06 16 views
5

Tôi nghĩ rằng Twitter Kit được cho là sẽ giúp các nhà phát triển tích hợp Twitter trong một vài dòng mã. Các tài liệu trực tuyến là người nghèo để nói rằng ít nhất. Tôi chỉ đơn giản là cố gắng để hiển thị một dòng thời gian của người dùng trong ứng dụng của tôi trong một bộ điều khiển xem bảng. Tôi chỉ muốn đọc, khách chỉ truy cập vào dòng thời gian. Bản sao/dán dưới đây từ tài liệu trực tuyến chỉ đơn giản là hiển thị 2 ô có hình ảnh màu xám và biểu tượng twitter nhưng không có tweet. Chuyện gì thế? Cảm ơnTwitter kit - iOS

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

Trả lời

2

Bạn cần phải gọi: cell.configureWithTweet(tweet)

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

+0

Cảm ơn câu trả lời của bạn sẽ giúp tôi :) Tôi có thể đặt một câu hỏi: Từ nơi chúng tôi có thể nhận được id tweet của? – Mariam

+4

liên kết github không còn giá trị. – user1171911

2

Steven đây, một trong những nhà phát triển Kit Twitter.

Cách tốt nhất để làm điều này ngay bây giờ là phân lớp TWTRTimelineViewController và đặt thuộc tính dataSource.

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

+0

hey Steven, một câu hỏi nhanh. Làm thế nào về nếu tôi muốn tùy chỉnh các tế bào xem hoặc hiển thị tweets trong nhiều phần những gì tôi nên sử dụng? –

+0

Xin chào Steven Cảm ơn bạn đã dành thời gian giải thích. Câu trả lời của bạn được đánh giá cao. –

+0

Tùy chọn tùy chỉnh cho 'TWTRTimelineViewController' rất hạn chế nhưng đó là cách dễ nhất để hiển thị dòng thời gian hoàn chỉnh của người dùng. Đối với một 'UITableView' bạn sẽ bị mắc kẹt với phương pháp của OP và sẽ cần phải biết các id tweet của tweets bạn muốn hiển thị và có khả năng liên quan đến việc sử dụng các API JSON và thư viện của bên thứ ba khác. Bất kỳ đề xuất nào khác @StevenHepting? – Annjawn

Các vấn đề liên quan