2012-02-06 196 views
1

我想从数据库插入数据到表。 我能够从数据库获取数据并将其插入表中,但只有最后一个数据插入到表的所有行中。我已经使用了代码从数据库插入数据到表

-(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; 
} 

这里PrepopulatedActionsDao就是我从数据库中获取所有提交的数据的类。

我想插入数据库的所有数据在表中不仅是最后一个。 请有人帮忙。

+0

是什么的UITableView类的numberOfRowsInSection价值? – Sarah 2012-02-06 08:43:01

+0

@Sarah:它的100作为数据库中的数据是100. – hgpl 2012-02-06 08:44:37

回答

1

不需要for循环。对于tableview中的每一行都调用cellforrow方法。所以你只需要把下面的代码放在cell.textLabel.text = [NSString stringWithFormat: @"%@",[[allPreActions objectAtIndex:indexPath.row] action];而不是for循环中。

希望这可以解决您的问题。

1

cellForRowAtIndexPath将针对每行调用,因此您需要在每次调用中提供正确的数据。因此,而不是个“for循环”,你可以做

actionInformation=[allPreActions objectAtIndex:[indexPath row]]; 
cell.textLabel.text = [NSString stringWithFormat: @"%@",actionInformation.action]; 

您可能还需要缓存allPreActions而不是每个呼叫加油吧的。

+0

Luis:非常感谢。这工作。 – hgpl 2012-02-06 08:50:36

0

每次都得到分配。所以在外面宣布。

PrepopulatedActionsDao * preAction = [[PrepopulatedActionsDao alloc] init]; 

[preAction getDataFromDatabase]; 
    NSMutableArray *allPreActions=[preAction getDataFromDatabase]; 

而且你不需要的for循环,而不是做以这种方式:

actionInformation=[allPreActions objectAtIndex:indexpath.row]; 
    cell.textLabel.text = [NSString stringWithFormat: @"%@",actionInformation.action]; 
+0

在allPreActions = [preAction getDataFromDatabase];后保留NSMutableArray; – Sarah 2012-02-06 08:56:38

0

中声明.h文件中(声明为一个类成员)NSMutableArray成员allPreActions和填充它viewDidLoadcellForRowAtIndexPath:

之前别的地方那么这段代码会在viewDidLoad

PrepopulatedActionsDao * preAction = [[PrepopulatedActionsDao alloc] init]; 

if(allPreActions) 
{ 
    [allPreActions releaseAllObjects]; 
    [allPreActions release]; 
} 

allPreActions = [[preAction getDataFromDatabase] retain]; 
[preAction release]; 

而且你cellForRowAtIndexPath:会像下面

-(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; 
}