objective-c - 简单的 Twitter 程序无法运行

标签 objective-c ios xcode twitter

我正在使用本教程来练习创建一个极其基本的 Twitter 应用程序:http://www.codeproject.com/Articles/312325/Making-a-simple-Twitter-app-using-iOS-5-Xcode-4-2#setting-up-the-table-view

我的应用程序中的唯一区别是我仅使用 tableView ViewController。我似乎无法让它发挥作用。

ViewController.h

@interface ViewController : UIViewController {


NSArray *tweets;
}
-(void)fetchTweets;

@property (retain, nonatomic) IBOutlet UITableView *tableView;

@end

ViewController.m

#import "ViewController.h"
#import "Twitter/Twitter.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize tableView = _tableView;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self fetchTweets];
}

- (void)fetchTweets
{
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData* data = [NSData dataWithContentsOfURL:
                    [NSURL URLWithString: @"https://api.twitter.com/1/statuses/public_timeline.json"]];

    NSError* error;

    tweets = [NSJSONSerialization JSONObjectWithData:data
                                             options:kNilOptions
                                               error:&error];

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
});
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TweetCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
NSString *text = [tweet objectForKey:@"text"];
NSString *name = [[tweet objectForKey:@"user"] objectForKey:@"name"];

cell.textLabel.text = text;
cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", name];

return cell;
}

- (void)viewDidUnload
{
_tableView = nil;
[self setTableView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
    return YES;
}
}

@end

最佳答案

您忘记为表定义委托(delegate)和数据源,并且就我在代码中看到的情况而言,没有正确实现协议(protocol),

尝试在您的 .h 文件中:

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    // your implementation...
}

以及 viewDidLoad 中的 .m 文件

self.tableView.dataSource = self;
self.tableView.delegate = self;

在您为此表定义委托(delegate)和数据源之前,numberOfRowscellForRow 等方法将无法工作:)

关于objective-c - 简单的 Twitter 程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10179806/

相关文章:

ios - 在 Storyboard 中设置 View Controller root

ios - 在 IOS 6.0 或 iPhone 中上传时拍摄的照片自动旋转

ios - CBConcreteCentralManager 未开机错误

xcode - iOS 6 : present a modal view controller

ios - 静态链接dylib

objective-c - 调用 sleep (5);并更新文本字段不起作用

objective-c - 未知类型名称 uint32/uint16

ios - 本地化不适用于 Xcode

ios - UIDocumentPickerViewController 崩溃

ios - 如何开始在 mac os 的 cocoa 应用程序中处理标签?