从不同的类重新加载数据?

发布于 2024-10-21 06:28:51 字数 1617 浏览 5 评论 0原文

我正在尝试从不同的类重新加载 tableView。在控制台中进行一些 NSLogs 后,我的数据被更改但没有更改。下面是一些代码:

SecondViewController.m

RootViewController *test = [[RootViewController alloc] init];
// Set up database connection
NSString *myDB = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"flashpics.db"];
database = [[Sqlite alloc] init];
[database open:myDB];

[database executeNonQuery:@"INSERT INTO albums (id, name, connections) VALUES (?,?,'No Connections')", theID, theName];
[test.listOfItems addObject:theName];


RootViewController *controller = [[RootViewController alloc] init];
[controller.tableView reloadData];

[self dismissModalViewControllerAnimated:YES];

你能帮我解决可能是最简单的问题之一吗?

谢谢, Coulton

编辑#1: NSLogs 显示以下内容。这能有什么用吗? __NSAutoreleaseNoPool(): UITableView 类的对象 0x5883000 自动释放,没有池 - 只是泄漏

编辑 #2: 向 SecondViewController.m 添加更多代码

编辑 #3 :由于某种原因它不起作用。

SecondViewController.m

RootViewController *controller = [[RootViewController alloc] init];
[RootViewController refreshTableView];

RootViewController.m

- (void) refreshTableView {
[self.tableView reloadData];
NSLog(@"Refreshing TableView - %@", listOfItems);
}

我在数组中插入了“neato”。然后我在SecondViewController.m中调用reloadTableView到RootViewController.m。我确实得到了输出,但它没有更新 tableView。下面是 NSLog。

Refreshing TableView - (
"Default Album",
"Hello.",
"This is cool!",
hi,
neato
)

PS 我没有得到结果,但我确实得到了我通过 NSLog 编写的内容。

I am trying to reload a tableView from a different class. After some NSLogs in the console, my data is being altered but not changed. Below is some code:

SecondViewController.m

RootViewController *test = [[RootViewController alloc] init];
// Set up database connection
NSString *myDB = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"flashpics.db"];
database = [[Sqlite alloc] init];
[database open:myDB];

[database executeNonQuery:@"INSERT INTO albums (id, name, connections) VALUES (?,?,'No Connections')", theID, theName];
[test.listOfItems addObject:theName];


RootViewController *controller = [[RootViewController alloc] init];
[controller.tableView reloadData];

[self dismissModalViewControllerAnimated:YES];

Can you please help me solve probably one of the easiest questions that exists?

Thanks,
Coulton

EDIT#1: NSLogs display the following. Could this do with anything?
__NSAutoreleaseNoPool(): Object 0x5883000 of class UITableView autoreleased with no pool in place - just leaking

EDIT #2: Added more code to the SecondViewController.m

Edit #3: For some reason it's not working.

SecondViewController.m

RootViewController *controller = [[RootViewController alloc] init];
[RootViewController refreshTableView];

RootViewController.m

- (void) refreshTableView {
[self.tableView reloadData];
NSLog(@"Refreshing TableView - %@", listOfItems);
}

I Inserted 'neato' in the array. Then I called reloadTableView in the SecondViewController.m to RootViewController.m. I did get an output, but it didn't update the tableView. Below is the NSLog.

Refreshing TableView - (
"Default Album",
"Hello.",
"This is cool!",
hi,
neato
)

P.S. I don't get results, but I do get stuff I programmed it to say through a NSLog.

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

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

发布评论

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

评论(2

江湖彼岸 2024-10-28 06:28:51

您初始化 RootViewController 两次。我想象的太多了两倍。您需要找到对视图控制器的引用并将该项目添加到其 listOfItems 中。然后在该控制器的 UITableView 上调用 reloadData

You initialize your RootViewController twice. Twice too many I imagine. You need to find a reference to your view controller and add the item to it's listOfItems. Then call reloadData on that controller's UITableView.

永言不败 2024-10-28 06:28:51

问题是为什么?你真正想做什么?

你的代码没有多大意义,你正在创建一个 RootViewController 的新实例,并且你正在调用在 tableView 显示一些数据之前重新加载数据。在tableview有数据之前不需要重新加载。但我认为您想要引用旧实例而不是刚刚创建的新实例

如果我认为这是一个基于导航的项目,其中在 tableView:didSelectRowAtIndexPath: 中创建 SecondView,然后推送到导航控制器,您应该尝试在 RootViewController 的 - (void)viewWillAppear:(BOOL)animated 中调用 [self.tableView reloadData];

但我只是猜测(虽然我很擅长这一点),因为你的代码没有意义。此阶段不需要重新加载tableview,因为tableview没有任何数据。

the question is why? or what do you really want to do?

Your code doesn't make much sense, you are creating a new instance of RootViewController, and you are calling reloadData before the tableView has showed you some data. No need to reload before the tableview has data. But I think you want to reference the old instance instead of the new one you've just created

If this is what I think it is, a Navigation Based project where SecondView gets created in tableView:didSelectRowAtIndexPath:, and then pushed to the navigationcontroller you should try to call [self.tableView reloadData]; in - (void)viewWillAppear:(BOOL)animated of the RootViewController

But I'm just guessing (although I'm pretty good at this) because your code makes no sense. No need to reload the tableview at this stage, because the tableview doesn't have any data.

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