从不同的类重新加载数据?
我正在尝试从不同的类重新加载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您初始化 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.问题是
为什么?
或你真正想做什么?
你的代码没有多大意义,你正在创建一个 RootViewController 的新实例,并且你正在调用在 tableView 显示一些数据之前重新加载数据。在tableview有数据之前不需要重新加载。但我认为您想要引用旧实例而不是刚刚创建的新实例
如果我认为这是一个基于导航的项目,其中在
tableView:didSelectRowAtIndexPath:
中创建 SecondView,然后推送到导航控制器,您应该尝试在 RootViewController 的- (void)viewWillAppear:(BOOL)animated
中调用[self.tableView reloadData];
但我只是猜测(虽然我很擅长这一点),因为你的代码没有意义。此阶段不需要重新加载tableview,因为tableview没有任何数据。
the question is
why?
orwhat 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 RootViewControllerBut 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.