いろいろフレームワークがしてくれて楽なのですが、まだまだ未完成のようで、あまり機能がありません・・・
また、公式ドキュメントも一部Objective-Cしかなく、頼りないです。
ただし、セルの高さの計算・アイコン画像の非同期受信などはすべて自動で行ってくれます。ログインボタンもとても簡単に実装することができます。
Qiitaー[iOS]FabricでTwitterクライアントを作った
このページがとてもわかり易いです。Fabricの適用方法などはこちらを参照してください。
このページのTwitterAPI.swiftを拡張して、サーチ・投稿・IDによるツイートの取得の関数を追加しました。
GitHub上に公開しています。
https://github.com/ha1fha1f/Lit_final/blob/master/TwitterAPI.swift
以下、上のAPIを用いたデータの保持・取得を行うクラスの例です。(2015/4/17現在)
import Foundation
import TwitterKit
class TweetDataModel :NSObject{
var tweets:[TWTRTweet]
// initialize
override init() {
self.tweets = []
}
func fetchTimeline(maxid:String?){
TwitterAPI.getHomeTimeline({
twttrs in
println("fetch")
if self.tweets.count == 0 {
self.tweets = twttrs
}else{
let tmptweets:[TWTRTweet]
if twttrs[0].tweetID > self.tweets.last?.tweetID {//新しいツイートがある
tmptweets = reverse(twttrs)
}else{
//if twttrs.last?.tweetID < self.tweets[0].tweetID{//古いツイートがある
tmptweets=twttrs
}
for tweetCell in tmptweets {
if tweetCell.tweetID > self.tweets[0].tweetID {//新ツイートを上に追加
self.tweets.insert(tweetCell,atIndex: 0)
}else if tweetCell.tweetID < self.tweets.last?.tweetID {//古いツイートを下に追加
self.tweets.append(tweetCell)
}
}
}
println("finish")
//notificationを送る
NSNotificationCenter.defaultCenter().postNotificationName("tweetLoaded", object: nil)
},
maxid: maxid,
count: "40",
error: {error in println(error.localizedDescription)})
}
}
ご参考までに。
これは.Darkのテーマでの表示です!
セルのテーマカラーは二種類用意されています。.Darkと.Lightです。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! OriginalTweetTableViewCell
println("\(indexPath.row)")
if tweetData.tweets.count > indexPath.row {
let tweet = tweetData.tweets[indexPath.row]
cell.tag = indexPath.row
cell.configureWithTweet(tweet)
cell.tweetView.theme = .Dark
cell.delegate = self
}
return cell
}
のように、セルを返すときに、tweetView.themeを設定すればよいです。僕の場合はTWTRTweetCellを継承したオリジナルのクラスを作ったのでやや異なりますが、設定方法などはだいたい同じです。
(https://github.com/ha1fha1f/Lit_final/blob/master/Litfinal/OriginalCell.swift)
雑になってしまいましたが、終わります


0 件のコメント:
コメントを投稿