0

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.

Gad
  • 2,867
  • 25
  • 35
user3258772
  • 103
  • 5
  • 12

4 Answers4

3

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]
}
Gad
  • 2,867
  • 25
  • 35
  • selector is recognized in all project? because this button is in another class and [self.tableView reloadData] must be in viewC – user3258772 Feb 12 '14 at 14:26
  • 1
    No, the selector will be recognized only if it's a method of `self`. You can however pass a pointer to the view controller which contains the table view instead of `self`. I'll update my answer. – Gad Feb 12 '14 at 14:32
  • Ill tell you in another way: i have @property...*scrollView. In this class you can find buttons + [addTarget] for every of this button. I dont understand how can i exit from this property and do something on the class which contains this property (myVC) ? – user3258772 Feb 12 '14 at 14:43
  • Could you update your question with the structure of your classes so it'll be easier to help you? – Gad Feb 12 '14 at 14:46
  • One different from my proj: my ScrollView is subclass of UIScrollView not NSObject, so whats the difference in first group of your line ? – user3258772 Feb 12 '14 at 17:22
  • @user3258772 you just need to change `NSObject` to `UIScrollView`. I updated my answer. – Gad Feb 12 '14 at 19:15
0

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).

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

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];
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
0

This is simple like this -

viewController.m

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

   [yourtablename reloadData];

   }
  • Man i dont understand. I have a subclass of UIScrollView: "scrollView" MyVc: @property .... scrollView, this is programatacally made, and i have a table from interface builder. – user3258772 Feb 12 '14 at 14:34
  • So what is the issue then ?? –  Feb 12 '14 at 14:53