2 tableview on a single view

前端 未结 2 989
深忆病人
深忆病人 2020-12-15 14:22

I need an example or explanations of how to populate 2 table views which are on the same view. I need to understand the \"cellForRowAtIndexPath\" method, could someone provi

相关标签:
2条回答
  • 2020-12-15 15:00

    On one view controller if you have to use two tables then you can set IBOutlet to both tables or assigns different tag to them so when you use following cellForRowAtIndexPath you can differentiate in both tables as below

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {   
    
        UITableViewCellStyle style =UITableViewCellStyleSubtitle;
        static NSString *MyIdentifier = @"MyIdentifier"; 
        DataListCell  *cell = (DataListCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    
        cell = [[DataListCell alloc]  initWithStyle:style reuseIdentifier:MyIdentifier];
    
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    
    
            if(tableView==tblLanguage)//tblLanguage IBOutlet for first table
            {  
                if ((selectedIndexPath != nil) && (selectedIndexPath.row == indexPath.row))
                {
    
                    UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(320-30, 9, 22, 15)];
                    imgView.image=[UIImage imageNamed:@"btn_Expand.png"];
                    [cell addSubview:imgView];
    
    
    
                    tblSongs.hidden=NO;
                    tblSongs.frame=CGRectMake(0,42, 320, ([arrSongListForSpecificLanguage count]*40));
                    [cell addSubview:tblSongs];
                }
                else 
                {
                    UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(320-30, 9, 16, 22)];
                    imgView.image=[UIImage imageNamed:@"btn_Collaps.png"];
                    [cell addSubview:imgView];
                }
    
    
                cell.lblCustomerName.textColor=[UIColor blackColor];
                cell.lblCustomerName.font=[UIFont boldSystemFontOfSize:16];
    
    
    
    
                //set the first label which is always a NamesArray object
                [cell setcustomerName:[objAppDelegate.viewController.arrLanguage objectAtIndex:indexPath.row]];
            }
            else //for other table
            {
                ParseData *objParse;
                objParse=[arrSongListForSpecificLanguage objectAtIndex:indexPath.row];
    
    
    
                cell.lblCustomerName.textColor=[UIColor blackColor];
                cell.lblCustomerName.frame=CGRectMake(cell.lblCustomerName.frame.origin.x, cell.lblCustomerName.frame.origin.y, 310, cell.lblCustomerName.frame.size.height);
    
    
                //set the first label which is always a NamesArray object
                [cell setcustomerName:objParse.track];
    
            }
            return cell;    
        }
    
    
    }
    

    You can also use tag for the same in which your if statement as below if(tableView.tag==1)//tblLanguage tag=1

    Similar if statement use for other delegate & datasource methods of table

    0 讨论(0)
  • 2020-12-15 15:02

    IMO the cleanest solution would be to have one controller for each tableview.

    radios_tv would call it own delegate's method, while presets_tv calls it own.

    edit

    if you use one controller for n tableview, you will have to use if-statemenst in many places, in

    • – numberOfSectionsInTableView:
    • – tableView:numberOfRowsInSection:
    • – tableView:titleForHeaderInSection:

    basically in all UITableViewDatasource-Protocol methods that you will need to implement.

    So if you need to change something, you have to change it in many places.

    If you use one controller class for one tableview, you won't have to check at all.

    1. write a controller class for every tableview, make it conforming to the UITableViewDatasource protocol
      • implement the protocol methods you will need. at least
        • – numberOfSectionsInTableView:,
        • – tableView:numberOfRowsInSection:,
        • – tableView:cellForRowAtIndexPath:
    2. call -setDataSource:for every tableview to an object of the right controller class

    I think, it was shown in one of the WWDC 2010 videos. I am not sure, but I guess it was Session 116 - Model-View-Controller for iPhone OS.

    edit

    I wrote an example code: http://github.com/vikingosegundo/my-programming-examples

    0 讨论(0)
提交回复
热议问题