通过在tableview iPhone错误中选择tableview行来添加动态子行?

时间:2012-06-28 14:12:23

标签: iphone objective-c uitableview

我想动态添加行。我有建筑名称的tableview列表。如果有人选择构建(didSelectRowAtIndexPath),那么建筑物的各个楼层应该作为子行动态添加。它最大化和最小化各个建筑物清单选择的子行程。我该怎么做呢。提前谢谢......

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

didSlectRowAtIndexPath方法:

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

它给出了以下错误[__NSArrayI compare:]:或者[NSIndexPath _fastCStringContents:]:发送到实例的无法识别的选择器。我尝试了很多方法,但可能是我缺少某个地方。请建议。提前致谢。

2 个答案:

答案 0 :(得分:0)

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

}

这可能符合您的要求

答案 1 :(得分:0)

好吧,所以不要听起来很卑鄙,但这里有太多问题需要考虑。

让我们从对tableView工作方式的基本解释开始,以便您可以开始解决这个问题:

首先,tableView通过调用来询问表中有多少个部分:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

}

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

其次,tableView通过调用以下方式询问表格每个部分中的行数:

- (NSInteger)tableView:(UITableView *)tableView 
             numberOfRowsInSection:(NSInteger)section {

}

在您的代码中,您只需返回区域数量。但是,如上所述,您需要包含已添加到表中的行。如果要将它们添加到不同的部分,则需要返回不同的值,具体取决于section变量中要求索引的部分中的行数。如果它们都在同一部分中,那么您需要将它们相加并返回正确的值。

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

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

}

在您的代码中,您只返回一个包含由indoorZones数组填充的数据的单元格,但您还需要提供为特定区域/楼层正确配置的单元格。同样,您需要根据需要通过部分编号或行号来确定。

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

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

}

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

如果您希望所有内容都保留在一个部分中,那么我会想出一个可以包含两种类型的行的数据源,并且能够区分它们,以便cellForRowAtIndexPath可以创建正确的类型细胞并将其归还。

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

我希望这有帮助!