1

ビューコントローラーを備えたテーブルビューがあり、このテーブルに NSMutableArray からのデータを入力しようとしています。

私が示すように、いくつかのプロパティ(.mファイルに@synthesizeを含む)を持つLugar.mおよびLugar.hというクラスがあります。

@property (nonatomic, strong) NSString *nombre;
@property (nonatomic, strong) NSString *direccion;
@property (nonatomic, strong) NSString *descripcion;
@property (nonatomic, strong) NSString *precio;

次に、次のviewDidLoadメソッドを使用してテーブルビューのコントローラーを持っています

- (void)viewDidLoad
{
[super viewDidLoad];
self.title =@"Comer";
Lugar *lugar = [[Lugar alloc] init];
self.datosTabla = [[NSMutableArray alloc] initWithCapacity:6];
NSArray *nombres = [[NSArray alloc] initWithObjects:@"Masala",@"Italiano",@"Gaia",@"Pekaditos",@"Ojeda",@"Plan B", nil];
NSArray *direcciones = [[NSArray alloc] initWithObjects:@"C/San Roque, 5", @"C/ Moneda, 7 ",@"C/Nueva, 6", @"C/San Roque, 5", @"C/ Moneda, 7 ",@"C/Nueva, 6", nil];
NSArray *descripciones = [[NSArray alloc] initWithObjects:@"Bonito restaurante ecologico",@"Restaurante fino y caro", @"El mejor marisco de burgos",@"Calida recio mud", @"Calidad insuperable" , @"Calidad insuperable",nil];
NSArray *precios = [[NSArray alloc] initWithObjects:@"25€", @"15€", @"12€", @"6€", @"34€", @"34€",nil];
for (NSUInteger i = 0; i < 6; i++) {
lugar.nombre = [nombres objectAtIndex:i];
lugar.direccion = [direcciones objectAtIndex:i];
lugar.descripcion = [descripciones objectAtIndex:i];
lugar.precio = [precios objectAtIndex:i];
[self.datosTabla insertObject:lugar atIndex:i];
}

このファイルには cellForRowAtIndex もあります

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    Lugar *tempName =[self.datosTabla objectAtIndex:indexPath.row];
    cell.textLabel.text = tempName.nombre;
    }
    return cell;
    }

行とセクションを返すメソッドもあります

アプリは正常に実行されますが、mutableArray に追加された最後のオブジェクトの名前だけが表示されます。この場合、レストランは Plan B と呼ばれます。同じレストラン情報を持つ 6 行のテーブルを取得しました。

私はそれを修正しようとしましたが、結果が得られずに何時間も費やしました。どんな助けにも感謝します。前もってありがとうルイス

PD: アレイの情報はスペイン語です。申し訳ありません

4

1 に答える 1

1

datosTabla同じオブジェクトを配列に 5 回挿入しています。for ループの反復ごとにオブジェクトのプロパティを変更しても、lugarオブジェクトを指すポインターlugarは変更されません。

lugar5 つの異なるオブジェクトが存在するように、for ループ内にオブジェクトを割り当ててください。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title =@"Comer";
    self.datosTabla = [[NSMutableArray alloc] initWithCapacity:6];
    NSArray *nombres = [[NSArray alloc] initWithObjects:@"Masala",@"Italiano",@"Gaia",@"Pekaditos",@"Ojeda",@"Plan B", nil];
    NSArray *direcciones = [[NSArray alloc] initWithObjects:@"C/San Roque, 5", @"C/ Moneda, 7 ",@"C/Nueva, 6", @"C/San Roque, 5", @"C/ Moneda, 7 ",@"C/Nueva, 6", nil];
    NSArray *descripciones = [[NSArray alloc] initWithObjects:@"Bonito restaurante ecologico",@"Restaurante fino y caro", @"El mejor marisco de burgos",@"Calida recio mud", @"Calidad insuperable" , @"Calidad insuperable",nil];
    NSArray *precios = [[NSArray alloc] initWithObjects:@"25€", @"15€", @"12€", @"6€", @"34€", @"34€",nil];
    for (NSUInteger i = 0; i < 6; i++) {
        Lugar *lugar = [[Lugar alloc] init];
        lugar.nombre = [nombres objectAtIndex:i];
        lugar.direccion = [direcciones objectAtIndex:i];
        lugar.descripcion = [descripciones objectAtIndex:i];
        lugar.precio = [precios objectAtIndex:i];
        [self.datosTabla insertObject:lugar atIndex:i];
    }
}

cell == nil の場合だけでなく、そのメソッドが呼び出されるたびにセルのテキスト フィールドにデータを入力する必要があります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Lugar *tempName =[self.datosTabla objectAtIndex:indexPath.row];
    cell.textLabel.text = tempName.nombre;
}
于 2012-10-17T17:42:02.617 に答える