ios xcode重新加载tableview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8594606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:59:19  来源:igfitidea点击:

xcode reloading tableview

objective-ciosuitableviewreloaddata

提问by user1110415

In my app I have a tableview being filled by values from an NSMutableArray. This mutable array is being added to in a separate viewcontroller, and so I have to reload the data.

在我的应用程序中,我有一个由 NSMutableArray 中的值填充的 tableview。这个可变数组被添加到一个单独的视图控制器中,所以我必须重新加载数据。

This is probably a very simple question, but I have reading 15+ posts and none of them seem to address xcode 4.2, and where exactly to put the [self.tableView reloadData]. Where exactly should this method be put? When I have tried putting it in various places in the tableviews source code, the tableview initialized for the first time with no values in it.

这可能是一个非常简单的问题,但我已经阅读了 15 多个帖子,但似乎没有一个涉及 xcode 4.2,以及将 [self.tableView reloadData] 放在哪里。这个方法到底应该放在哪里?当我尝试将它放在 tableviews 源代码中的不同位置时,tableview 首次初始化时没有任何值。

Or should it be put in the other viewcontrollers code, in which case how can I reference the particular instance of my UItableview subclass that is being used?

或者它应该放在其他视图控制器代码中,在这种情况下,我如何引用正在使用的 UItableview 子类的特定实例?

Thanks very much in advance.

首先十分感谢。

回答by Jim

There is nothing special about Xcode 4.2 that should make any difference here, so you should pay attention to other sources you have found, even if they refer to other versions of Xcode.

Xcode 4.2 没有什么特别的地方应该在这里有所不同,所以你应该注意你找到的其他来源,即使它们引用了其他版本的 Xcode。

It doesn't have to be [self.tableView reloadData]exactly. You send the table view object the reloadDatamessage. How you access that table view object - through a property on self, through an instance variable, through a property in another class, etc. - is up to you.

不必[self.tableView reloadData]完全如此。您向表视图对象reloadData发送消息。您如何访问该表视图对象 - 通过 self 上的属性、实例变量、另一个类中的属性等 - 取决于您。

If you already have the tableViewproperty set up, then the simplest way of reloading the data from another view controller is to simply send the table view object the reloadDatamessage directly. So, for example, if a method in ViewControllerFoohas a pointer to ViewControllerBarcalled barand knows it should reload its table view, it could call [bar.tableView reloadData].

如果您已经tableView设置了该属性,那么从另一个视图控制器重新加载数据的最简单方法是直接向表视图对象reloadData发送消息。因此,例如,如果一个方法ViewControllerFoo有一个指向ViewControllerBar被调用的指针bar并且知道它应该重新加载它的表视图,它可以调用[bar.tableView reloadData].

If you don't have the property set up, you can create it yourself, or you could create a reloadDatamethod on the view controller holding the table view that does it on behalf of other view controllers.

如果你没有设置属性,你可以自己创建它,或者你可以reloadData在包含表视图的视图控制器上创建一个方法来代表其他视图控制器执行它。

However these approaches mix up logic and presentation, which is usually a pretty poor architecture. If one view controller knows that another view controller should be updating its view, then chances are, you should be factoring out some of that logic to a third class that is independent of any particular view controller. That third class can transmit notifications, or your view controllers can listen for changes to its state via KVO.

然而,这些方法混合了逻辑和表示,这通常是一个非常糟糕的架构。如果一个视图控制器知道另一个视图控制器应该更新它的视图,那么很有可能,您应该将某些逻辑分解为独立于任何特定视图控制器的第三个类。第三个类可以传输通知,或者您的视图控制器可以通过 KVO 监听其状态的变化。

回答by bweberapps

Are you using a navigation controller? Is the view controller that populates the array data pushed ontop of the table view? If so put the reloadData call in viewwillappear because this does get called when a subview of the navigation controller is popped off and the table view is now visible again.

你在使用导航控制器吗?填充数组数据的视图控制器是否推送到表视图之上?如果是这样,将 reloadData 调用放在 viewwillappear 中,因为当导航控制器的子视图弹出并且表视图现在再次可见时,它会被调用。

回答by Yann

You should check if your array is correctly implemented. Try to log it when you enter in the view, for example in the method -viewWillAppear.

您应该检查您的数组是否正确实现。尝试在您进入视图时记录它,例如在方法 -viewWillAppear 中。

Usually you put the [self.tableView reloadData]in the -viewWillAppearmethod.

通常你放[self.tableView reloadData]-viewWillAppear方法。

回答by fishinear

You typically call reloadData in the "viewDidLoad" method of the viewController that is set as the dataSource and delegate of the tableView. But it does not really matter much, just make sure you call reloadData every time after changes to the datamodel (your NSMutableArray).

您通常在设置为 tableView 的数据源和委托的 viewController 的“viewDidLoad”方法中调用 reloadData。但这并不重要,只需确保每次更改数据模型(您的 NSMutableArray)后都调用 reloadData。

Judging from your description, the reloadData is not the likely cause of your problem. Check that you have set your viewController as the dataSource property of the tableView.

从您的描述来看, reloadData 不是您问题的可能原因。检查您是否已将 viewController 设置为 tableView 的 dataSource 属性。

Also, you typically use a data source, as I have assumed here, rather than subclassing the TableView, as you seem to be doing.

此外,您通常使用数据源,正如我在这里假设的那样,而不是像您似乎正在做的那样对 TableView 进行子类化。

回答by IPaPa

after you do [tableView reloadData];

在你做 [tableView reloadData] 之后;

tableView will reload and delegate function to get the table content

tableView 将重新加载和委托函数以获取表格内容

if you [tableView reloadData] clear your table

如果你 [tableView reloadData] 清除你的表格

then check the

然后检查

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

you must read the wrong value there

您必须在那里读取错误的值