我目前正試圖將一個NSMutableArray顯示到UITableView中。問題在於NumberOfRowsInSection和CellForRowAtIndex。我能夠獲取Twitter提要和邏輯到控制檯,但我似乎無法將其顯示到我的UITable視圖。可能是什麼原因?需要知道如何向UITableView Xcode顯示一個NSMutableArray 5
#import "ViewController.h"
#import <Accounts/Accounts.h>
#import <Social/Social.h>
#import "TwitterPostInfo.h"
#import "CustomCell.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
twitterPosts = [[NSMutableArray alloc]init];
[self refreshTwitter];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)refreshTwitter
{
ACAccountStore *accountStore = [[ACAccountStore alloc]init];
if (accountStore != nil)
{
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
if (accountType != nil)
{
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
if (granted)
{
//Succesful Access
NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
if (twitterAccounts != nil)
{
ACAccount *currentAccount = [twitterAccounts objectAtIndex:0];
if (currentAccount != nil)
{
NSString *requestString = @"https://api.twitter.com/1.1/statuses/user_timeline.json";
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:requestString] parameters:nil];
[request setAccount:currentAccount];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if ((error == nil) && ([urlResponse statusCode] == 200))
{
NSArray *twitterFeed = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
// loop throught all posts
for (NSInteger i=0; i<[twitterFeed count]; i++)
{
TwitterPostInfo *postInfo = [self createPostInfoFromDictionary:[twitterFeed objectAtIndex:i]];
if (postInfo != nil)
{
[twitterPosts addObject:postInfo];
}
}
}
}];
}
}
}
else
{
//Access Denied
}
}];
}
}
}
-(TwitterPostInfo*)createPostInfoFromDictionary:(NSDictionary*)postDictionary
{
NSString *timeDateString = [postDictionary valueForKey:@"created_at"];
NSDictionary *userDictionary = [postDictionary objectForKey:@"user"];
NSString *userString = [userDictionary valueForKey:@"screen_name"];
NSString *userDesc = [userDictionary valueForKey:@"description"];
NSString *tweetText = [postDictionary valueForKey:@"text"];
TwitterPostInfo *postInfo = [[TwitterPostInfo alloc]initWithPostInfo:userString userDesc:userDesc text:tweetText timeDateInfo:timeDateString];
return postInfo;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [twitterPosts count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CloneCell"];
if (cell != nil)
{
// ISSUE BEGINS HERE
//TwitterPostInfo *postInfo = (TwitterPostInfo*) twitterPosts[indexPath.row];
//cell = [twitterPosts objectAtIndex:indexPath.row];
cell.textLabel.text = [twitterPosts objectAtIndex:indexPath.row];
}
return cell;
}
@end
謝謝!這讚賞第一個答案! – user3078406