【问题标题】:update the table view data from sqlite database in iphone从 iphone 中的 sqlite 数据库更新表视图数据
【发布时间】:2013-01-30 14:30:26
【问题描述】:

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

【问题讨论】:

  • 从表中选择名称和金额 费用取决于日期以在我插入到第一个选项卡的第二个选项卡的分组表视图中显示此数据。我应该在哪里使用 reloadData?
  • 我不明白你放在这里的...请用在 ` ` 之间写入的代码进行编辑
  • 在第一个选项卡中,更新后,您必须从第二个选项卡重新加载表格,否则您必须在单击第二个选项卡时重新加载表格。
  • 我调用了 [self.tableview reloadData];但是在 sqlite 中输入的新数据没有加载到表格视图中
  • 您正在重新加载表格.. 但您是否也在更新数据源? (NameArray 和.. DateArray)?

标签: iphone ios sqlite uitableview


【解决方案1】:

正如 cmets 中已经说过的那样..

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

- (void)viewWillAppear:(BOOL)animated{

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

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

}

【讨论】:

    【解决方案2】:

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

    [self.YourTableView reloadData];
    

    【讨论】:

    • 感谢 iPatel,当我使用此方法时,表格视图中不显示任何内容,仅显示 titleForHeaderInSection
    • numberOfRowsInSection 和 numberOfSectionsInTableView 没有更新。如何更新这个数组计数?
    • @FadiaJaradat- 将您的代码发送到我的电子邮件...我会发回给您解决您的问题 :) 谢谢 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 2017-11-17
    • 1970-01-01
    • 2011-10-05
    相关资源
    最近更新 更多