0

我有一个类 Vehicle,它是一个 NSOBject。它有 4 个字符串变量 vrn、make、model、yearmade。我从通过 Web 服务获得的数据中创建 Vehicle 对象,并将每个对象存储在 nsmutablearray 中。如何从每个存储对象的可变数组中访问变量 vrn、make、model、yearmade?

编辑:我试过这个:

[[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;
}
4

1 回答 1

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;
于 2013-10-22T09:03:21.920 回答