我目前正在尝试将NSMutableArray显示到UITableView中。问题出在NumberOfRowsInSection和CellForRowAtIndex中。我能够将Twitter提要和逻辑提取到控制台,但我似乎无法将其显示在我的UITable视图中。可能是什么原因?
#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
答案 0 :(得分:0)
dequeueReusableCellWithIdentifier:
可能会返回nil
。在这种情况下,您需要自己创建一个合适的UITableViewCell
。
如果是nil
,请尝试创建一个单元格。始终确保返回有效的单元格对象。
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CloneCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] init];
}
cell.textLabel.text = [twitterPosts objectAtIndex:indexPath.row];
return cell;
答案 1 :(得分:0)
关于有效单元格对象的帖子很重要,但您真正的问题是您要将TwitterPostInfo
类型的对象分配给cell.textLabel.text
,这是NSString
。
如果您要显示的内容是推文的文字,您会想要更像这样的内容:
TwitterPostInfo *postInfo = (TwitterPostInfo*) twitterPosts[indexPath.row];
cell.textLabel.text = postInfo.text; //or however your getter for the text is implemented
如果您尝试在单元格中显示Tweet的所有信息,则必须创建自己的自定义单元格,然后将单元格中的每个UILabel分配给存储在TwitterPostInfo对象中的不同属性