2012-02-06 211 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; 
}