Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用动态重用标识符设置自定义UITableViewCell并将其退出队列?_Ios_Objective C_Uitableview_Nib - Fatal编程技术网

Ios 如何使用动态重用标识符设置自定义UITableViewCell并将其退出队列?

Ios 如何使用动态重用标识符设置自定义UITableViewCell并将其退出队列?,ios,objective-c,uitableview,nib,Ios,Objective C,Uitableview,Nib,以下是我最终要做的事情。我希望在UITableView中动态显示项目菜单,以便显示的项目类型决定加载的自定义单元格视图。例如,假设菜单项类型为“switch”,那么它将加载一个名为“switch.xib”的nib,并且根据特定菜单项的值,状态将为on/off。可能有5项属于“开关”类型,但值不同。所以我想对每一个都使用相同的xib,但有5个实例 所以,这个问题还有很长的路要走。当我从nib加载cell视图时,我认为当它在屏幕上滚动时,它需要唯一的重用标识符来进行出列,对吗?(对于每个实例,即每个

以下是我最终要做的事情。我希望在UITableView中动态显示项目菜单,以便显示的项目类型决定加载的自定义单元格视图。例如,假设菜单项类型为“switch”,那么它将加载一个名为“switch.xib”的nib,并且根据特定菜单项的值,状态将为on/off。可能有5项属于“开关”类型,但值不同。所以我想对每一个都使用相同的xib,但有5个实例

所以,这个问题还有很长的路要走。当我从nib加载cell视图时,我认为当它在屏幕上滚动时,它需要唯一的重用标识符来进行出列,对吗?(对于每个实例,即每个菜单项都是唯一的。)在Interface Builder的UITableViewCell中,我看到了可以在何处设置重用标识符属性,但我希望在运行时为交换机的每个实例设置该属性。例如,菜单项#1是一个开关,#2是一个文本字段,#3是一个开关,等等。因此#1和#3都需要唯一的单元格ID才能出列

下面是我的CellForRowatineXpath的外观:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Cells are unique; dequeue individual cells not generic cell formats
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.row];

ITMenuItem *menuItem = [menu.menuItems objectAtIndex:indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    // Load cell view from nib
    NSString *cellNib = [NSString stringWithFormat:@"MenuCell_%@", menuItem.type];
    [[NSBundle mainBundle] loadNibNamed:cellNib owner:self options:nil];
    cell = myCell;
    self.myCell = nil;
}
// Display menu item contents in cell
UILabel *cellLabel = (UILabel *) [cell viewWithTag:1];
[cellLabel setText:menuItem.name];
if ([menuItem.type isEqualToString:@"switch"]) {
    UISwitch *cellSwitch = (UISwitch *) [cell viewWithTag:2];
    [cellSwitch setOn:[menuItem.value isEqualToString:@"YES"]];
}
else if ([menuItem.type isEqualToString:@"text"]) {
    UITextField *textField = (UITextField *) [cell viewWithTag:2];
    [textField setText:menuItem.value];
}

return cell;
}

您可以在nib文件中设置重用标识符。因此,对于switch.xib,您将使用“switch”作为重用标识符。那就换衣服吧

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.row]; NSString*CellIdentifier=[NSString stringWithFormat:@“单元格%d”,indexath.row]; 到

NSString*CellIdentifier=menuItem.type;
假设
menuItem.type
是“开关”

对于我的想法,如果您的类型不是太多,请在不同的xib和swift文件中设计每个单元格。主要针对性能问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    TextFieldFormElement *item = [self.formItems objectAtIndex:indexPath.row];
    cell.labelField.text = item.label;
    return cell;
}
如果无法退出队列,请为他们提供不同的标识符。您可以为tableview或collection view注册多个标识符(我们的一个应用程序以这种方式使用12个不同的单元格)


但是用这种方法处理iBaction会有点麻烦。

您的自定义单元格(来自xib文件)可以这样加载。xib文件名与单元类名和重用id相同

- (YourCell *)tableView:(UITableView *)_tableView getCellWithId:(NSString *)cellId
{
        YourCell *cell;

        /* Cell id and xib have the same name. */
        cell = [_tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil];
                cell = [nib objectAtIndex:0];
        }
        return cell;
}

你的问题是什么?你有什么问题?什么错误信息?对不起,可能有点难说清楚。问题是,我如何在分配单元时使用重用标识符标记该单元,以便出列工作?正确,menuItem.type为“switch”、“text”等。问题是,它的各个实例没有唯一性;假设我在同一个菜单中有5个“切换”菜单项,这不是一个问题吗?也许不会,也许我想得太多了。是的,这正是你想要的。具有相同的重用标识符将意味着“switch”单元的一个实例可以为不同的交换机重用。在我的例子中,对于少量菜单项,在不增加内存使用量的情况下,似乎可以很好地工作,根本不让单元出列。我不知道如果它试图同时在屏幕上显示多个“开关”单元格,是否会导致问题。可能不会,因为只有从屏幕上滚动下来的单元格才会排出来。由于菜单不长,无论如何也不会有太多的排队。谢谢你的提示!是否可以从xib加载?
- (YourCell *)tableView:(UITableView *)_tableView getCellWithId:(NSString *)cellId
{
        YourCell *cell;

        /* Cell id and xib have the same name. */
        cell = [_tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil];
                cell = [nib objectAtIndex:0];
        }
        return cell;
}