【问题标题】:How to create a Expandable view without using tableview in iOS如何在 iOS 中不使用 tableview 创建可扩展视图
【发布时间】:2014-04-11 07:22:09
【问题描述】:

我是 iPhone 开发的初学者,无法创建可展开的视图。就像点击 tableview row 一样,它们扩展到更多行。我想创建两个带有一些标题的标题,当用户首先单击然后他们的详细视图打开时,再次单击详细视图时隐藏动画。在第二个标题中相同。

这两个标题之间应该有一些区别。 请帮我 。我在此链接的图片中找到了一个关于我的要求的链接,但无法实施。

【问题讨论】:

  • 如果你知道怎么做,为什么不能使用表格视图?
  • 感谢您的回复,实际上在我的情况下只有两个标题,单击时会在其底部打开一个详细视图,然后再次单击该标题隐藏,我可以像上图所示创建相同的这个stackoverflow.com/questions/1944428/…。如果是请指导我,我也想确认我可以在我的项目中使用github.com/appsome/AccordionView库并根据我的要求进行修改。请帮帮我。

标签: ios objective-c uitableview


【解决方案1】:

您可以有一个高度为 0 的 UIView,然后在单击按钮时将其展开:

UIView *expandableView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
expandableView.backgroundColor = [UIColor redColor];
expandableView.alpha = 0;

然后当你点击你的按钮时你会做一个动画

[self.view addSubview:expandableView];
CGRect newFrame = expandableView.frame;
newFrame.size.height = 100;

//If this is the first view you would have to move the second header below 
CGRect newHeaderFrame = header2.frame;
newHeaderFrame.origin.y += expandableView.frame.size.height; 

[UIView animateWithDuration:0.3 animations:^{
    expandableView.frame = newFrame;
    expandableView.alpha = 1;
    header2.frame = newHeaderFrame;
}];

当你想缩小它时:

CGRect newFrame = expandableView.frame;
newFrame.size.height = 0;

//If this is the first view you would have to move back to header 2
CGRect newHeaderFrame = header2.frame;
newHeaderFrame.origin.y -= newFrame.size.height; 

[UIView animateWithDuration:0.3 animations:^{
    expandableView.frame = newFrame;
    expandableView.alpha = 0;
    header2.frame = newHeaderFrame;
} completion:^{
    [expandableView removeFromSuperview];
}];

【讨论】:

  • 非常感谢,但是有两个header,那么如何管理容器高度,就是说有两个header大家点击展开,第一个用户先点击然后第一个展开第二个会自动显示在第一个展开视图的底部。请告诉我,如何管理这个
  • 如果只有第二个标题要移动,您也可以在动画中更改其帧。我刚刚编辑了我的答案。但如果有其他视图要移动,我强烈建议使用 UITableView。
  • 意味着,我必须使用 tableview 并隐藏它们的边框并添加为子视图。请告诉我它是否有效,因为我必须在视图底部添加这些扩展视图,视图的其他部分已经填充了一些标签和 imageView。
  • 我不明白你到底想要什么。我要说的是,如果您只有几个标题(不是动态的),没有太多要移动的视图并且不想使用 uitableview,那么我的解决方案就很棒。否则会变得乱七八糟。如果您最终想使用 uitableview,请查看 Harsh 提供的链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 2022-08-14
  • 1970-01-01
相关资源
最近更新 更多