开发者

How do I use a UIButton Action to change the NSArray that is used to populate a UITableView?

I am making a new app that has four sets of data that are very similar. I want to make four UIButtons to navigate to one UITabBar with four UITableViews. I don't want to make four buttons that push to four sets of tablevie开发者_Python百科ws. I figure making an app with 13 UIViews would make it bigger than it needs to be. So, I have to figure out how to make the UIButton action change the data the tables receive. I am working on this project actively. Any suggestions would be helpful. Thank you.


In order to change a table views data source you need to change what is returned from numberOfRowsInSection on your table delegate. One way I have done this is to use an enum to represent which set of data I want to use and have a local variable to save which one is selected

typedef enum
{
    DataSetOne,
    DataSetTwo
} DataSetEnum

@property(nonatomic, retain) DataSetEnum dataset;
@property(nonatomic, retain) NSArray *datasetone;
@property(nonatomic, retain) NSArray *datasettwo;

- (void) viewDidLoad
{
     dataset = DataSetOne;
}
- (IBAction) buttonPressed
{
     dataset = DataSetTwo;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     if(dataset == DataSetOne)
     {
          return [self.datasetone count];
     }
     else if(dataset == DataSetTwo)
     {
          return [self.datasettwo count];
     }
}

And then in cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     if(dataset == DataSetOne)
     {
           //configure cell
     }
}


If your data is similar you could just set a data from each button's IBAction variable:

NSArray* data = ...

Then in your button method:

-(IBAction*)clickedButton1:(id)sender {
    data = [self getButton1Data]
    [self reloadAll]
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜