0

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 = ?
0

3 Answers 3

2
Account *model = [_accountArray objectAtIndex:[indexPath row]];
cell.accountLabel.text = model. setAccountName;
Sign up to request clarification or add additional context in comments.

Comments

1

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;

5 Comments

I tried this way but I have only the last value of my array. The initializing of my array is in the method: viewDidLoad.
where you alloc _accountArray?
it should be look like this : NSMutableArray *_accountArray = [[NSMutableArray 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]; }
Can you please +1 answer? Thanks :)
I have no reputation enough :'( At least 15 ans I have only 3 ^^
0

Can be acheived using the following code

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.