从iphone中的sqlite数据库更新表视图数据

时间:2013-01-27 07:46:44

标签: iphone ios sqlite uitableview

我在sqlite数据库中插入数据,在第二个选项卡中我将插入数据显示到分组表视图中,但是我插入的最后一个数据没有显示在表视图中,尽管当我再次运行应用程序时所有数据都是如何显示,如何在表格视图中显示所有插入数据?

此代码用于从费用表中选择名称和金额

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    NSLog(@"sec%i",section);
    NSLog(@"row%i",row);
    NSMutableArray *NAMeA= [[NSMutableArray alloc]init ];
    NSMutableArray *DateA= [NSMutableArray array];
    if (sqlite3_open([[AppDelegate getDBPath] UTF8String], &database) == SQLITE_OK) {
        const char *sql ="select ExpenseMonth,count(*) as day from Expense  where strftime('%m',ExpenseMonth)=strftime('%m','now') group by ExpenseMonth order by ExpenseMonth desc";

        sqlite3_stmt *selectstmt;
        int result = sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL);
        if( result== SQLITE_OK) {
            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                NSString *dateAndTime=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
                NSInteger numofday=sqlite3_column_int(selectstmt,1);

                MaxEntity *DatTime=[[MaxEntity alloc]initWithDate:dateAndTime andnumofDay:numofday];                
                [DateA addObject:DatTime];
                [NAMeA removeAllObjects];
                NSLog(@"Date %@",dateAndTime);
                NSString *querySQL =[NSString stringWithFormat:@"select Name, amount from Expense  where ExpenseMonth=\"%@\" order by ExpenseMonth desc",dateAndTime];
                const char *query_stmt = [querySQL UTF8String];
                sqlite3_stmt *selectstm = NULL;
                int result = sqlite3_prepare_v2(database, query_stmt, -1, &selectstm, NULL);
                if( result== SQLITE_OK) {
                    while(sqlite3_step(selectstm) == SQLITE_ROW) {
                        NSString *NAMe=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstm, 0)];
                        NSString *Amount=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstm, 1)];
                        MaxEntity *NM=[[MaxEntity alloc]initWithName:NAMe andAmount:Amount];
                        [NAMeA addObject:NM];
                        NSLog(@"text%@",NAMe);

                        if ([indexPath section]+1 ==[DateA count] && [indexPath row]+1==[NAMeA count]) {
                            cell.textLabel.text =NM.name;
                            cell.detailTextLabel.text = NM.amountD;
                            NSLog(@"%@ name",NM.name);

                        }
                    }
                }
                sqlite3_finalize(selectstm);
            }
        }else {
            NSLog(@"failed to prepare %d",result);
        }
    }
    else{

        NSLog(@"Faile to open db");
        sqlite3_close(database);
       [self.table reloadData];

    }
    self.NameArray =NAMeA ;

    self.DateArray = DateA;
       [self.table reloadData];

    return cell;
}

2 个答案:

答案 0 :(得分:0)

您需要在执行UITableView Oparation后重新加载insert/delete/update

[self.YourTableView reloadData];

答案 1 :(得分:0)

正如评论中所说..

在选项卡出现时更新de table,添加第二个选项卡的实现文件:

- (void)viewWillAppear:(BOOL)animated{

       [super viewWillAppear:animated];
       NSLog(@"updateTable");

     [self.table reloadData];
    //or - depend of your your conf
    //[self.tableView reloadData];

}