0

我是Objective-C的新手。我花了無數小時被困在獲得一個空白桌面,我在這一點上絕望。 我使用API​​通過JSON調用加載twitter數據。我將所有內容都存儲在一個NSDictionary中,運行一個for循環來僅選擇「文本」值。我將過濾的字典存儲在一個對象中,我稍後在TableView初始化中使用它。 我爲我的自定義單元格創建了UItableViewCell的子類。 我的數據源和代表似乎也連接良好(這是我的想法至少) 我很難找到我的問題。如果有人能幫助我。使用JSON序列化從Twitter填充自定義TableViewCell

#import "ViewController.h" 
#import "myCell.h" 
#import <TwitterKit/TwitterKit.h> 

@interface ViewController() 
@end 

@implementation ViewController 
@synthesize myTableView; 

NSMutableArray *tweetObject; 
NSDictionary *dictionary; 
NSString *name; 
NSString *text; 


- (void)viewDidLoad { 
    tweetObject = [[NSMutableArray alloc] init]; 
    [super viewDidLoad]; 
    self.myTableView.dataSource = self; 
    self.myTableView.delegate = self; 
    text = @"text"; 
    [[Twitter sharedInstance] logInGuestWithCompletion:^(TWTRGuestSession *guestSession, NSError *error) { 
     if (guestSession) { 
      // make API calls that do not require user auth 
      NSString *statusesShowEndpoint = @"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=goofernator"; 
      NSError *clientError; 
      NSURLRequest *request = [[[Twitter sharedInstance] APIClient] 
            URLRequestWithMethod:@"GET" 
            URL:statusesShowEndpoint 
            parameters:0 
            error:&clientError]; 

      if (request) { 
       [[[Twitter sharedInstance] APIClient] 
       sendTwitterRequest:request 
       completion:^(NSURLResponse *response, 
           NSData *data, 
           NSError *connectionError) { 
        if (data) { 
         // handle the response data e.g. 
         NSError *jsonError; 
         NSDictionary *json = [NSJSONSerialization 
               JSONObjectWithData:data 
               options:0 
               error:&jsonError]; 

         for (NSDictionary *dataDict in json) { 
          NSString *text = [dataDict valueForKeyPath: @"text"]; 
          dictionary = [NSDictionary dictionaryWithObjectsAndKeys:text,@"bodytext",nil]; 
          [tweetObject addObject:dictionary]; 
          } 

        } 
        else { 
         NSLog(@"Error: %@", connectionError); 
        } 
       }]; 
      } 
      else { 
       NSLog(@"Error: %@", clientError); 
      } 
     } else { 
      NSLog(@"error: %@", [error localizedDescription]); 
     } 
    }]; 
    } 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

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

} 

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

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

    NSDictionary *tmpDict = [self.tweetObject objectAtIndex:indexPath.row]; 

    cell.txtLblOutput.text = [tmpDict objectForKey:text]; 
    [tableView reloadData]; 

    return cell; 
} 

@end 

在這裏你可以看到我的故事板是放在一起的方式,並引用我使用

http://postimg.org/image/79w7pqmu3/

http://postimg.org/image/ixq9kabyz/

回答

0

你應該叫[tableView reloadData];在您的要求完成處理後,您已經填滿了你的陣列。檢查您是否收到任何數據。數組是否被字典對象填充?

不過,嚴重的是,你需要閱讀一些關於編碼的好書,你的代碼真的不瞭解你在做什麼。請從- ...cellForRowAtIndexPath:...方法中刪除[tableView reloadData];

+0

感謝您的快速回復,移動[tableView reloadData];請求完成處理程序解決了空的tableview問題。但是,我嘗試加載的數據並未顯示。它肯定是成功加載,(能夠NSLog),但我認爲我在將它添加到一個新的NSdictionary時出錯,因爲tableview返回空值。 我並不反對,我應該閱讀和學習,這正是我想要做的,這是我第二天做/看到Objective-C,我很高興人們在這裏有所幫助。 –

+0

好吧,我知道了......找到了將數組輸出到行中的正確方法。 –