【问题标题】:Passing data with array but shows as empty in table view delegate使用数组传递数据,但在表视图委托中显示为空
【发布时间】: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


【解决方案1】:

您需要强制分配您的数组,尽管它是类级别的对象。在 viewdidload 中分配 allFilteredItemsArray 并将其写为 self.allFilteredItemsArray。检查以下代码:

-(void)viewDidLoad {

[超级viewDidLoad];

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

self.allFilteredItemsArray=tempArray;

[tempArray 释放];

NSLog(@"viewdidload");

[自配置视图];

}

您也可以使用 %d 进行数组计数,如下所示:

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

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

返回 [allFilteredItemsArray 数量];

}

【讨论】:

    【解决方案2】:

    您应该将 allFilteredItemsArray 作为 self.allFilteredItemsArray 访问,这可能会解决问题;-)

    【讨论】:

    • 我把self.allFilteredItemsArray 放在NSLog(@"numberOfRowsInSection: %i", allFilteredItemsArray.count) 里面。好像没用。
    • 应该是 NSLog(@"numberOfRowsInSection: %i",[self.allFilteredItemsArray count]);
    • 在使用 performSelector: afterDelay: 或在收到响应数据后调用 refreshTable 方法意味着您应该在 sendData 方法中调用 refreshTable 而不是在 configureView 中调用。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多