我想动态添加行。我有建筑物名称的表视图列表。如果有人选择建筑物(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比较:]:或 [NSIndexPath _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 工作原理的基本解释开始,以便您可以开始解决此问题:
首先,表视图通过调用以下命令询问表中有多少个部分:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
}
在你的代码中,你只需告诉它它有一个部分。 但是,在后面的代码中,当您尝试向表中添加行时,您会告诉它要将行添加到第二部分(索引为 1)。 因此,您要么需要将这些行添加到第 0 节,要么更新上述方法以告诉它,有时有两个节。
其次,tableView 通过调用以下命令询问表的每个部分中有多少行:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
}
在代码中,您只需返回区域数。 但是,如上所述,您需要包括已添加到表中的行。 如果要将它们添加到其他部分,则需要返回不同的值,具体取决于section
变量中请求索引的部分中有多少行。 如果它们都在同一部分中,则需要将它们相加并返回正确的值。
第三,tableView 通过调用以下命令请求该行的实际单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
}
在您的代码中,您只返回一个包含由 indoorZones 数组填充的数据的单元格,但您还需要提供为特定区域/楼层正确配置的单元格。 同样,您需要根据需要通过节号或行号来确定这一点。
最后,当您单击一行时,表视图会通过调用以下方法告诉您:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
在此方法中,您需要更新前面函数使用的数据源,以便在再次调用它们时,它们将提供正确的数据。 数据源必须镜像您希望表视图的外观。 在您的例子中,您有一个名为 indoorZone 的数组。 如果您只想显示区域列表,这是您开始做的事情,这很好。 但是,如果要添加更多行,需要先向数据源添加更多行,以便在 tableView 重新启动此过程时,它已经存在。
如果您希望所有内容都保留在一个部分中,那么我会想出一个可以包含两种类型的行的数据源,并且能够区分它们,以便cellForRowAtIndexPath
可以创建正确类型的单元格并返回它。
如果你想要两个部分,那么我会为第二个部分添加第二个数组(因为它不是相同类型的数据),并根据您需要用于该部分的数组,在每个方法中返回适当的值。
我希望这有帮助!