Ios 无法同时添加和删除行
我正在学习一个教程,在这个教程中,我可以通过添加一些子单元格来扩展表视图,并通过删除子单元格来折叠表。我正在尝试更改扩展操作的执行方式。当我点击一行时,它会展开并显示子单元格,当我点击另一行时,上一个展开的行会关闭。我不能这样做。我尝试了以下操作,但无法使一行一次可展开,而另一行在展开时应关闭 注意:此代码工作正常,但要折叠行,需要在同一行上点击两次。当另一个家长被点击时,我试图崩溃 代码如下:Ios 无法同时添加和删除行,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在学习一个教程,在这个教程中,我可以通过添加一些子单元格来扩展表视图,并通过删除子单元格来折叠表。我正在尝试更改扩展操作的执行方式。当我点击一行时,它会展开并显示子单元格,当我点击另一行时,上一个展开的行会关闭。我不能这样做。我尝试了以下操作,但无法使一行一次可展开,而另一行在展开时应关闭 注意:此代码工作正常,但要折叠行,需要在同一行上点击两次。当另一个家长被点击时,我试图崩溃 代码如下: - (UITableViewCell *)tableView:(UITableView *)tab
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Node *node = [self nodeForIndexPath:indexPath];
// differenet table view cell prototypes for different node levels
UITableViewCell *cell;
if(node.level == 0)
{
cell = [tableView dequeueReusableCellWithIdentifier:@"level1cell" forIndexPath:indexPath];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:@"level2cell" forIndexPath:indexPath];
}
// set the nodes title as row text
cell.textLabel.text = node.title;
// attach the nodeId for later lookup when selected
cell.tag = node.nodeId;
// Configure the cell...
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Node *node = [self nodeForIndexPath:indexPath];
// NSLog(@"node id is %ld level is %ld and indexpath.row is %d",(long)node.nodeId,(long)node.level,indexPath.row);
node.expanded = !node.expanded;
if (node.level==0) {
NSLog(@"you tapped parent");
//now check other parents are expanded or not
}else{
NSLog(@"you tapped child");
}
//insertion always happen after deletion
// NSLog(@"you touched at %@,index row is %d",indexPath,indexPath.row);
if(node.expanded )
{
// add n rows
NSMutableArray *indexPaths = [NSMutableArray array];
for(NSInteger i=indexPath.row; i< indexPath.row+node.subNodes.count; i++)
{
[indexPaths addObject:[NSIndexPath indexPathForRow:i+1 inSection:0]];
}
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
else
{
//you store the node refe after row is expanded
// delete n rows
NSMutableArray *indexPaths = [NSMutableArray array];
for(NSInteger i=indexPath.row; i< indexPath.row+node.subNodes.count; i++)
{
[indexPaths addObject:[NSIndexPath indexPathForRow:i+1 inSection:0]];
}
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
}
#pragma mark - Private helper
-(Node*) nodeForIndexPath:(NSIndexPath*)indexPath
{
int idx = 0;
for(Node *node in _nodes)
{
if(idx == indexPath.row)
{
return node;
}
idx++;
if(node.expanded)
{
for (Node *subNode in node.subNodes)
{
if(idx == indexPath.row)
{
return subNode;
}
idx++;
}
}
}
return nil;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
Node*Node=[self-nodeForIndexPath:indexPath];
//不同节点级别的DifferNet表视图单元原型
UITableViewCell*单元格;
如果(node.level==0)
{
cell=[tableView dequeueReusableCellWithIdentifier:@“level1cell”forIndexPath:indexPath];
}
其他的
{
cell=[tableView dequeueReusableCellWithIdentifier:@“level2cell”forIndexPath:indexPath];
}
//将节点标题设置为行文本
cell.textlab.text=node.title;
//选中时,附加节点ID以供以后查找
cell.tag=node.nodeId;
//配置单元格。。。
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
Node*Node=[self-nodeForIndexPath:indexPath];
//NSLog(@“节点id为%ld,级别为%ld,indexpath.row为%d”,(长)node.nodeId,(长)node.level,indexpath.row);
node.expanded=!node.expanded;
if(node.level==0){
NSLog(@“您点击了家长”);
//现在检查是否扩展了其他父级
}否则{
NSLog(@“您点击了孩子”);
}
//插入总是在删除之后发生
//NSLog(@“您在%@,索引行是%d”,indexath,indexPath.row);
如果(node.expanded)
{
//添加n行
NSMutableArray*索引路径=[NSMutableArray];
对于(NSInteger i=indexPath.row;i
KAccordontableViewController可以帮助您吗
嗯。。。如果当前代码正常工作,但您需要点击两次以折叠打开/选定的行,可能是因为在第一次点击期间调用了
didSelectRowAtIndexPath:
,而不是didSelectRowAtIndexPath:
,以便取消选择选定的行。我建议将tableview配置为允许多个选择,以便每次调用didSelectRowAtIndexPath:
,例如:
- (void)viewDidLoad {
[super viewDidLoad];
tableView.allowsMultipleSelection = YES;
}
我尽了一切努力添加了一个故事板,但没有在我的表视图上显示任何内容。为什么不呢?它也支持故事板。我添加了一个表视图控制器和两个单元格。一个是父母,一个是孩子。我删除了app delegate中的窗口分配。但是我的表视图中没有显示任何内容。我要将其粘贴到这里吗?