故事板上的ios可折叠/可扩展表视图
我想制作一个可折叠/可扩展的tableview,其中我有两个标题product和services,每个标题都包含10+个带有自定义单元格的对象,该单元格左侧包含复选标记和标签。我查看了教程,但大多数教程都在使用.xib,而我的项目是基于故事板的。 我查看了这些教程,有人能帮我吗故事板上的ios可折叠/可扩展表视图,ios,iphone,uitableview,Ios,Iphone,Uitableview,我想制作一个可折叠/可扩展的tableview,其中我有两个标题product和services,每个标题都包含10+个带有自定义单元格的对象,该单元格左侧包含复选标记和标签。我查看了教程,但大多数教程都在使用.xib,而我的项目是基于故事板的。 我查看了这些教程,有人能帮我吗 https://www.cocoacontrols.com/controls/collapseclick https://www.cocoacontrols.com/controls/ratreeview https:/
https://www.cocoacontrols.com/controls/collapseclick
https://www.cocoacontrols.com/controls/ratreeview
https://www.cocoacontrols.com/controls/combobox-for-uitableview
我在以下网站上找到了一个很好的示例项目: 它很容易理解和实现。
//
//
// ViewController.h
// expandableTV
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSMutableIndexSet *expandedSections;
}
@property (weak, nonatomic) IBOutlet UITableView *tbl_expandablecategory;
@end
//
// ViewController.m
// expandableTV
//
#import "ViewController.h"
#import "expandableTC.h"`
@interface ViewController ()
{
NSArray *jsonArray;
BOOL currentlyExpanded;
}
@end
@implementation ViewController
@synthesize tbl_expandablecategory;
NSMutableArray *arr_categorymenumodel;
- (void)viewDidLoad {
[super viewDidLoad];
arr_categorymenumodel=[[NSMutableArray alloc] init];
if (!expandedSections)
{
expandedSections = [[NSMutableIndexSet alloc] init];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 6;//[arr_categorymenumodel count];
}
- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self tableView:tableView canCollapseSection:section])
{
if ([expandedSections containsIndex:section])
{
return 2;
}
}
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0)
{
return 40;
}
return 270;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell...
if (!indexPath.row)
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =@"Room ";// [NSString stringWithFormat:@"%@",[[arr_categorymenumodel objectAtIndex:indexPath.section] valueForKey:@"CategoryName"]];
// cell.backgroundColor=[UIColor colorWithRed:237.0/255.0 green:237.0/255.0 blue:237.0/255.0 alpha:1.0];
cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue" size:20.0];
if (currentlyExpanded)
{
}
cell.accessoryView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"rightarrow.png"]];
return cell;
}
else
{
expandableTC *cell = [tableView dequeueReusableCellWithIdentifier:@"Customcell"];
return cell;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
currentlyExpanded = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
cell.accessoryView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"rightarrow.png"]];
}
else
{
[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
cell.accessoryView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"downarrow.png"]];
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
[tmpArray addObject:tmpIndexPath];
}
if (currentlyExpanded)
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationFade];
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationFade];
}
}
}
}
@end
//ViewController.h
//可扩展电视
#进口
@界面ViewController:UIViewController
{
NSMutableIndexSet*扩展节;
}
@属性(弱、非原子)IBUITableView*tbl_expandablecategory;
@结束
//
//ViewController.m
//可扩展电视
//
#导入“ViewController.h”
#导入“expandableTC.h”`
@界面视图控制器()
{
NSArray*jsonArray;
BOOL当前扩展;
}
@结束
@实现视图控制器
@综合tbl_可扩展范畴;
NSMutableArray*arr_类别数模型;
-(无效)viewDidLoad{
[超级视图下载];
arr_categorymenumodel=[[NSMutableArray alloc]init];
如果(!expandedSections)
{
expandedSections=[[NSMutableIndexSet alloc]init];
}
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回6;/[arr_categorymenumodel count];
}
-(BOOL)tableView:(UITableView*)tableView可以合并分区:(NSInteger)分区
{
返回YES;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
if([自身表格视图:表格视图可合并部分:节])
{
if([扩展节包含索引:节])
{
返回2;
}
}
返回1;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径
{
if(indexPath.row==0)
{
返回40;
}
返回270;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//配置单元格。。。
如果(!indexPath.row)
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
cell.textlab.text=@“房间”;/[NSString stringWithFormat:@“%@”,[[arr_categorymenumodel对象索引:indexPath.section]valueForKey:@“CategoryName”];
//cell.backgroundColor=[UIColor COLOR WITHRED:237.0/255.0绿色:237.0/255.0蓝色:237.0/255.0 alpha:1.0];
cell.textLabel.font=[UIFont fontWithName:@“HelveticaNeue”大小:20.0];
如果(当前扩展)
{
}
cell.accessoryView=[[UIImageView alloc]initWithImage:[UIImageName:@“rightarrow.png”];
返回单元;
}
其他的
{
expandableTC*单元格=[tableView dequeueReusableCellWithIdentifier:@“Customcell”];
返回单元;
}
}
#pragma标记-表视图委托
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
if([self tableView:tableView canCollapseSection:indexPath.section])
{
如果(!indexPath.row)
{
UITableViewCell*单元格=[tableView cellForRowAtIndexPath:indexPath];
[tableView取消行索引路径:indexPath动画:是];
NSInteger section=indexPath.section;
currentlyExpanded=[expandedSections包含索引:section];
NSInteger行;
NSMutableArray*tmpArray=[NSMutableArray];
如果(当前扩展)
{
行=[self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
cell.accessoryView=[[UIImageView alloc]initWithImage:[UIImageName:@“rightarrow.png”];
}
其他的
{
[扩展节添加索引:节];
行=[self tableView:tableView numberOfRowsInSection:section];
cell.accessoryView=[[UIImageView alloc]initWithImage:[UIImageName:@“downarrow.png”];
}
对于(int i=1;当我单击标题时,iImage没有上下移动