我希望在表格视图中,当单击可扩展的单元格时,能够展开该单元格。其中有两个单元格是可扩展的,其他单元格则不可扩展。我需要在单击可扩展的单元格时,它应该显示其下方的单元格,并再次单击时隐藏。下面的图片定义了这一点。
我可以帮助您实现这个功能,请为上述内容提供指导。 谢谢。我希望在表格视图中,当单击可扩展的单元格时,能够展开该单元格。其中有两个单元格是可扩展的,其他单元格则不可扩展。我需要在单击可扩展的单元格时,它应该显示其下方的单元格,并再次单击时隐藏。下面的图片定义了这一点。
我可以帮助您实现这个功能,请为上述内容提供指导。 谢谢。这里是带有可展开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];
}
}
}
}
如下图所示
好的,我的想法是,标题“Events”,“Overhead”和“Friends”将是自定义的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 heightForRowAtIndexPath:(NSIndexPath *)indexPath;
单元格项的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
最初所有部分的值应该为0,当用户点击展开时,它应该相对于该部分中行数*单元格高度增加。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void) expandSection:(UIButton *)sender;
您可以使用sender.tag来识别要展开的部分,所以不要忘记正确添加标签。您可能需要在.h文件中使用int类型来存储当前部分,并且可以在- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section数据源方法中使用。
接受的答案是正确的,但链接已过时。要使该示例正常工作,您需要执行额外的操作:
从此处下载ExpandableTableCells项目:
https://github.com/cocoanetics/Examples
从此处下载DTCustomColoredAccessory.h和.m文件:
https://github.com/Cocoanetics/DTFoundation/tree/develop/Core/Source/iOS
将DTCustomColoredAccessory文件放入ExpandableTableCells项目中。
这样就有一个可工作的示例了,从那里开始编辑和移动比尝试从零开始编写更容易。
Download ExpandableTableCells project
并添加了 Download DTCustomColoredAccessory.h
和 .m
文件。但是我遇到了一个错误 "error: linker command failed with exit code 1 (use -v to see invocation)",你能帮忙解决一下吗? - Luai Kalkatawi通过委托,这是可能的。
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
//write code here
}