如何在同一视图控制器中加载两个tableview与索引行的单元格?

时间:2020-12-08 14:02:58

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.

我面对的一个模块包括在同一个UIViewController中加载两个不同的UITableView我知道我在哪里做错了,问题是表视图方法中的行AtIndexpath的单元格。我只在UIViewController中获得一个UITableView,但我的secondviewcontroller不返回单元格中的任何值。

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

 }

3 个解决方案

#1


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

}

#2


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

问题是你要在if(tableView == table)之外返回单元格,所以你的程序不会在它之后到达代码。

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

#3


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.

为什么要进行这样复杂的编码,只需为表视图提供不同的标记值,然后返回if条件之外的单元格,这是错误的。请遵循以下代码。

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

#1


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

}

#2


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

问题是你要在if(tableView == table)之外返回单元格,所以你的程序不会在它之后到达代码。

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

#3


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.

为什么要进行这样复杂的编码,只需为表视图提供不同的标记值,然后返回if条件之外的单元格,这是错误的。请遵循以下代码。

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