简体   繁体   English

在 tableView:cellForRowAtIndexPath 中确定 iPhone 和 iPad 的不同单元格:

[英]Determine different cells for iPhone and iPad in tableView:cellForRowAtIndexPath:

I just try to create an app for iPhone and iPad.我只是尝试为 iPhone 和 iPad 创建一个应用程序。 For this i created a tableview that creates cells expecting the json that will be received when the app is loading.为此,我创建了一个 tableview,它创建了单元格,期望在加载应用程序时将收到 json。 Inside the method方法里面

tableView:cellForRowAtIndexPath:

I initiate the cell that will be used for the iPhone and I set the specific values from the json.我启动将用于 iPhone 的单元格,并从 json 设置特定值。 The cell I use is a custom Cell i created with a new object and NIB-File.我使用的单元格是我使用新对象和 NIB 文件创建的自定义单元格。 The code looks like this:代码如下所示:

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

Now that I try to make the app work on the iPad too, I created a new custom cell, also with a new object and a NIB-File.现在我也尝试让应用程序在 iPad 上运行,我创建了一个新的自定义单元格,还有一个新对象和一个 NIB 文件。 The names of the variables are the same as in the customIPhoneCell so that I don't have to change the whole code.变量的名称与 customIPhoneCell 中的名称相同,因此我不必更改整个代码。 I just inserted a switch inside the tableView:cellForRowAtIndexPath: so that i show the right cell but it can't be accessed by the code:我刚刚在 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];
}

I tried to define the cell objects at the top of the class but then i can't use the same variable name.我试图在类的顶部定义单元格对象,但是我不能使用相同的变量名。 I also tried to choose the NIB file of the iPad to check if it'll be displayed but nothing happens.我还尝试选择 iPad 的 NIB 文件来检查它是否会显示,但没有任何反应。 And I tried to set an UITableviewcell at the top,我试图在顶部设置一个 UITableviewcell,

UITableViewCell *cell

and set the specific cell type inside my switch in cellForRowAtIndexPath but then the variables can't be accessed.并在 cellForRowAtIndexPath 中设置我的开关内的特定单元格类型,但随后无法访问变量。

So my question is, is it possible to do this switch inside the tableview method or do i have to code to several parts for each device where each celltype has its own variablename?所以我的问题是,是否可以在 tableview 方法中进行此切换,还是必须为每个单元格类型都有自己的变量名的每个设备编码到几个部分?

What I do:我做的事情:

I create only one UITableViewCell subclass with 2 xibs , one xib for iPad and another for iPhone.创建了一个带有 2 个 xibs 的 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];
}

It's quite simple, why don't you create another nib for iPad, and just access it when device is iPad.很简单,为什么不为iPad创建另一个笔尖,然后在设备是iPad时访问它。

You all code will remain same, ie you don't need to compare iPad like anywhere你所有的代码都将保持不变,即你不需要像任何地方一样比较 iPad

Like:像:

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

this line of code useful for differenciating iphone and ipad,这行代码对于区分iphone和ipad很有用,

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

    }else{

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

     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM