Objective-C数组问题

时间:2015-05-01 14:03:13

标签: objective-c arrays

我有这个NSArray,每个项目返回一个POIndex和PONumber,我将数组的每个项目放在另一个数组中,另一个数组返回如下:

POIndex
PONumber
POIndex
PONumber

这是我的代码:

- (void)GetRequest
{

    NSArray *tableData = [dataSource.areaData GetPurchaseOrderItems:[NSString stringWithFormat:@"%@%@",areaPickerSelectionString,unitPickerSelectionString]];

    if(!self.objects){
        self.objects = [[NSMutableArray alloc]init];
    }

    for(int i = 0; i < [tableData count]; i++){
        [self.objects addObjectsFromArray:[tableData objectAtIndex:i]];
    }

    [self.tableView reloadData];

}

以下是tableData返回的屏幕截图:

enter image description here

我要做的是将PONumber作为键,POIndex作为显示值,我该怎么做?

我尝试了以下内容:

NSMutableDictionary *subDict=[[NSMutableDictionary alloc]init];
    for (NSDictionary *dict in tableData) {
        [subDict setValue:[dict objectForKey:@"POIndex"] forKey:[dict objectForKey:@"PONumber"]];
        [self.objects addObject:subDict];
    }

但显示如下:

{
{
{
{

以下是显示它的内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSString *object = self.objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

1 个答案:

答案 0 :(得分:1)

您的tableData数组在每个索引处包含NSDictionary。因此,您可以在cellForRowIndexPath方法中使用相同的数组,如下所示,

@property(nonatomic, retain) NSArray * tableData;

- (void)GetRequest
{
   self.tableData = [dataSource.areaData GetPurchaseOrderItems:[NSString stringWithFormat:@"%@%@",areaPickerSelectionString,unitPickerSelectionString]];

   [self.yourTableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

   NSString *strPOIndex = [self.tableData[indexPath.row] valueForKey:@"POIndex"];
   cell.textLabel.text = strPOIndex;
   return cell;
}