2014-07-09 49 views
0

我目前正試圖將一個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 

回答

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 

如果你想在顯示所有的鳴叫信息該單元格,您將不得不創建自己的自定義單元格,然後將單元格中的每個UILabel分配給存儲在TwitterPostInfo對象中的不同屬性。

+0

謝謝!這讚賞第一個答案! – user3078406

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; 
+0

感謝您的及時答覆!當你說故事板中的「創建一個合適的UITableViewCell」時,你的意思是? – user3078406

+0

@ user3078406這意味着這行:'cell = [[UITableViewCell alloc] init]; '。 – Eonil

相關問題