我有一个类车辆,它是一个NSOBject。它有4个字符串变量vrn,make,model,yearmade。我通过Web服务从数据中提取Vehicle对象,并将每个对象存储在nsmutablearray中。如何从每个存储对象的可变数组中访问变量vrn,make,model,yearmade? p>
编辑: 我试过这个:
[[myArray objectAtIndex:indexPath.row] objectAtIndex:0];
编辑2:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"test");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
if ([tableView isEqual:vrnTable]) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.opaque = NO;
vrnLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0, 7.0, 50.0, 30.0)];
vrnLabel.font = [UIFont systemFontOfSize:12.0];
vrnLabel.textAlignment = UITextAlignmentRight;
vrnLabel.textColor = [UIColor whiteColor];
vrnLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
vrnLabel.backgroundColor = [UIColor clearColor];
vrnLabel.tag = VRN_LABEL_TAG;
[cell.contentView addSubview:vrnLabel];
makeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 7.0, 10.0, 30.0)];
makeLabel.font = [UIFont systemFontOfSize:12.0];
makeLabel.textAlignment = UITextAlignmentRight;
makeLabel.textColor = [UIColor whiteColor];
makeLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
makeLabel.backgroundColor = [UIColor clearColor];
makeLabel.tag = MAKE_LABEL_TAG;
[cell.contentView addSubview:makeLabel];
modelLabel = [[UILabel alloc] initWithFrame:CGRectMake(200.0, 7.0, 10.0, 30.0)];
modelLabel.font = [UIFont systemFontOfSize:12.0];
modelLabel.textAlignment = UITextAlignmentRight;
modelLabel.textColor = [UIColor whiteColor];
modelLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
modelLabel.backgroundColor = [UIColor clearColor];
modelLabel.tag = MODEL_LABEL_TAG;
[cell.contentView addSubview:modelLabel];
yearLabel = [[UILabel alloc] initWithFrame:CGRectMake(120.0, 7.0, 10.0, 30.0)];
yearLabel.font = [UIFont systemFontOfSize:12.0];
yearLabel.textAlignment = UITextAlignmentRight;
yearLabel.textColor = [UIColor whiteColor];
yearLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
yearLabel.backgroundColor = [UIColor clearColor];
yearLabel.tag = YEAR_LABEL_TAG;
[cell.contentView addSubview:yearLabel];
}
else {
vrnLabel = (UILabel *)[cell.contentView viewWithTag:VRN_LABEL_TAG];
}
}
NSString *cell_image = [NSString alloc];
if (indexPath.row != 0) {
cell_image = [NSString stringWithFormat:@"color%d.png", indexPath.row % 8 + 1];
}
else {
cell_image = [NSString stringWithFormat:@"color%d.png", 0];
}
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:cell_image] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
vrnLabel.text = ((Vehicle *)[entries objectAtIndex:indexPath.row]).vrn;
return cell;
}
答案 0 :(得分:1)
简单一点,只需使用objectAtIndex并使用类型转换来访问对象变量:
NSString *firstObjVrn = ((Vehicle*)[vehicleArray objectAtIndex:indexPath.row]).vrn;
NSString *firstObjMake = ((Vehicle*)[vehicleArray objectAtIndex:indexPath.row]).make;
NSString *firstObjModel = ((Vehicle*)[vehicleArray objectAtIndex:indexPath.row]).model;
NSString *firstObjYearMade = ((Vehicle*)[vehicleArray objectAtIndex:indexPath.row]).yearMade;