Two Tableviews in one Controller

ios, uitableview, xcode

Solution

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
}

Problem

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

Original source