从 appDelegate 调用数据时重新加载 tableview 源

发布于 2024-09-27 22:19:13 字数 1434 浏览 3 评论 0原文

我有一个基于表的应用程序,将数据存储在 mysql 服务器上,它会更新并将数据写入 nsdictionary 以实现持久性。当您更改数据时,表需要更新。但是,如果我放置 [self tableview reloadData],则应用程序在选择行时会崩溃。有谁知道如何使表刷新发生。我是否需要放弃这个并从数据中创建一个对象并使用它?

谢谢。

  -(void) loadData3;{

    MyAppDelegate *AppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource3 = [AppDelegate.data3 objectForKey:@"Rows"];
NSLog(@"AppDelegate.data3 : %@",AppDelegate.data3 );
NSLog(@"self.tableDataSource3 : %@",self.tableDataSource3 );

}

- (void)viewDidLoad {
    [super viewDidLoad]; 
    [self loadData3];
    if(CurrentLevel3 == 0) {
    self.navigationItem.title = @"Family";
}
else 
    self.navigationItem.title = CurrentTitle3;  
}
}


- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

 }

这是 cellForRowAtIndexPath 方法。

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    NSDictionary *dictionary = [self.tableDataSource3 objectAtIndex:indexPath.row];
    cell.textLabel.text = [dictionary objectForKey:@"Title"];

    return cell;
}

I have a table based app that stores data on a mysql server, it gets updated and writes the data to nsdictionary for persistance. When you make a change to the data, the table need to update. However if I put a [self tableview reloadData] the app crashes when selecting a row. Does anyone have an Idea on how to make the table refresh happen. Do I need to ditch this and make an object from the data and use that?

Thanks.

  -(void) loadData3;{

    MyAppDelegate *AppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource3 = [AppDelegate.data3 objectForKey:@"Rows"];
NSLog(@"AppDelegate.data3 : %@",AppDelegate.data3 );
NSLog(@"self.tableDataSource3 : %@",self.tableDataSource3 );

}

- (void)viewDidLoad {
    [super viewDidLoad]; 
    [self loadData3];
    if(CurrentLevel3 == 0) {
    self.navigationItem.title = @"Family";
}
else 
    self.navigationItem.title = CurrentTitle3;  
}
}


- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

 }

Here is the cellForRowAtIndexPath method.

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    NSDictionary *dictionary = [self.tableDataSource3 objectAtIndex:indexPath.row];
    cell.textLabel.text = [dictionary objectForKey:@"Title"];

    return cell;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-10-04 22:19:13

我同意 reloadData 是正确方法的解释,您需要找到并修复导致崩溃的错误。

这里有两个重要的事情需要注意:

  1. 确保在更改数据库后,对 numberOfRowsInSection 的调用将立即返回有效的新值作为表被修改的结果。 (我看到很多人因此而发布问题)。

  2. 仔细查看您的 cellForRowAtIndexPath 方法。请记住,此方法不仅设置单元格中的数据,还负责回收单元格。我见过很多处理不当的情况 - 当表数据随后重新加载时 - 并且需要回收和重新分配单元格 - 事情就会变得混乱。我敢打赌 99% 的问题都出在此处。

I concur with the explanation that reloadData is the correct way to go, and you need to find and fix the bug that is causing your crash.

Here are two giant things to look at:

  1. Make sure immediately after altering the database, the call to numberOfRowsInSection will return the valid, new value as a result of the table being modified. (I have seen many people post issues due to this).

  2. Look carefully at your cellForRowAtIndexPath method. Remember, this method doesn't only set the data in the cells, but is also responsible for recycling cells. I have seen many instances where this was mishandled - and when table data is subsequently reloaded - and the cells need to be reclaimed and reassigned - things go haywire. I'd bet 99% that your issue is in here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文