Reputation: 869
I want to insert data from database to table. I am able to get data from database and insert that into table,but only the last data is inserted in all rows of table. I have used the code as
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
Action *actionInformation;
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 460) reuseIdentifier:@"MyIdentifier"] autorelease];
PrepopulatedActionsDao * preAction = [[PrepopulatedActionsDao alloc] init];
[preAction getDataFromDatabase];
NSMutableArray *allPreActions=[preAction getDataFromDatabase];
for(int i=0;i<[allPreActions count];i++){
actionInformation=[allPreActions objectAtIndex:i];
cell.textLabel.text = [NSString stringWithFormat: @"%@",actionInformation.action];
}
}
return cell;
}
Here PrepopulatedActionsDao is the class where I am getting all datas from database.
I want to insert all datas of database in table not only the last one. please anybody help.
Upvotes: 1
Views: 170
Reputation: 1302
cellForRowAtIndexPath will be called for each row, so you need to provide the correct data in each call. So, instead of the "for loop", you can do
actionInformation=[allPreActions objectAtIndex:[indexPath row]];
cell.textLabel.text = [NSString stringWithFormat: @"%@",actionInformation.action];
You may also want to cache allPreActions instead of filling it in each call.
Upvotes: 1
Reputation: 17478
Declare NSMutableArray
member allPreActions
in .h file (Declare it as a class member) and populate it in viewDidLoad
or somewhere else before cellForRowAtIndexPath:
So this code will come in viewDidLoad
PrepopulatedActionsDao * preAction = [[PrepopulatedActionsDao alloc] init];
if( allPreActions )
{
[allPreActions releaseAllObjects];
[allPreActions release];
}
allPreActions = [[preAction getDataFromDatabase] retain];
[preAction release];
And your cellForRowAtIndexPath:
will look like as follows
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
Action *actionInformation;
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 460) reuseIdentifier:@"MyIdentifier"] autorelease];
}
actionInformation=[allPreActions objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat: @"%@",actionInformation.action];
return cell;
}
Upvotes: 0
Reputation: 2649
This gets allocated every time. So declare it outside.
PrepopulatedActionsDao * preAction = [[PrepopulatedActionsDao alloc] init];
[preAction getDataFromDatabase];
NSMutableArray *allPreActions=[preAction getDataFromDatabase];
Also you don't need for loop, Instead do it in this manner :
actionInformation=[allPreActions objectAtIndex:indexpath.row];
cell.textLabel.text = [NSString stringWithFormat: @"%@",actionInformation.action];
Upvotes: 0
Reputation: 5098
There is no need of the for loop. The cellforrow
method is called for every row in the tableview. So u just need to put the following line cell.textLabel.text = [NSString stringWithFormat: @"%@",[[allPreActions objectAtIndex:indexPath.row] action];
instead of the for loop.
Hope this solves your problem.
Upvotes: 1