2013-11-20 104 views
0

我已經做了的UIViewController用的TableView內從服務器我的代碼顯示的東西是這樣的:的iOS TableViewController不顯示

- (void) viewDidLoad{ 
    [super viewDidLoad]; 
    NSUserDefaults *userDefaults = [UserDefaults instance]; 
    [Async activitiesForPersonId:[userDefaults objectForKey:USERDEFAULTS_PERSONID] 
        unionId:[userDefaults objectForKey:USERDEFAULTS_UNIONID] 
        callback:^(NSArray *activities){ 
         if(activities && [activities count] != 0){ 
          NSLog(@"%hhd",[NSThread isMainThread]); //1 
          NSLog(@"%i", [activities count]); //16 
          self.activities = activities; 
         }else{ 
          NSLog(@"What the ...."); //Doesn't get printed 
         } 
        }]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [self.activities count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *identifier = @"CalendarActivity"; 
    CalendarCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
    return cell; 
} 

爲什麼沒有顯示在屏幕上。當我將[self.activities count]更改爲硬編碼5時,它顯示5個單元格。所以我想我已經通過IB正確設置了數據源。此外,數據來自另一個線程,但將其返回到主線程,以便可能不是問題。

回答

1

你實際上沒有在單元格中設置任何東西。例如:

- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    // Set up cell stuff 
    cell.textLabel.text = @"Title For Cell"; 

    return cell; 
} 

編輯:

你可能需要重新加載的tableView一旦數據已經能夠顯示,[tableView reloadData];應該爲你做這個。

+0

我知道,但ATLEAST它應該顯示我的電池 – Haagenti

+0

哦佈局,在這種情況下,當你的數據返回調用'[的tableView reloadData];' – NinjaLikesCheez

+0

你是在開玩笑我哈哈哈哈。固定,讓我們把問題叫到電腦後面幾個小時;)謝謝,8分鐘內會接受 – Haagenti

1

你必須重新綁定在執行回調的實現代碼如下,當你異步函數完成:

- (void) viewDidLoad{ 
    [super viewDidLoad]; 
    NSUserDefaults *userDefaults = [UserDefaults instance]; 
    [Async activitiesForPersonId:[userDefaults objectForKey:USERDEFAULTS_PERSONID] 
        unionId:[userDefaults objectForKey:USERDEFAULTS_UNIONID] 
        callback:^(NSArray *activities){ 
         if(activities && [activities count] != 0){ 
          NSLog(@"%hhd",[NSThread isMainThread]); //1 
          NSLog(@"%i", [activities count]); //16 
          self.activities = activities; 

          //add this line: 
          [self.tableView reloadData]; 

         }else{ 
          NSLog(@"What the ...."); //Doesn't get printed 
         } 
        }]; 
} 
+0

謝謝,你的回答是正確的 – Haagenti

1

而且數據來自另一個線程,但可能是這」不是個返回它在主線程這個問題也是。

其實,這是你的問題。如果你添加了一些斷點,我相信你會看到numberOfRowsInSection在你的異步回調處理器之前被調用。在異步回調塊的末尾重新加載主線程中的tableView數據,您應該沒問題。

+0

謝謝我沒有意識到這一點 – Haagenti

1

你必須調用[tableView reloadData]; 在viewDidLoad中更改代碼:

- (void) viewDidLoad{ 
     [super viewDidLoad]; 
     NSUserDefaults *userDefaults = [UserDefaults instance]; 
     [Async activitiesForPersonId:[userDefaults objectForKey:USERDEFAULTS_PERSONID] 
         unionId:[userDefaults objectForKey:USERDEFAULTS_UNIONID] 
         callback:^(NSArray *activities){ 
          if(activities && [activities count] != 0){ 
           NSLog(@"%hhd",[NSThread isMainThread]); //1 
           NSLog(@"%i", [activities count]); //16 
           self.activities = activities; 
           // If your metod doesn't run on main thread call it on main tread: 
           dispatch_async(dispatch_get_main_queue(), ^{ 
            [tableView reloadData]; 
           }); 
           //Otherwise call just [tableView reloadData]; 
          }else{ 
           NSLog(@"What the ...."); //Doesn't get printed 
          } 
         }]; 
    } 
相關問題