使用数组传递数据但在表视图委托中显示为空

时间:2012-10-17 02:58:24

标签: objective-c ios uitableview ios4 nsarray

我的应用基于UISplitViewController,它是一款通用应用 我在我的应用程序中有这个代码,它可以查找来自webserver的数据。

结果显示计数为100,但NSMutableArraynil之后为[myTable reloadData]

我该如何解决?

方案 第1页:搜索引擎,将值传递给另一个视图 第2页:从[Page1]接收值,然后传递给实例sendData。获得JSON的结果,然后在NSMutableArray', using this

之后将它们转换为to populate NSMutableArray viewdidload UITableView'

代码:

- (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

3 个答案:

答案 0 :(得分:1)

你需要强制分配你的数组,虽然它是类级别的对象。在viewdidload&中Alloc allFilteredItemsArray将其写为self.allFilteredItemsArray。请检查以下代码:

- (无效)viewDidLoad中 {

[super viewDidLoad];

NSMutableArray * tempArray = [[NSMutableArray alloc] init];

self.allFilteredItemsArray = tempArray;

[tempArray release];

的NSLog(@ “viewDidLoad中”);

[self configureView];

}

此外,您可以将%d用于数组计数,如下所示:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSLog(@“numberOfRowsInSection:%d”,[allFilteredItemsArray count]);

return [allFilteredItemsArray count];

}

答案 1 :(得分:0)

如果allFilteredItemsArray是您的子类的属性,则应将其称为[self allFilteredItemsArray]

答案 2 :(得分:0)

您应该将allFilteredItemsArray作为self.allFilteredItemsArray访问,以解决问题; - )