【问题标题】:Determine different cells for iPhone and iPad in tableView:cellForRowAtIndexPath:在 tableView:cellForRowAtIndexPath 中确定 iPhone 和 iPad 的不同单元格:
【发布时间】:2013-04-19 13:35:08
【问题描述】:

我只是尝试为 iPhone 和 iPad 创建一个应用程序。为此,我创建了一个表格视图,该表格视图创建的单元格期望应用程序加载时将收到的 json。方法内部

tableView:cellForRowAtIndexPath:

我启动将用于 iPhone 的单元格,并从 json 中设置特定值。我使用的单元格是我使用新对象和 NIB 文件创建的自定义单元格。代码如下所示:

static NSString *CellIdentifier = @"tvChannelIdentifier";

tvChannelCell *cell = (tvChannelCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"tvChannelCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
....

现在我尝试让该应用程序也可以在 iPad 上运行,我创建了一个新的自定义单元格,还包含一个新对象和一个 NIB 文件。变量的名称与 customIPhoneCell 中的名称相同,因此我不必更改整个代码。我刚刚在 tableView:cellForRowAtIndexPath: 中插入了一个开关,以便显示正确的单元格,但代码无法访问它:

static NSString *CellIdentifier = @"tvChannelCell";
static NSString *IPadCellIdentifier = @"tvChannelIPadCell";


//determination which device ---> choose cell
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    tvChannelCell *cell = (tvChannelCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

}else{

    tvChannelIPadCell = (tvChannelIPadCell *) [tableView dequeueReusableCellWithIdentifier:IPadCellIdentifier];
}

我试图在类的顶部定义单元格对象,但是我不能使用相同的变量名。我还尝试选择 iPad 的 NIB 文件来检查它是否会显示,但没有任何反应。我尝试在顶部设置一个 UITableviewcell,

UITableViewCell *cell

并在我的 switch 中的 cellForRowAtIndexPath 中设置特定的单元格类型,但随后无法访问变量。

所以我的问题是,是否可以在 tableview 方法中执行此切换,或者我是否必须为每个单元格类型都有自己的变量名的每个设备编写几个部分?

【问题讨论】:

    标签: iphone objective-c ipad uitableview detection


    【解决方案1】:

    我做什么:

    只创建了一个带有 2 个 xib 的 UITableViewCell 子类,一个用于 iPad 的 xib,另一个用于 iPhone。

    tvChannelCell *cell = (tvChannelCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
    NSArray *nib;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    
         nib = [[NSBundle mainBundle] loadNibNamed:@"tvChannelCell_iPhone" owner:self options:nil];
    
    }else{
    
        nib = [[NSBundle mainBundle] loadNibNamed:@"tvChannelCell_iPad" owner:self options:nil];
    }
    
        cell = [nib objectAtIndex:0];
    }
    

    【讨论】:

    • 谢谢。我现在将 iPad-NIB 连接到与 iPhone-NIB 相同的对象。
    • @xandruCea :很好,你的问题已经解决了,但你应该始终考虑或接受答案(如果它们相同),首先回答的答案..
    • @jcesar :你是对的,但你的答案在第一行有单元对象的初始化,这只是帮助我将两个 NIB 文件与一个对象连接起来,所以你的对我帮助最大
    【解决方案2】:

    这行代码对区分iphone和ipad很有用,

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            tvChannelCell *cell = (tvChannelCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        }else{
    
            cell = (tvChannelIPadCell *) [tableView dequeueReusableCellWithIdentifier:IPadCellIdentifier];
    
         }
    

    【讨论】:

      【解决方案3】:

      很简单,为什么不为 iPad 创建另一个 nib,然后在设备是 iPad 时访问它。

      您的所有代码都将保持不变,即您无需像任何地方一样比较 iPad

      喜欢:

      if (cell == nil) {
          NSArray *nib;
          if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
           nib = [[NSBundle mainBundle] loadNibNamed:@"tvChannelCell-iPad" owner:self options:nil];}
          else{
          nib = [[NSBundle mainBundle] loadNibNamed:@"tvChannelCell" owner:self options:nil];}
          cell = [nib objectAtIndex:0];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-13
        • 2017-05-18
        • 1970-01-01
        • 2012-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-16
        相关资源
        最近更新 更多