Anand -
Anand -

Reputation: 291

how to load two tableview in same view controller with cell for index row?

i am facing one module consists to load two different UITableView in same UIViewController i know where i doing mistake, the problem is cell for row AtIndexpath in table view method. i getting only one UITableView in UIViewController but my secondviewcontroller not return any values in cell.

here my sample code for ur reference:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView==table) {
     return [self.arryData count];
}
else
return [tblArr count];
NSLog(@"tabeCount==>%lu",(unsigned long)tblArr.count);

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *CellIdentifier  = @"Cell";
static NSString *CellIdentifier1 = @"Cell";

       dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *cell;
mobilePlanDetailsCellTableViewCell *plan_cell;

if (tableView==table) {
    cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];


}return cell;

if (tableView==planTable) {

    plan_cell =  [self.planTable dequeueReusableCellWithIdentifier:CellIdentifier1];
    if (plan_cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"mobilePlanDetailsCellTableViewCell" owner:self options:nil];
        plan_cell = [nib objectAtIndex:0];
    }

    plan_cell.label.text = [[tblArr objectAtIndex:indexPath.row] objectForKey:@"Answer1"];
    plan_cell.Label2.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:@"answer2"];
    plan_cell.Label3.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:@"answer3"];
    plan_cell.label4.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:@"answer4"];

 }
    return plan_cell;
}
  - (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView setBackgroundColor:[UIColor whiteColor]];
[cell setBackgroundColor:[UIColor whiteColor]];

 }

Upvotes: 0

Views: 1379

Answers (3)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

you made the small mistake just change the Return Cell in inside the method

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


 static NSString *CellIdentifier  = @"Cell";
if (tableView==table) {


UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];

return cell;

}
else {



  mobilePlanDetailsCellTableViewCell *plan_cell =  [self.planTable dequeueReusableCellWithIdentifier: CellIdentifier];
if (plan_cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"mobilePlanDetailsCellTableViewCell" owner:self options:nil];
    plan_cell = [nib objectAtIndex:0];
}

plan_cell.label.text = [[tblArr objectAtIndex:indexPath.row] objectForKey:@"Answer1"];
plan_cell.Label2.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:@"answer2"];
plan_cell.Label3.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:@"answer3"];
plan_cell.label4.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:@"answer4"];


 return plan_cell;
 }

}

Upvotes: 2

Vizllx
Vizllx

Reputation: 9246

Why you are doing such complex coding, Just simply provide different tag value for Table Views, and you are returning cell outside of your if condition which is wrong. Follow the below code.

Do a checking like this:-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {  
     if(tableView.tag==1)// Table 1
       {

           //load cell as per your choice
           //cell operations
           return cell;
        }
      else // Table 2
       {
         //load cell as per your choice
           //cell operations
           return cell;
        }

Upvotes: 1

FormigaNinja
FormigaNinja

Reputation: 1571

The problem is that you are returning the cell outside the if (tableView==table), so your program will NOT reach the code after it.

Instead of

if (tableView==table) {
    cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];

}return cell; //This line is wrong

You should do this:

if (tableView==table) {
    cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];

    //Should be inside the if scope
    return cell;
}

Upvotes: 1

Related Questions