Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two Tableviews in one Controller

So I'm trying to make two tableviews in one view and I'm having some trouble. I've read some other response on how to do it but they don't exactly help me.

In my .h file I made two outlets 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 out seperate values in each different controller and I'm not too sure since you only return 1 cell?

So far i've Done this

- (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;

This prints "I love Jazzy" in my first TableView and nothing gets printed in the second one. Why does this happen and how can I fix it? Thanks :D

like image 690
Jaspreet Chauhan Avatar asked Dec 12 '22 20:12

Jaspreet Chauhan


2 Answers

This method gets called by all tableViews that have an instance of your class set as their dataSource.

This means you need to check which tableView was asking for cell number so-and-so.

So, your method should basically 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
}
like image 187
fzwo Avatar answered Dec 14 '22 08:12

fzwo


My suggestion would be to set a tag on both of your table views and then in your tableview dataSource methods check for which the tag the table view your are in. So for instance some where in your code you can do:

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

later when your 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";
 }

Also if you have 2 tableviews with different sizes you can achieve this like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
    if (tableView.tag == 1)
         return 1;
    else if (tableView.tag == 2)
         return 2;
  }
like image 39
Yuliani Noriega Avatar answered Dec 14 '22 10:12

Yuliani Noriega