需要知道如何向UITableView Xcode 5显示NSMutableArray

需要知道如何向UITableView Xcode 5显示NSMutableArray,uitableview,nsmutablearray,Uitableview,Nsmutablearray,我当前正在尝试将NSMutableArray显示到UITableView中。问题在于节中的行数和单元格中的行数索引。我能够将twitter提要和逻辑提取到控制台,但我似乎无法将其显示到我的UITable视图中。原因可能是什么 #import "ViewController.h" #import <Accounts/Accounts.h> #import <Social/Social.h> #import "TwitterPostInfo.h" #import "Custo

我当前正在尝试将NSMutableArray显示到UITableView中。问题在于节中的行数和单元格中的行数索引。我能够将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
dequeueReusableCellWithIdentifier:可能返回nil。在这种情况下,您需要自己创建一个适当的UITableViewCell

如果单元格为零,请尝试创建该单元格。始终确保返回有效的单元格对象

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CloneCell"];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] init];
}

cell.textLabel.text = [twitterPosts objectAtIndex:indexPath.row];
return cell;

关于有效cell对象的帖子很重要,但这里真正的问题是您正在将TwitterPostInfo类型的对象分配给cell.textLabel.text,这是一个NSString

如果您试图显示的是Tweet的文本,您将需要类似以下内容:

TwitterPostInfo *postInfo = (TwitterPostInfo*) twitterPosts[indexPath.row];
cell.textLabel.text = postInfo.text; //or however your getter for the text is implemented

如果您试图在单元格中显示推文的所有信息,则必须创建自己的自定义单元格,然后将单元格中的每个UILabel指定给TwitterPostInfo对象中存储的不同属性。

谢谢您的提示!当你说在故事板中创建一个合适的UITableViewCell时,你的意思是?@user3078406,意思是:cell=[[UITableViewCell alloc]init]。非常感谢。这是对第一个答案的赞美!