Search code examples
arraysobjective-cuitableview

How can I access data of object stored in an array?


I would like to get data of an object stored in an array. But I don't know how.

Here's the class type of my object:

#import <Foundation/Foundation.h>
    
@interface Account : NSObject
    
@property(strong, nonatomic) NSString *accountNumber;
@property(strong, nonatomic) NSString *accountName;
    
@end
Account *model = [[Account alloc]init];

for(int i=0; i<NUMBER_OF_CELL; i++){
    [model setAccountName:[NSString stringWithFormat:@"Account %d",i]];
    [model setAccountNumber:[NSString stringWithFormat:@"Number %d",i]];

    [_accountArray addObject:model];
}

I list this in a UITableView:

cell.accountLabel.text = ?

Solution

  • You are doing it wrong. Account Object is created only once. So that you are see only one object.

    Use below code:

    for(int i=0; i<NUMBER_OF_CELL; i++){
    
        Account *model = [[Account alloc]init];
    
        [model setAccountName:[NSString stringWithFormat:@"Account %d",i]];
        [model setAccountNumber:[NSString stringWithFormat:@"Number %d",i]];
    
        [_accountArray addObject:model];
    
    }
    

    and to display data use below code :

    Account *model = [_accountArray objectatIndex:indexpath.row];
    cell.accountLabel.text = model.accountName;