我想展开tableview单元格单击它。有一个tableview,其中两个单元格可扩展,其他单元格不可扩展。我需要当我点击可展开的单元格时,它应该显示它下面的单元格,并再次点击它应该隐藏。下面有一张图片定义了这一点。展开UITableViewstylePlain中的UITableView单元格
我怎样才能实现这个功能,请指导的上方。 在此先感谢。
我想展开tableview单元格单击它。有一个tableview,其中两个单元格可扩展,其他单元格不可扩展。我需要当我点击可展开的单元格时,它应该显示它下面的单元格,并再次点击它应该隐藏。下面有一张图片定义了这一点。展开UITableViewstylePlain中的UITableView单元格
我怎样才能实现这个功能,请指导的上方。 在此先感谢。
Here是可扩展的UITableView
完整的教程下面是该代码剪断。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
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 = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
}
else
{
[expandedSections 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];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
}
}
}
如下图所示画面
这是组表,我只是要求普通表。 – 2013-02-27 10:14:41
@iPhone是否为普通表格检查了[this](http://www.roostersoftstudios.com/2011/04/14/iphone-uitableview-with-animated-expanding-cells/) – swiftBoy 2013-02-27 10:19:21
我以前见过这样的示例发布的问题,这个使用部分扩大了,原来我们只有一节只有cell需要扩展。 – 2013-02-27 10:21:13
我想你想要的是Apple给出的。您可以在表格视图动画和手势的标题下看看苹果提供的Sample Code。
这可能是由委托
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
//write code here
}
你有没有使用这种方法来达到这个目的,你可以发布这个样本代码 – 2013-02-27 10:33:47
好了,我在想什么是,标题,活动,开销和朋友将自定义UIView
与UIImageView
背景,UILabel
为标题,并UIButton
的扩大。所以基本上
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
有你的标题返回计数。有
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
每个标题回报UIView
。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
具有该标题内的行数。 您可能需要为此维护一个数组。具有细胞项
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
最初的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
它应该是0(零)的所有部分中,当用户敲击扩大,它应该是相对于增加的行数*该部分中的单元格高度。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
你可能需要一些良好的逻辑来设置所有行的扩张
您的扩展按钮的操作类似,
- (void) expandSection:(UIButton *)sender;
,你可以找出哪些部分使用sender.tag被扩大,所以不要忘记正确添加标签。您可能需要int
中的.h
文件来存储当前段,并且可以使用- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
数据源方法。
JKExpandTableView是MIT许可的iOS库,实现了expandable table view。一定要检查包含的示例项目。
接受的答案是正确的,但该链接已经过期。为了让这个例子工作,你必须做一些额外的事情:
下载ExpandableTableCells项目从这里:
https://github.com/cocoanetics/Examples
下载DTCustomColoredAccessory.h和.m文件从这里: https://github.com/Cocoanetics/DTFoundation/tree/develop/Core/Source/iOS
把DTCustomColoredAccessory ExpandableTableCells项目中的文件。
在那里你有一个工作的例子,它更容易编辑和从那里移动,而不是试图从零写入。
我已经下载了'Download ExpandableTableCells项目'并添加了下载DTCustomColoredAccessory.h和.m文件。但我得到一个错误“错误:链接器命令失败,退出代码1(使用-v来查看调用)”你能帮忙吗? – 2015-01-26 13:33:47
你能在错误中发布更多细节吗? – Esqarrouth 2015-01-26 13:37:33
你的问题解决了吗? – Bhavin 2013-02-27 10:16:29
不,还没有... – 2013-02-27 10:17:34
你检查了我的答案吗? – Bhavin 2013-02-27 10:18:20