2013-04-03 115 views
2

我仍然是一个obj-C,但我不清楚,我正在创建一个应用程序,当我添加项目到tableview时需要刷新一个tableview。我GOOGLE了一下,发现一些答案,说我需要使用[self.tableView reloadData],但它不适合我。加载视图后无法更新UITableView

这里是我的代码为我的tableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *) tableView { 
    return 1; } 

- (NSString *) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section { 
    NSString *myTitle = [[NSString alloc] initWithFormat:@"Runs"]; 
    return myTitle; 
     } 

- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section { 

    return [entries count]; 
    } 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
forIndexPath:indexPath]; 

    // Set up the cell... 
    NSString *cellValue = [entries objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; 

    return cell; 
} 

这里是我填充表

- (void)populateTable{ 
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    NSString *username = appDelegate.username; 
    entries = [[NSMutableArray alloc]init ]; 
    [self openDB]; 
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Savings WHERE username = '%@'",username]; 
    sqlite3_stmt *statement; 

    if(sqlite3_prepare_v2(runnerAppDB, [sql UTF8String], -1, &statement, nil)==SQLITE_OK) 
    { 
     while(sqlite3_step(statement)==SQLITE_ROW) 
     { 

      char *field1 = (char *) sqlite3_column_text(statement, 0); 
      NSString *field1Str = [[NSString alloc] initWithUTF8String:field1]; 

      // username - distance - speed - date - comment 
      NSString *str = [[NSString alloc] initWithFormat:@"%@ %@ %@ - %@", field1Str]; 
      [entries addObject:str]; 

     } 
    } } 

我已经尝试做的是调用populateTable功能每次我添加一些代码到数据库,但仍然不刷新,请有人可以帮我吗?一段时间以来一直困扰着这个问题。

感谢

+0

您的代码不会创建一个单元格开始,使用调试器检查它返回的内容,我认为它将为零。 – Tim

+0

迈克尔,在开始为你编写代码之前,你应该先掌握iOS开发。 – samfisher

回答

1

试试吧....

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:YES]; 
    [self.myTableView reloadData]; 
} 
+0

@Michael:发生了什么告诉我.... –

+0

它在“self.myTableView”上给出错误。在我输入“self”后,我甚至没有任何有关我的tableView的自动完成功能。你知道为什么吗 ?我不知道它是什么错,因为我可以在tableView中的值,我只是不能更新新的。谢谢 – Michael

+0

IBOutlet UITableView * myTableView;声明为.h文件 现在将其设置为.xib文件 现在[myTableView reloadData];工作 –

0

您需要重新加载你的表之后的所有数据是你的阵列从中你感觉你的表所示。 并在tableview的cellforrowatindexpath方法中需要重用该单元格。请更新您的cellforrow方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *cellIdentifier = @"ArticleCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

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

    NSString *cellValue = [entries objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; 

    return cell; 

} 
+0

我试过了,但不起作用 – Michael

+0

你确定你的条目数组从数据库获取所有数据? 或者得到另一种获取数据的方式,并确保使用NSLog更新数据。获取所有数据后,添加[urTableView reloadData]; 这是标准的实施方式。 – iCoder

+0

我可以在我的tableView中看到值,所以我想我可以访问数据库中的数据..我的问题是,我添加了一些新的数据库后,我无法刷新我的viewTable – Michael

0

一旦表的数据源得到改变,那么只有你需要重新加载表。只要检查一次数据源是否受到影响就重新加载表视图。喜欢这个。

- (void)populateTable{ 
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    NSString *username = appDelegate.username; 
    entries = [[NSMutableArray alloc]init ]; 
    [self openDB]; 
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Savings WHERE username = '%@'",username]; 
    sqlite3_stmt *statement; 

    if(sqlite3_prepare_v2(runnerAppDB, [sql UTF8String], -1, &statement, nil)==SQLITE_OK) 
    { 
     while(sqlite3_step(statement)==SQLITE_ROW) 
     { 

      char *field1 = (char *) sqlite3_column_text(statement, 0); 
      NSString *field1Str = [[NSString alloc] initWithUTF8String:field1]; 

      // username - distance - speed - date - comment 
      NSString *str = [[NSString alloc] initWithFormat:@"%@ %@ %@ - %@", field1Str]; 
      [entries addObject:str]; 

     } 
    } 
    [self.tableView reloadData] 
} 
+0

我有这个问题是,它不刷新。我的应用程序有3个选项卡,第二个是与tableView的。它只保存我第一次点击标签的状态。所以如果我添加一些东西,并在第二个选项卡上,值将被添加,但如果我再次添加它将保留在'旧'版本。 – Michael