我无法从NSArray传输数据以填充我的表格视图。我环顾四周,试了几件不同的东西,但到目前为止还没有运气。
这是我的代码:
#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 =@"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text=[_allFilms objectAtIndex:indexPath.row];
return cell;
}
我从Azure中提取数组数据并将其传输到数组。现在我想要它,以便每个单元格与该数组不同。关于如何做到这一点的任何想法?
答案 0 :(得分:1)
在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];
}
}];
这将导致表格视图重新查询数据源方法numberOfSections
,numberOfRowsInSection:
和tableView:cellForRowAtIndexPath:
,并在表格视图中显示新内容。
您可能还想添加某种消息或进度指示器,以便用户知道应用程序正在从远程服务器获取数据。您可以在启动异步请求时显示消息/进度指示器,然后在调用[myTableView reloadData]
之前在完成块中将其删除。
答案 1 :(得分: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);
}
}];
希望这有帮助!