How does a UITableView show two different arrays one at a time?

The code below works, but not the way I want. I want the UIbutton to automatically update the new value in the UITableview instead of the old value when clicked. The code only works when I press the UI buttons and after that, when I look at the UITableview then it updates the UItableview with the new values. In my application, I use UITableview as a subclass of my main classclass.as image below

enter image description here

I am adding a Tableview to my Mainclass, which is a "testingViewController" this way In testingViewController.h

#import "Inputtableview.h" @interface testingViewController :UIViewController<UITableViewDelegate,UITableViewDataSource> { Inputtableview *inputview; IBOutlet UITableView *inputtbl; } @end 

In testViewController.m

 - (void)viewDidLoad { btn1bool=FALSE; if (inputview == nil) { inputview = [[Inputtableview alloc] init]; } [inputtbl setDataSource:inputview]; [inputtbl setDelegate:inputview]; inputview.view = inputview.tableView; } 

Now in the button action method

 -(IBAction)input:(id)sender { btn1bool=TRUE; } 

my subclass code "inputtableview.m" is shown below

 - (void)viewDidLoad { [super viewDidLoad]; listOfItems=[[NSMutableArray alloc] initWithObjects:@"Iceland",@"Greenland",@"Switzerland", @"Norway",@"New Zealand",@"Greece",@"Italy",@"Ireland",nil]; array1 = [[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H", nil] ; } #pragma mark - #pragma mark Table View datasource methods -(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { if (btn1bool) { return [array1 count]; } else { return [listOfItems count]; } [self.tableView reloadData]; } -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } NSLog(@"Row: %i", indexPath.row); if (btn1bool) { NSString *cellValue = [array1 objectAtIndex:indexPath.row]; cell.text = cellValue; } else { NSString *cellValue = [listOfItems objectAtIndex:indexPath.row]; cell.text = cellValue; } return cell; } 

Any help would be indicated.

+4
source share
1 answer

Just enter the following code:

 [inputtbl reloadData]; 

There are a few things you need to change in your project, but I assume that this project is for testing only.


You want the date to reload after clicking the button, so you call the method in IBAction.

 -(IBAction)input:(id)sender { btn1bool=TRUE; [inputview.tableView reloadData]; } 

To switch between two data sources at the click of a button, you can go to this line of code: btn1bool=!btn1bool;

 (NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { if (btn1bool) { return [array1 count]; } else { return [listOfItems count]; } } 

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

+1
source

Source: https://habr.com/ru/post/1445107/


All Articles