iOS how to acces [tableView reloadData] from another class [closed]

本小妞迷上赌 提交于 2019-12-25 13:16:05

问题


I have an viewController, which has a property:UIScrollView, and same controller contains a tableView. My UIScrollView contains buttons. How can i use [tableView reloadData] from touching some of this buttons? tableView data source and delegate is on viewController.

MyViewController:

@property (strong, nonatomic) MyScrollView* scrollView; 
@property (strong, nonatomic) IBOutlet UITableView *table; 

MyScrollView class:

no property and 5 buttons created programatically.


回答1:


After better understanding what you're trying to do I think I would use delegates. Although there are other ways to do this I think that delegates are flexible and if you haven't used them yet then this is a good time to learn about them.

We define the delegate methods that a class needs to implement if it wishes to adopt our protocol: MyScrollView.h

@protocol MyScrollViewDelegate <NSObject>
-(void)reloadTableData;
@end

@interface MyScrollView : UIScrollView
@property (nonatomic,weak) id<MyScrollViewDelegate> delegate;
@end

Then in the implementation we do the following:

MyScrollView.m

// Set the button's target
[button addTarget:self action:@selector(reloadData) forControlEvents:UIControlEventTouchUpInside];

// Here we check if our delegate responds to the reloadTableData selector and if so we call it
-(void)reloadData
{
    if ([self.delegate respondsToSelector:@selector(reloadTableData)])
    {
        [self.delegate reloadTableData];
    }
}

In the implementation of MyViewController we add the MyScrollViewDelegate and implement the reloadTableData method:

MyViewController.m

@interface MyViewController () <MyScrollViewDelegate>
@property (strong, nonatomic) MyScrollView* scrollView; 
@property (strong, nonatomic) IBOutlet UITableView *table; 
@end

// Set the delegate after you've created scrollView
self.scrollView.delegate = self;

// Once the delegate is called we simply reload the table data
-(void)reloadTableData
{
    [self.table reloadData]
}



回答2:


If your view controller contains both the table view and the scroll view with a number of buttons, you can simply connect your table view to an IBOutlet that's also declared as a property and then call "reloadData" on that table view property.

And if the table view is in a different view controller or class from the object that contains the buttons, you just need to have some kind of pointer to the separate view controller which contains the table view. And if that table view is declared a property, you can still reload data by doing something like "[theOtherViewController.tableView reloadData];" (assuming the property is named tableView and the pointer to the other VC is named theOtherViewController).




回答3:


Please do following way, to reload table data

.h File

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
    IBOutlet UITableView *exTable1;
    IBOutlet UITableView *exTable2;
}
@property (nonatomic, retain) UITableView *exTable1;
@property (nonatomic, retain) UITableView *exTable2;

Give relation to .xib

.m File

@synthesize exTable1;
@synthesize exTable2;

// reload data when you need 
[self.exTable1 reloadData];

// reload data when you need 
[self.exTable2 reloadData];



回答4:


This is simple like this -

viewController.m

// set up an action for your button
-(IBAction)yourButtonClick{

   [yourtablename reloadData];

   }


来源:https://stackoverflow.com/questions/21730025/ios-how-to-acces-tableview-reloaddata-from-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!