1

我在 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;
}
4

2 回答 2

0

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

[self.YourTableView reloadData];
于 2013-01-27T07:48:42.407 回答
0

正如评论中已经说过的..

要在选项卡出现时更新表格,请添加第二个选项卡的实现文件:

- (void)viewWillAppear:(BOOL)animated{

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

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

}
于 2013-01-29T12:10:43.593 回答