从 sqlite 数据库中检索数据并将其显示在分组表视图上

问题描述:

大家好

我是 obj c 的新手,我做了一个示例项目,在第一个屏幕上有 2 个屏幕,我有六个文本字段 &2 个名为 save 和 ViewData 的按钮,单击文本字段中输入的保存数据将保存在 sqliteData Base 中,&单击按钮 ViewData 它将导航到一个具有分组表视图的新屏幕,在这里我试图显示存储在 sqlite 中的数据,在分组表视图中我有 6 个部分,我正在使用以下代码在分组表视图中显示数据,问题是分组表视图仅显示在文本字段中输入的最后一个数据,但我需要显示输入的所有数据应显示在该部分下

i am brand new to obj c, i have did a sample project where i have 2 screens on the first screen i have six text fields & 2 buttons named save and ViewData ,on click of save data which is entere d in the text field will be get saved in the sqliteData Base ,& on click of the button ViewData it will navigate to a new screen which has a grouped table view, here i am trying to display the data which is stored in the sqlite ,in the grouped table view i have 6 sections i am using following code to display the data in grouped table view,problem is grouped table view is displaing only the last data which is entered ih the text field,,but i need to display all the data which enterd should be shown under that section

appDelegate = (iICS_testAppDelegate *)[[UIApplication sharedApplication] delegate];

    for(int intVar=0;intVar < [appDelegate.arrObjects count];intVar++)
    {
    insertUpdateDelete *InsertRecord = [appDelegate.arrObjects objectAtIndex:intVar];

    NSLog(@"InsertRecord:%@",InsertRecord);




    NSMutableArray *arrTemp1 = [[NSMutableArray alloc]initWithObjects:InsertRecord.strsixth,nil];
    NSMutableArray *arrTemp2 = [[NSMutableArray alloc]initWithObjects:InsertRecord.strfifth,nil];
    NSMutableArray *arrTemp3 = [[NSMutableArray alloc]initWithObjects:InsertRecord.strFourth,nil];

    NSMutableArray *arrTemp4 = [[NSMutableArray alloc]initWithObjects:InsertRecord.strLogin,nil];
    NSMutableArray *arrTemp5 = [[NSMutableArray alloc]initWithObjects:InsertRecord.strMiddleName,nil];
    NSMutableArray *arrTemp6 = [[NSMutableArray alloc]initWithObjects:InsertRecord.strFirstName,nil];


    NSMutableDictionary *temp =[[NSMutableDictionary alloc]initWithObjectsAndKeys:arrTemp1,@"Item Name",arrTemp2,@"Manufacturer",arrTemp3,@"Weight of Item",arrTemp4,@"Num of Item",arrTemp5,@"Price of Item",arrTemp6,@"MFG Date",nil];

    self.tableContents =temp;
    [temp release];
    NSLog(@"table %@",self.tableContents);
    NSLog(@"table with Keys %@",[self.tableContents allKeys]);
    self.sortedKeys =[[self.tableContents allKeys] sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"sorted %@",self.sortedKeys);



    [arrTemp1 release];
    [arrTemp2 release];
    [arrTemp3 release];

    [arrTemp4 release];
    [arrTemp5 release];
    [arrTemp6 release];
    }

在这里我为行分配文本

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //insertUpdateDelete *InsertRecord = [appDelegate.arrObjects objectAtIndex:indexPath.row];
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];


    UITableViewCell * cell = [tableView 
                              dequeueReusableCellWithIdentifier:SimpleTableIdentifier];


    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:SimpleTableIdentifier] autorelease];
    }

    NSUInteger row = [indexPath row];

    //cell.textLabel.text = [appDelegate.arrObjects objectAtIndex:row];
    cell.textLabel.text = [listData objectAtIndex:row];




    return cell;
}

提前致谢!

这个是我的分组表格视图的快照.我需要通过文本字段输入的数据应该在特定部分下查看

This is my snapshopt of grouped table view. I need the data which is entered through the text field shoul be viewed under particular section

只显示最后一个数据的解决方案,

使用 NSMutableArray 代替 NSArray.

字段值错误的解决方法,

您的问题可能出在插入本身,

Your problem may be in insertion itself,

 NSArray *arrTemp1 = [[NSArray alloc]initWithObjects:InsertRecord.strsixth,nil];
 NSArray *arrTemp2 = [[NSArray alloc]initWithObjects:InsertRecord.strfifth,nil];

您正在将价格值插入日期字段,我认为是这样.请检查一下.

You are inserting price value into date field, i think so. Please check that.

将您的代码更改为,

NSMutableArray *arrTemp1 = [[NSMutableArray alloc]init];
NSMutableArray *arrTemp2 = [[NSMutableArray alloc]init];
NSMutableArray *arrTemp3 = [[NSMutableArray alloc]init];
NSMutableArray *arrTemp4 = [[NSMutableArray alloc]init];
NSMutableArray *arrTemp5 = [[NSMutableArray alloc]init];
NSMutableArray *arrTemp6 = [[NSMutableArray alloc]init];

for(int intVar=0;intVar < [appDelegate.arrObjects count];intVar++)
    {
    insertUpdateDelete *InsertRecord = [appDelegate.arrObjects objectAtIndex:intVar];

    NSLog(@"InsertRecord:%@",InsertRecord);

    [arrTemp1 addObject:InsertRecord.strsixth];
    [arrTemp2 addObject:InsertRecord.strfifth];
    [arrTemp3 addObject:InsertRecord.strFourth];
    [arrTemp4 addObject:InsertRecord.strLogin];
    [arrTemp5 addObject:InsertRecord.strMiddleName];
    [arrTemp6 addObject:InsertRecord.strMiddleName];
    }
 NSDictionary *temp =[[NSDictionary alloc]initWithObjectsAndKeys:arrTemp1,@"Item Name",arrTemp2,@"Manufacturer",arrTemp3,@"Weight of Item",arrTemp4,@"Num of Item",arrTemp5,@"Price of Item",arrTemp6,@"MFG Date",nil];

    self.tableContents =temp;
    [temp release];
    NSLog(@"table %@",self.tableContents);
    NSLog(@"table with Keys %@",[self.tableContents allKeys]);
    self.sortedKeys =[[self.tableContents allKeys] sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"sorted %@",self.sortedKeys);

    [arrTemp1 release];
    [arrTemp2 release];
    [arrTemp3 release];
    [arrTemp4 release];
    [arrTemp5 release];
    [arrTemp6 release];