2014-05-07 101 views
1

我無法從我的NSArray傳輸數據來填充我的表格視圖。我環顧四周,嘗試了一些不同的東西,但目前還沒有運氣。把NSArray放入TableView

這裏是我的代碼:

#import "ListFilmController.h" 
#import <WindowsAzureMobileServices/WindowsAzureMobileServices.h> 
#import <QuartzCore/QuartzCore.h> 
#import "AppDelegate.h" 

@interface ListFilmController() 


@end 

@implementation ListFilmController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
self = [super initWithStyle:style]; 
if (self) { 
     // Custom initialization 
} 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    MSClient *client = [(AppDelegate *) [[UIApplication sharedApplication] delegate]  client]; 

MSTable *itemTable = [client tableWithName:@"filmbuff"]; 

[itemTable readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error) { 

    if (error) { 

     NSLog(@"Error: %@", error); 
    } 
    else { 
     _allFilms = items; 
     NSLog(@"Item inserted, array: %@", items); 
    } 

}]; 

// Uncomment the following line to preserve selection between presentations. 
// self.clearsSelectionOnViewWillAppear = NO; 

// Uncomment the following line to display an Edit button in the navigation bar for this  view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

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

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
#warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1; 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return [_allFilms count];; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier [email protected]"Cell"; 
    UITableViewCell *cell = [tableView 
          dequeueReusableCellWithIdentifier:CellIdentifier  forIndexPath:indexPath]; 

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

    return cell; 
} 

我從Azure中拉陣列中的數據並將其傳送到一個數組。現在我需要它,以便每個單元格與該陣列不同。任何想法如何做到這一點?

+0

您確定'_allFilms'數組正在填充數據嗎? – Stonz2

+1

'readWithCompletion'是一個異步任務,你的tableview可能在你有數據之前加載 –

+0

你可以嘗試在「readWithCompletion」行之前初始化你的_allFilms數組。之後,在您將「items」分配給「_allFilms」的完成塊中,您應該添加諸如「[_myTable reloadData];」之類的內容。在這一點上,你的_allFils數組已經被填充。 – Alex

回答

1

Simon在評論中有答案。我將重新發布它作爲答案。 (@SimonMcLoughlin,你應該真的這樣做,所以你可以得到接受的答案功勞)。

在您的viewDidLoad方法中,您正在觸發一個異步請求。該請求將在稍後的一段時間才能完成。

與此同時,您的應用程序正在顯示錶格視圖。表格視圖調用各種數據源方法,並被告知沒有要顯示的單元格。

你想要做的是一個[myTableView reloadData]呼叫添加到您的結束塊的結束:

[itemTable readWithCompletion: 
^(NSArray *items, NSInteger totalCount, NSError *error) { 

    if (error) { 

     NSLog(@"Error: %@", error); 
    } 
    else { 
     _allFilms = items; 
     NSLog(@"Item inserted, array: %@", items); 
     [myTableView reloadData]; 
    } 
}]; 

這將導致表視圖,以重新查詢數據源的方法numberOfSectionsnumberOfRowsInSection:tableView:cellForRowAtIndexPath: ,並在表格視圖中顯示新內容。

您可能還想添加某種消息或進度指示器,以便用戶知道應用程序正在從遠程服務器獲取數據。在啓動異步請求時,您會顯示消息/進度指示器,然後在調用[myTableView reloadData]之前將其在完成塊中刪除。

0

我不確定是否理解您的問題。我想我會這樣做:

#import "ListFilmController.h" 
#import <WindowsAzureMobileServices/WindowsAzureMobileServices.h> 
#import <QuartzCore/QuartzCore.h> 
#import "AppDelegate.h" 

@interface ListFilmController() 


@end 

@implementation ListFilmController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
self = [super initWithStyle:style]; 
if (self) { 
     // Custom initialization 
} 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    MSClient *client = [(AppDelegate *) [[UIApplication sharedApplication] delegate]   client]; 

MSTable *itemTable = [client tableWithName:@"filmbuff"]; 

// Your app will not crash if it tries to populate the tableview before you get the data 
_allFilms = [NSArray new]; 

[itemTable readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error) { 

if (error) { 

    NSLog(@"Error: %@", error); 
} 
else { 
    _allFilms = items; 
    // After populating your data array you will have to reload the table view data 
    [_tableView reloadData]; 
    NSLog(@"Item inserted, array: %@", items); 
} 

}]; 

希望這有助於!

+0

謝謝,不得不改變一些東西,但這工作。它在表視圖後加載數組。 – dmbll