【发布时间】:2012-10-17 02:58:24
【问题描述】:
我的应用基于 UISplitViewController,它是通用应用。
我的应用中有这段代码,它可以从网络服务器查找数据。
结果显示计数为 100,但 NSMutableArray 在 [myTable reloadData] 之后是 nil。
我该如何解决?
场景
Page1:一个搜索引擎,将值传递给另一个视图
Page2:从 [Page1] 接收值,然后传递给实例 sendData。通过 JSON 获取结果,然后在 viewdidload 之后将它们转换为 NSMutableArray', using thisNSMutableArrayto populateUITableView'
代码:
- (void)configureView
{
if (self.detailItem) {
NSLog(@"ConfigureView");
[self sendData];
[self refreshTable];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"viewdidload");
[self configureView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) refreshTable
{
NSLog(@"Refresh Table");
[myTable reloadData];
NSLog(@"Counter:%i",allFilteredItemsArray.count);
}
#pragma mark - Table
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"numberOfRowsInSection: %i", allFilteredItemsArray.count);
return [allFilteredItemsArray count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"fileredCell";
UITableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:CellIdentifier];
HardwareCell *hardwareCell = [[HardwareCell alloc] init];
// Configure the cell...
NSDictionary *item = [allFilteredItemsArray objectAtIndex:[indexPath row]];
hardwareCell.lbl_model = [item objectForKey:@"name"];
return cell;
}
#pragma mark - Searching Asset
-(void)sendData
{
NSString *searchString = [self.detailItem description];
.........ignored some of the code............
allFilteredItemsArray = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(@"Search Count: %i", allFilteredItemsArray.count);
NSLog(@"End Searching Data.");
}
结果:
2012-10-17 10:45:54.535 Portal[10753:c07] viewdidload
2012-10-17 10:45:54.536 Portal[10753:c07] Value to be sent: %/%/%/%/%/1
2012-10-17 10:45:54.537 Portal[10753:c07] ConfigureView
2012-10-17 10:45:54.537 Portal[10753:c07] Start Seraching Data...
2012-10-17 10:45:54.923 Portal[10753:c07] Search Count: 100
2012-10-17 10:45:54.923 Portal[10753:c07] End Searching Data.
2012-10-17 10:45:54.923 Portal[10753:c07] Refresh Table
2012-10-17 10:45:54.924 Portal[10753:c07] Counter:100
2012-10-17 10:45:54.924 Portal[10753:c07] numberOfRowsInSection: 0
【问题讨论】:
-
我们在这里缺少关键信息。
allFilteredItemsArray在哪里声明,在哪里实例化? -
@property (nonatomic, retain) NSMutableArray *allFilteredItemsArray;被声明,@synthesize在 .h 文件中,初始化/分配它viewDidLoad在 .m 文件中。 -
ARC/非 ARC?您引用了声明的属性
allFilteredItemsArray,而在实现中您没有使用属性self.allFilteredItemsArray -
应该是 ARC。 XCode 默认,我没有更改任何偏好。那我应该怎么做最好呢?
标签: objective-c ios uitableview ios4 nsarray