Xcode 关闭UITable的所有部分
我有一个可扩展单元的UItable。 当用户点击某个部分时,它会展开并显示行。但是我需要在打开新的分区之前关闭以前打开的分区。我想我需要在didselectrow做这个,只是不知道怎么做 我的didselectrow代码如下Xcode 关闭UITable的所有部分,xcode,uitableview,Xcode,Uitableview,我有一个可扩展单元的UItable。 当用户点击某个部分时,它会展开并显示行。但是我需要在打开新的分区之前关闭以前打开的分区。我想我需要在didselectrow做这个,只是不知道怎么做 我的didselectrow代码如下 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(tableView.tag == 4) {
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView.tag == 4)
{
//NSLog(@"Did Select Row");
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// only first row toggles exapand/collapse
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
BOOL currentlyExpanded = [expandedSections3 containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections3 removeIndex:section];
}
else
{
[expandedSections3 addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded)
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
}
}
else {
}
}
//NSLog(@"Button Pressed?");
}
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
{
if(tableView.tag==4)
{
//NSLog(@“未选择行”);
if([self tableView:tableView canCollapseSection:indexPath.section])
{
如果(!indexPath.row)
{
//仅第一行切换展开/折叠
[tableView取消行索引路径:indexPath动画:是];
NSInteger section=indexPath.section;
BOOL currentlyExpanded=[expandedSections3包含索引:section];
NSInteger行;
NSMutableArray*tmpArray=[NSMutableArray];
如果(当前扩展)
{
行=[self tableView:tableView numberOfRowsInSection:section];
[扩展节3移除索引:节];
}
其他的
{
[扩展节3附加索引:节];
行=[self tableView:tableView numberOfRowsInSection:section];
}
对于(int i=1;i请尝试从tableView:didSelectRowAtIndexPath:
中调用UITableView的重新加载节:withRowAnimation:
。这样,表视图就可以为您指定的节重新查询您的代理。这样,您可以为以这种方式添加和删除的行获得一个漂亮的动画
当然,这与当前直接操作表视图行的解决方案大不相同,因此如果要尝试重新加载节:withRowAnimation:
,可能需要重写代理的大部分内容。更改将涉及直接在您的委托或委托引用的模型对象中,以便委托在被表视图重新查询时具有更新的返回值。您好,您知道有哪些教程可以帮助我完成您的建议吗???@hanimal\u p:没有,我没有任何特定的教程。我建议您学习Apple的文档对于UITableView
,尤其是关于各种重新加载方法的部分。然后是Apple的,最后我会求助于。如果您对“状态保存”或“模型对象”部分感到困惑,我建议您查找MVC设计模式。