UIToolBar與UITableView - 加載時TableView沒有刷新

[英]UIToolBar with UITableView - TableView is not refreshing when loaded

The UIToolBar has two views, a UIView w/UITextViews and a UIView w/UITableView.


The UITableView that gets called via a UIToolBar. The first time the UITableView loads, it hits the DB and gets the info. I "tab" back to the UIView w/UITextViews select some data, and then "tab" back to the UITableView, but none of the delegate methods get called, like viewDidLoad or initWithNibName or anything, thus the data in the table is stale.

通過UIToolBar調用的UITableView。第一次加載UITableView時,它會訪問數據庫並獲取信息。我“選項卡”回到UIView w / UITextViews選擇一些數據,然后“tab”返回UITableView,但沒有調用任何委托方法,如viewDidLoad或initWithNibName或任何東西,因此表中的數據是陳舊的。

So, I found out the method


-(void)viewWillAppear:(BOOL)animated { 

is called, HOWEVER, it doesn't have access to the tableView, like say,


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

I did stumble upon this recommendation:


"I can't seem to figure out how to make the UITableView refresh ... I did find this, which seems like the same problem I'm having,


What I did is to create a MyTableViewController* property in MyModalViewController class. Before presentModalView, I store the table view controller pointer to this property in modal view controller.

我所做的是在MyModalViewController類中創建一個MyTableViewController *屬性。在presentModalView之前,我將表視圖控制器指針存儲在模態視圖控制器中的此屬性中。

Now I have a reference to table view controller when I'm in the modal view controller. Now just use this stored table view controller to locate its tableView, then reloadData.


Feels dirty but gets work done."


If that's the answer, I'm not sure how to code it.


Thanks ahead!

  • sk

Note: I just tried this:



 UITableView *tableViewPtr; 
@property (nonatomic, retain) UITableView *tableViewPtr; 


@synthesize tableViewPtr; 

And store it in -

並將其存儲在 -

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {:
 self.tableViewPtr = tableView; 

Then try to call it from


-(void)viewWillAppear:(BOOL)animated {
 [self.tableViewPtr reloadData]; 

But that doesn't seem to work.


4 个解决方案


mr-sk: You probably need to learn a little more about how Objective-C works. When you declare the UIViewController (or a subclass) you simply implement the UITableView protocols:


@interface ViewWithTableView : UIViewController
    <UITableViewDelegate, UITableViewDataSource> {...}

Then you implement the required protocol methods inside your viewController. There are only a handful of methods you need to implement to support a tableView.


If you're using IB, define your interface like this:


@interface ViewWithTableView : UIViewController
    <UITableViewDelegate, UITableViewDataSource> {
    IBOutlet UITableView *yourTableView;
    . . .

and save it, then go to IB and right-click on the tableView. Click and drag bindings from both DataSource and Delegate to your viewController (likely your File's Owner.) Then click on the the viewController and type cmd-4. Select "ViewWithTableView" or whatever your called it in the Class pulldown in the Class Identity section. Save everything and it'll all work.

並保存它,然后轉到IB並右鍵單擊tableView。單擊並將DataSource和Delegate中的綁定拖動到viewController(可能是您的文件所有者。)然后單擊viewController並鍵入cmd-4。在Class Identity部分的Class下拉菜單中選擇“ViewWithTableView”或您調用的任何內容。保存一切,這一切都會奏效。

Note that unless you add a @property for your tableView, you won't call self because there is no accessor:


[yourTableView whatever:withWhatever];

As with any code typed off the cuff, your mileage may vary. :\

與袖帶上打字的任何代碼一樣,您的里程可能會有所不同。 :\


Thanks for the help. Finally solved and here was the issue:


1) My controller was extending UIViewController, NOT UITableViewController.


Once I extended the correct class, and then wired it correctly via File's Owner and IB, it worked. Now in viewWillAppear I can call self.tableView reloadData.

一旦我擴展了正確的類,然后通過文件的所有者和IB正確連接,它就可以了。現在在viewWillAppear中我可以調用self.tableView reloadData。

Won't ever make that mistake again!


Thanks all -

謝謝大家 -



You can use a UIViewController--you just need to implement the UITableViewDelegate and UITableViewDataSource protocols.

您可以使用UIViewController - 您只需要實現UITableViewDelegate和UITableViewDataSource協議。



in the view will appear method (which i assume is in your controller class) call


[self.tableView reloadData];



粤ICP备14056181号  © 2014-2021 ITdaan.com