通过选择表视图中的表行添加动态子行iPhone错误? [英] Adding dynamic sub-rows by selecting tableview row in tableview iPhone errors?

查看:96
本文介绍了通过选择表视图中的表行添加动态子行iPhone错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态添加行.我有建筑物名称的tableview列表.如果有人选择建筑物(didSelectRowAtIndexPath),则建筑物的各个楼层应作为子行动态添加.就像最大化和最小化各个建筑物列表选择中的子行.我该怎么做呢.预先感谢...

I want to add row dynamically. I have tableview list of building names. If some one choose building(didSelectRowAtIndexPath) then respective floors of building should get added dynamically as subrow. Its like maximizing and minimizing the subrow on respective building list selection. How do I do this. Thanks in advance...

- (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方法:

cellForRowAtIndexPath method:

  - (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方法:

didSlectRowAtIndexPath method:

 - (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比较:]:或[NSIndexPath _fastCStringContents:]:无法识别的选择器已发送到实例.我尝试了很多方法,但可能缺少某处.请提出建议.提前致谢.

It Gives following error [__NSArrayI compare:]: Or [NSIndexPath _fastCStringContents:]: unrecognized selector sent to instance. I tried many ways but may be I am lacking somewhere. Please suggest. thanks in advance.

推荐答案

好吧,听起来并不刻薄,但是这里有太多问题要数.

Okay, so not to sound mean, but there are almost too many issues here to count.

让我们首先对tableView的工作原理进行基本说明,以便您可以开始解决此问题:

Let's start with a basic explanation of how tableView's work so that you can start to fix this:

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

First, the tableView asks how many sections are in the table by calling:

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

}

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

In your code, you simply tell it that it has one section. However, in your later code, when you try to add rows to your table, you tell it that you want to add your rows to a second section (with an index of 1). Therefore, you either need to add these rows to section 0 instead, or update the above method to tell it that, sometimes, there are two sections.

第二,tableView通过调用以下命令询问表的每个部分中有多少行:

Second, the tableView asks how many rows are in each section of the table by calling:

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

}

在您的代码中,您只是返回区域的数量.但是,像上面一样,您需要包括添加到表中的行.如果要将它们添加到其他部分,则需要返回不同的值,具体取决于该部分中有多少行以及在section变量中要求的索引.如果它们都在同一部分,则需要将它们加起来并返回正确的值.

In your code, you are simply returning the number of zones. However, like above, you need to include the rows that you have added to your table. If you are adding them to a different section, then you need to return different values, depending on how many rows are in the section with the index asked for in the section variable. If they are all in the same section, then you need to add them up and return the correct value.

第三,tableView通过调用以下行来为该行请求一个实际的单元格:

Third, the tableView asks for an actual cell for the row by calling:

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

}

在您的代码中,您仅返回一个包含由IndoorZones阵列填充的数据的单元格,但是您还需要提供针对特定区域/楼层正确配置的单元格.同样,您需要根据需要通过部分号或行号来确定它.

In your code, you are only returning a cell which has data populated by the indoorZones array, but you also need to supply cells which are configured properly for the specific zone/floor. Again, you either need to determine this by section number or row number as appropriate.

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

Finally, when you click on a row, the tableview tells you by calling the following method:

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

}

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

In this method, you need to update your data source that is used by the previous functions so that when they get called again, they will provide the correct data. Your data source must mirror the way that you want your table view to look. In your case, you have an array called indoorZones. This is good if you just want to display a list of zones, which is what you start off doing. However, when you want to add more rows, you need to add more rows to your data source first, so that when the tableView starts this process over, it is already there.

如果您希望所有内容都停留在一个部分中,那么我想出一个可以同时包含两种类型的行的数据源,并且能够区分它们,以便cellForRowAtIndexPath可以创建适当类型的单元格并把它返还.

If you want everything to stay in one section, then I would come up with a data source that can include both types of rows, and be able to distinguish between them so that cellForRowAtIndexPath can create the proper type of cell and return it.

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

If you want two sections, then I would add a second array for the second section (since it is not the same type of data) and return the appropriate values in each of these methods, based on which array you need to use for that section.

我希望这会有所帮助!

这篇关于通过选择表视图中的表行添加动态子行iPhone错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆