Iphone 是否通过在tableview中选择tableview行来添加动态子行?

Iphone 是否通过在tableview中选择tableview行来添加动态子行?,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我想动态添加行。我有一个建筑名称的tableview列表。如果有人选择building(DidSelectRowatineXpath),则应将建筑的各个楼层动态添加为子视图。它类似于最大化和最小化各个建筑列表选择上的子程序。我该怎么做呢。先谢谢你 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // There is only one section. if (tableView == indoortable

我想动态添加行。我有一个建筑名称的tableview列表。如果有人选择building(DidSelectRowatineXpath),则应将建筑的各个楼层动态添加为子视图。它类似于最大化和最小化各个建筑列表选择上的子程序。我该怎么做呢。先谢谢你

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// There is only one section.
if (tableView == indoortable || tableView == indoortable_iPad)
{
    return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section    {
// Return the number of time zone names.
if (tableView == indoortable || tableView == indoortable_iPad)
{
    return [indoorZones count];

}

}
cellForRowAtIndexPath方法:

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if (tableView == indoortable || tableView == indoortable_iPad)
{
    static NSString *CellIdentifier = @"Cell";

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

        cell.selectionStyle = UITableViewCellSelectionStyleGray;             //cell bg
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }       
    // Set up the cell.
    //cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];

    cell.textLabel.text =[indoorZones objectAtIndex: indexPath.row];
    //[cell setIndentationLevel:[[self.indoorZones objectAtIndex:indexPath.row] intValue]];
    return cell;
}
}
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath 
 {
zonesFloor = [[NSMutableArray alloc]init];
zonesFloorA = [[NSArray alloc] initWithObjects:@"Gr fl",@"1st fl",@"2nd fl",nil];

[zonesFloor addObject:zonesFloorA]; 
if (tableView == indoortable )
{

    NSUInteger i=indexPath.row+1;
    for (NSArray *count in self.indoorZones)  //app is crashing here giving  error.......Collection <__NSArrayM: 0x4b1d550> was mutated while being enumerated.
 {

        [zonesFloor addObject:[NSIndexPath indexPathForRow:i inSection:0]];
        [self.indoorZones insertObject:zonesFloor atIndex:i++];


    }

    [[self indoortable] beginUpdates];
    [[self indoortable] insertRowsAtIndexPaths:(NSArray *)zonesFloor  withRowAnimation:UITableViewRowAnimationNone];
    [[self indoortable] endUpdates];

    }


if (tableView == indoortable_iPad )
{

    //some logic
}

[tableView deselectRowAtIndexPath:selectedIndexPath animated:NO];
}
didSlectRowAtIndexPath方法:

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if (tableView == indoortable || tableView == indoortable_iPad)
{
    static NSString *CellIdentifier = @"Cell";

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

        cell.selectionStyle = UITableViewCellSelectionStyleGray;             //cell bg
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }       
    // Set up the cell.
    //cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];

    cell.textLabel.text =[indoorZones objectAtIndex: indexPath.row];
    //[cell setIndentationLevel:[[self.indoorZones objectAtIndex:indexPath.row] intValue]];
    return cell;
}
}
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath 
 {
zonesFloor = [[NSMutableArray alloc]init];
zonesFloorA = [[NSArray alloc] initWithObjects:@"Gr fl",@"1st fl",@"2nd fl",nil];

[zonesFloor addObject:zonesFloorA]; 
if (tableView == indoortable )
{

    NSUInteger i=indexPath.row+1;
    for (NSArray *count in self.indoorZones)  //app is crashing here giving  error.......Collection <__NSArrayM: 0x4b1d550> was mutated while being enumerated.
 {

        [zonesFloor addObject:[NSIndexPath indexPathForRow:i inSection:0]];
        [self.indoorZones insertObject:zonesFloor atIndex:i++];


    }

    [[self indoortable] beginUpdates];
    [[self indoortable] insertRowsAtIndexPaths:(NSArray *)zonesFloor  withRowAnimation:UITableViewRowAnimationNone];
    [[self indoortable] endUpdates];

    }


if (tableView == indoortable_iPad )
{

    //some logic
}

[tableView deselectRowAtIndexPath:selectedIndexPath animated:NO];
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
{
ZoneFloor=[[NSMutableArray alloc]init];
ZonesFlora=[[NSArray alloc]initWithObjects:@“Gr fl”,“1st fl”,“2nd fl”,nil];
[zonesFloor添加对象:zonesFloorA];
如果(tableView==不可移植)
{
整数i=indexPath.row+1;
for(NSArray*count in self.indoorZones)//应用程序在此崩溃,出现错误…….集合在枚举时发生了变异。
{
[zonesFloor addObject:[NSIndexPath indexPathForRow:i分区:0]];
[self.indoorZones-insertObject:zonesFloor-atIndex:i++];
}
[[self indortable]beginUpdates];
[[self indortable]插入rowsatindexpaths:(NSArray*)带rowsanimation的区域地板:uitableviewrowsanimationnone];
[[self indortable]endUpdates];
}
如果(tableView==Indootable_iPad)
{
//一些逻辑
}
[表视图取消显示索引路径:已选择索引路径动画:否];
}
它给出以下错误[\uu NSArrayI compare:]:或[nsindepath\u fastCStringContents:]:发送到实例的选择器无法识别。我尝试了很多方法,但可能是我缺少什么。请建议。提前谢谢

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath 
{
    //NSIndexPath *selectedIndexPath = [self.indoortable indexPathForSelectedRow];
zonesFloorA = [[NSArray alloc] initWithObjects:@"Gr fl",@"1st fl",@"2nd fl",nil];

if (tableView == indoortable )
{
    for (NSString *str in zonesFloorA) {
        [indoorZones addObject:str];
   }
   //[[self indoortable] beginUpdates];
   //[[self indoortable] insertRowsAtIndexPaths:(NSArray *)zonesFloor  withRowAnimation:UITableViewRowAnimationNone];
   //[[self indoortable] endUpdates];
}
if (tableView == indoortable_iPad )
{

//some logic
 }

  [tableView reloadData];

}

希望这能满足您的要求

好吧,听起来不那么刻薄,但这里的问题太多了

让我们从tableView工作原理的基本解释开始,这样您就可以开始解决这个问题:

首先,tableView通过调用询问表中有多少节:

在代码中,您只需告诉它有一个部分。但是,在后面的代码中,当您尝试向表中添加行时,会告诉它您希望将行添加到第二个部分(索引为1)。因此,您需要将这些行添加到节0中,或者更新上面的方法,告诉它有时有两个节

其次,tableView通过调用来询问表的每个部分中有多少行:

在代码中,只需返回区域数。但是,与上面一样,您需要包括已添加到表中的行。如果要将它们添加到不同的节中,则需要返回不同的值,具体取决于
变量中要求索引的节中有多少行。如果它们都在同一节中,则需要将它们相加并返回正确的值

第三,tableView通过调用来请求行的实际单元格:

在代码中,您只返回一个单元格,其中包含由indoorZones数组填充的数据,但还需要提供为特定区域/楼层正确配置的单元格。同样,您需要根据节号或行号(视情况而定)来确定这一点

最后,当您单击一行时,tableview通过调用以下方法告诉您:

在这种方法中,您需要更新先前函数使用的数据源,以便在再次调用这些函数时,它们将提供正确的数据。数据源必须镜像您希望表视图的外观。在本例中,您有一个名为indoorZones的数组。如果您只想显示区域列表,这是很好的,这是您开始做的事情。但是,当您想要添加更多行时,需要首先向数据源添加更多行,这样当tableView重新开始这个过程时,它已经在那里了

如果您希望所有内容都保留在一个部分中,那么我将提供一个数据源,该数据源可以包含两种类型的行,并能够区分它们,以便
cellforrowatinexpath
可以创建适当类型的单元格并返回它

如果您需要两个部分,那么我将为第二部分添加第二个数组(因为它不是相同类型的数据),并根据您需要为该部分使用的数组在每个方法中返回适当的值


我希望这有帮助

它给了我错误[\uu NSArrayI addObject:]:无法识别的选择器发送到实例…使其成为indoorZones可变数组现在它没有显示在tableview列表中您是否分配了它..我想您是在viewdidload中通过nsarray分配的,或者其中??是NSMutableArray*indoorZones=[[NSMutableArray alloc]initwithobject:@“A1”…,nil];yaar在.h(NSMutableArray*indoorZones)中声明您的indoorZones,并在ViewDidLoad:indoorZones=[[NSMutableArray alloc]initwithobject:@“A1”…,nil]中这样分配它;我已经做了一些更改并更新了上面的代码……我得到了以下错误……集合在枚举时发生了变异。。在控制台中,我可以看到行和子行被添加,但它没有显示。当您在数组上使用快速枚举时,您不能更改数组。由于您实际上并没有使用indoorZones中的对象,只需将(NSArray*count in self.indoorZones)更改为
for(int x=0;x<[self.indoorZones count];x++)
。嗨……我已经完成了应用程序中的所有模块,除了上面的tableview。每次我都有新的错误。我想和你共享这个模块文件,所以你可以提供你的邮件ID吗。
- (UITableViewCell *)tableView:(UITableView *)tableView 
                     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

}
- (void)tableView:(UITableView *)tableView 
        didSelectRowAtIndexPath:(NSIndexPath   *)indexPath {

}