如何从数组中获取特定值并在tableview中显示

时间:2013-02-19 10:54:59

标签: iphone nsarray

我有数组并保存在arraypartno中的所有值,我将该特定值放在标签中,并希望在我的单元格中显示为PARTNUMBER: -

这是代码: -

 for (int i =0 ; i<[arrData count]; i++)


   {    
        [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"Condition"]];
        [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"DateCode"]];
        [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"MFG"]];
        [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"PARTNUMBER"]];
        [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"Qty"]];
        [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"id"]];
    }

所以我必须在lblplate1.text中为特定键编写,现在它显示整个数组值。

UILabel *lblplate1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 200, 30)];

lblplate1.text = [arraypartno  objectAtIndex:indexPath.row];
lblplate1.textColor = [UIColor colorWithRed:2.0/255.0 green:143.0/255.0 blue:213.0/255.0 alpha:1];

lblplate1.backgroundColor =[UIColor clearColor];
[cell.contentView addSubview:lblplate1];

2 个答案:

答案 0 :(得分:1)

如果您正在为客户端应用程序工作,请尝试采用MVC。通过以条件,DateCode,MFG,Partno等属性的模型对象形式存储数据,并将数据存储在模型对象数组中。

for (int icounter=0; icounter<[arrData count]; icounter++) {
                    if (objModalClass!=nil) {
                        [objModalClass release];
                        objModalClass=nil;
                    }
                    objModalClass=[[ModalClass alloc]init];
                    objModalClass.Condition=[[arrData objectAtIndex:i] valueForKey:@"Condition"];
                    objModalClass.DateCode=[[arrData objectAtIndex:i] valueForKey:@"DateCode"];;
                    objModalClass.partno=[[arrData objectAtIndex:i] valueForKey:@"PARTNUMBER"];
                    [arrList addObject:objModalClass];
                }
}

从模型类对象中检索数据

for (ModalClass *obj in arrList){
           lblCondition.text=obj.Condition;
}

尝试使用MVC,因为您可以轻松更改结构,以防必须添加新属性或删除它们。

答案 1 :(得分:0)

不是将不同的键值添加到同一个数组,而是为不同的键创建不同的数组

喜欢这个

    [arrayCondition addObject:[[arrData objectAtIndex:i] valueForKey:@"Condition"]];
    [arrayDateCode addObject:[[arrData objectAtIndex:i] valueForKey:@"DateCode"]];
    [arrayMFG addObject:[[arrData objectAtIndex:i] valueForKey:@"MFG"]];
    [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"PARTNUMBER"]];
    [arrayQty addObject:[[arrData objectAtIndex:i] valueForKey:@"Qty"]];
    [arrayID addObject:[[arrData objectAtIndex:i] valueForKey:@"id"]];

在cellforRowAtIndexPath

lblplate1.text = [arraypartno  objectAtIndex:indexPath.row];

注意: - 请在viewDidLoad中初始化数组。