Two kinds of tables in one controller

So, I'm trying to make two kinds of tables in one view, and I have problems. I read another answer on how to do this, but they do not help me.

In my .h file, I made two exits for two views, calling them myFirstViewText and mySecondViewTex

So in my m files for - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

I want to be able to print individual values ​​in each individual controller, and I'm not sure because you only return 1 cell?

So far i have done it

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Rx"; static NSString *CellIdentifier2 = @"allergies"; UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier]; UITableViewCell *cell2 = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier2]; if(!cell){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } if (!cell2) { cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]; } if (tableView == self.myFirstTextView ) { cell.textLabel.text = @"HI JAZZY";//[RxDict objectAtIndex:indexPath.row]; } if (tableView == self.mySecondTextView) { cell.textLabel.text = @"BYE JAZZY";//[RxDict objectAtIndex:indexPath.row]; } tableView = self.mySecondTextView; cell2.textLabel.text = @"I love Jazzy :D"; return cell2; 

It prints β€œI Love Jazzy” in my first TableView, and nothing prints in the second. Why is this happening and how can I fix it? Thanks: D

+4
source share
4 answers

This method is called by all tables that have an instance of your set of classes as a data source.

This means that you need to check which tableView requested the cell number so-and-so.

So your method should look like this:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == rxTableView) { //prepare and return the appropriate cell for *this* tableView } else if (tableView == allergiesTableView) { //prepare and return the appropriate cell for *this* tableView } return nil; //because you don't expect any other tableView to call this method } 
+7
source

My suggestion was to set the tag in both table views and then in the dataSource methods in tableview to check which tag the table you are in is used for. So for example, somewhere in your code you can do:

 myFirstTableView.tag = 1; mySecondTableView.tag = 2; 

later when you are in

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView.tag == 1) cell.textLabel.text = @"HI JAZZY";//[RxDict objectAtIndex:indexPath.row]; if (tableView.tag == 2) cell.textLabel.text = @"BYE JAZZY"; } 

In addition, if you have 2 tables with different sizes, you can achieve this as follows:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView.tag == 1) return 1; else if (tableView.tag == 2) return 2; } 
+4
source

One approach that I don't see too often is to use a subclass of NSObject to act as a delegate and data source for each table, and not for the view controller. This eliminates the need for if if code (tableView == someTable) ... and can make your view controller and UITableView code convenient and understandable.

You will subclass NSObject for each table. These subclasses will contain the desired implementation of your UITableView data and delegation methods. Then you create an instance of one of them in your view controller and attach it to the corresponding table.

+1
source

Problem 1:

 tableView = self.mySecondTextView; cell2.textLabel.text = @"I love Jazzy :D"; return cell2; 

What you do here always returns cell2 , setting its text β€œI love jazzy: D”, so it will always populate the first view of the table, because you would not set tableView=mySecondTextView before entering the method. The delegate always targets the firstTableView .

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

Problem 2:

 if (tableView == self.myFirstTextView ) { cell.textLabel.text = @"HI JAZZY"; //[RxDict objectAtIndex:indexPath.row]; //for printing in first table view return cell here like return cell; } if (tableView == self.mySecondTextView) { cell.textLabel.text = @"BYE JAZZY"; //[RxDict objectAtIndex:indexPath.row]; //for printing in second tableview return cell two here return cell2; } 

Make sure that the table for which you want to target is set before entering this function. You can set it in the numberofsection method, the numberofsection view didload or in another place where you want to display or display the table view (for example, clicking on a button). But you cannot install it here.

+1
source

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


All Articles