2 单个视图上的表视图

Posted

技术标签:

【中文标题】2 单个视图上的表视图【英文标题】:2 tableview on a single view 【发布时间】:2010-08-15 20:58:55 【问题描述】:

我需要一个示例或说明如何填充位于同一视图上的 2 个表视图。我需要了解“cellForRowAtIndexPath”方法,有人可以给我一个关于代码应该如何的示例吗?

我的意思是如何识别哪个表视图是哪个?

谢谢

下面是我的 cellForRowAtIndexPath 方法:

 // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    

// Configure the cell...
// Set up the cell
MyAppAppDelegate *appDelegate = (MyAppAppDelegate *)[[UIApplication sharedApplication] delegate];
    if (tableView == radios_tv)  //radio_tv is an IBOutleet UITableView
        sqlClass *aRadio = (sqlClass *)[appDelegate.array_radios objectAtIndex:indexPath.row];
        [cell setText:aRadio.r_name];
        return cell;
    
    if (tableView == presets_tv)  //preset_tv is an IBOutlet UITableView


    



嘿 vikingsegundo,现在我需要删除 TableViewController 类上的一个单元格,我该怎么做?我解释一下,这是我的代码:

- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath 

    if(editingStyle == UITableViewCellEditingStyleDelete) 

        //Get the object to delete from the array.
        Coffee *coffeeObj = [appDelegate.coffeeArray objectAtIndex:indexPath.row];
        [appDelegate removeCoffee:coffeeObj];

        //Delete the object from the table.
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    

既然我们放置了不同的控制器,我们应该如何处理这条线?我应该放 tableViewController 而不是“self”吗?

//Delete the object from the table.
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

【问题讨论】:

【参考方案1】:

IMO 最简洁的解决方案是为每个 tableview 设置一个控制器。

radios_tv 会调用它自己的委托方法,而presets_tv 会调用它自己的方法。

编辑

如果你对 n tableview 使用一个控制器,你将不得不在很多地方使用 if-statemenst, 在

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

基本上在您需要实现的所有 UITableViewDatasource-Protocol 方法中。

因此,如果您需要更改某些内容,则必须在很多地方进行更改。

如果你为一个 tableview 使用一个控制器类,你根本不需要检查。

    为每个tableview编写一个控制器类,使其符合UITableViewDatasource协议 实现您需要的协议方法。至少 – numberOfSectionsInTableView:, – tableView:numberOfRowsInSection:, – tableView:cellForRowAtIndexPath: 为每个表视图调用-setDataSource:到正确控制器类的对象

我认为,它显示在WWDC 2010 videos 之一中。我不确定,但我猜是Session 116 - iPhone OS 的模型-视图-控制器

编辑

我写了一个示例代码:http://github.com/vikingosegundo/my-programming-examples

【讨论】:

我们如何做到这一点?你说的控制器是什么意思?对不起,我是个新手:s 我现在正在寻找一个例子。请继续关注:) 谢谢 :D 我也找了一个例子,但我一直找不到:( 我明天上班试试看 :D 非常感谢 vikingosegundo :D 对不起 vikingosegundo,你说的一个控制器一个 tableview 是什么意思?可以举个例子吗?【参考方案2】:

在一个视图控制器上,如果您必须使用两个表,那么您可以将 IBOutlet 设置为两个表或为它们分配不同的标签,因此当您使用以下 cellForRowAtIndexPath 时,您可以区分两个表,如下所示

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



您也可以在 if 语句中使用标签,如下所示 if(tableView.tag==1)//tblLanguage tag=1

类似的 if 语句用于表的其他委托和数据源方法

【讨论】:

以上是关于2 单个视图上的表视图的主要内容,如果未能解决你的问题,请参考以下文章

如何在 MVC 的单个视图中制作插入表单和显示来自数据库的数据的表?

MarkerView 在图表上的最后一个点走出图表

Swift 中的表视图页脚

如何防止数据透视图成为工作表副本上的常规图表?

视图控制器上的表视图控制器

可视化工具-视图-事物-以及存储过程