Wie kann ich ein Twitter Feed in einen Table View integrieren ??

Alexone Dev

Registriert
Thread Starter
Dabei seit
14.03.2013
Beiträge
4
Reaktionspunkte
0
Hi Community,

In eine neue Version meiner App würde ich gerne mein Twitterfeed per Tableview statt per Webview einfügen aber ich habe keine Ahnung wie ich das anstellen soll. In Google finde ich auch nichts :/ Kann mir jemand einen Tipp geben wie ich das mache? Ich habe generell noch wenig Ahnung von Tableviews an sich.

Gruß Alex
 
ViewController.h
Code:
#import <UIKit/UIKit.h>


@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>


@property (strong, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) NSArray *tweets;


@end

ViewController.m
Code:
#import "ViewController.h"


@implementation ViewController


@synthesize myTableView;
@synthesize tweets;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Issue load request after the view has been loaded.
    [self issueLoadRequest];
}


- (void)viewDidUnload
{
    [self setMyTableView:nil];
    [super viewDidUnload];
}


- (void)issueLoadRequest
{
    // Dispatch this block asynchronosly. The block gets JSON data from the specified URL and performs the proper selector when done.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://api.twitter.com/1/statuses/user_timeline.json?screen_name=derstandardat&count=10"]];
        [self performSelectorOnMainThread:@selector(receiveData:) withObject:data waitUntilDone:YES];
    });
}


- (void)receiveData:(NSData *)data {
    // When we have the data, we serialize it into native cocoa objects. (The outermost element from twitter is
    // going to be an array. I JUST KNOW THIS. Reload the tableview once we have the data.
    self.tweets = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    [self.myTableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tweets.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    // The element in the array is going to be a dictionary. I JUST KNOW THIS. The key for the tweet is "text".
    NSDictionary *tweet = [self.tweets objectAtIndex:indexPath.row];
    cell.textLabel.text = [tweet objectForKey:@"text"];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:11];
    cell.textLabel.numberOfLines = 3;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Spit out some pretty JSON for the tweet that was tapped. Neato.
    NSString *formattedJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:[self.tweets objectAtIndex:indexPath.row] options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
    NSLog(@"tweet:\n%@", formattedJSON);
}




@end

bySha7x
 
Zurück
Oben Unten