Ios 目前在我的应用程序中,点击时打开多个单元格,如何仅扩展“;一个&x201D;一次一个手机?
我想在单击时展开tableview单元格。在我的tableview中,两个或更多单元格是可扩展的。但我想做的是,当我点击可展开单元时,它应该显示它下面的单元,再次点击时它应该隐藏。这意味着点击一个单元,该单元应该展开,只有一个单元应该折叠Ios 目前在我的应用程序中,点击时打开多个单元格,如何仅扩展“;一个&x201D;一次一个手机?,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我想在单击时展开tableview单元格。在我的tableview中,两个或更多单元格是可扩展的。但我想做的是,当我点击可展开单元时,它应该显示它下面的单元,再次点击时它应该隐藏。这意味着点击一个单元,该单元应该展开,只有一个单元应该折叠 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{ UITableView *mTableView; NSMutableArray *s
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
UITableView *mTableView;
NSMutableArray *sectionTitleArray;
NSMutableDictionary *sectionContentDict;
NSMutableArray *arrayForBool;
NSInteger previousSelectedIndex;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
mTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
mTableView.delegate = self;
mTableView.dataSource = self;
mTableView.rowHeight = 70;
[self.view addSubview:mTableView];
if (!sectionTitleArray) {
sectionTitleArray = [NSMutableArray arrayWithObjects:@"Aachen", @"Berlin", @"Düren", @"Essen", @"Münster", nil];
}
if (!arrayForBool) {
arrayForBool = [NSMutableArray arrayWithObjects:[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO] , nil];
}
if (!sectionContentDict) {
sectionContentDict = [[NSMutableDictionary alloc] init];
NSArray *array1 = [NSArray arrayWithObjects:@"bla 1", @"bla 2", @"bla 3", @"bla 4", nil];
[sectionContentDict setValue:array1 forKey:[sectionTitleArray objectAtIndex:0]];
NSArray *array2 = [NSArray arrayWithObjects:@"wurst 1", @"käse 2", @"keks 3", nil];
[sectionContentDict setValue:array2 forKey:[sectionTitleArray objectAtIndex:1]];
NSArray *array3 = [NSArray arrayWithObjects:@"banane", @"auto2", @"haus", @"eidechse", nil];
[sectionContentDict setValue:array3 forKey:[sectionTitleArray objectAtIndex:2]];
NSArray *array4 = [NSArray arrayWithObjects:@"hoden", @"pute", @"eimer", @"wichtel", @"karl", @"dreirad", nil];
[sectionContentDict setValue:array4 forKey:[sectionTitleArray objectAtIndex:3]];
NSArray *array5 = [NSArray arrayWithObjects:@"Ei", @"kanone", nil];
[sectionContentDict setValue:array5 forKey:[sectionTitleArray objectAtIndex:4]];
}
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [sectionTitleArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([[arrayForBool objectAtIndex:section] boolValue]) {
return [[sectionContentDict valueForKey:[sectionTitleArray objectAtIndex:section]] count];
}
return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
headerView.tag = section;
headerView.backgroundColor = [UIColor whiteColor];
UILabel *headerString = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width-20-50, 50)];
BOOL manyCells = [[arrayForBool objectAtIndex:section] boolValue];
if (!manyCells) {
headerString.text = @"click to enlarge";
}else{
headerString.text = @"click again to reduce";
}
headerString.textAlignment = NSTextAlignmentLeft;
headerString.textColor = [UIColor blackColor];
[headerView addSubview:headerString];
UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
[headerView addGestureRecognizer:headerTapped];
//up or down arrow depending on the bool
UIImageView *upDownArrow = [[UIImageView alloc] initWithImage:manyCells ? [UIImage imageNamed:@"upArrowBlack"] : [UIImage imageNamed:@"downArrowBlack"]];
upDownArrow.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
upDownArrow.frame = CGRectMake(self.view.frame.size.width-40, 10, 30, 30);
[headerView addSubview:upDownArrow];
return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footer = [[UIView alloc] initWithFrame:CGRectZero];
return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([[arrayForBool objectAtIndex:indexPath.section] boolValue]) {
return 50;
}
return 1;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
BOOL manyCells = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
if (!manyCells) {
cell.textLabel.text = @"click to enlarge";
}
else{
NSArray *content = [sectionContentDict valueForKey:[sectionTitleArray objectAtIndex:indexPath.section]];
cell.textLabel.text = [content objectAtIndex:indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
#pragma mark - gesture tapped
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
if (indexPath.row == 0) {
BOOL collapsed = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
collapsed = !collapsed;
[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:collapsed]];
//reload specific section animated
NSRange range = NSMakeRange(indexPath.section, 1);
NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
[mTableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];
}
previousSelectedIndex = indexPath.section;
}
@界面视图控制器(){
UITableView*mTableView;
NSMUTABLEARRY*sectionTitleArray;
NSMutableDictionary*sectionContentDict;
NSMutableArray*arrayforpool;
NSInteger先前选择的索引;
}
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
mTableView=[[UITableView alloc]initWithFrame:self.view.bounds样式:UITableViewStylePlain];
mTableView.delegate=self;
mTableView.dataSource=self;
mTableView.rowHeight=70;
[self.view addSubview:mTableView];
如果(!sectionTitleArray){
sectionTitleArray=[NSMutableArray阵列,其对象为:@“亚琛”,“柏林”,“杜伦”,“埃森”,“明斯特”,无];
}
如果(!ArrayForPool){
ArrayForPool=[NSMutableArrayWithObjects:[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:否],
[NSNumber numberWithBool:否],
[NSNumber numberWithBool:否],
[NSNumber numberWithBool:NO],无];
}
如果(!sectionContentDict){
sectionContentDict=[[NSMutableDictionary alloc]init];
NSArray*array1=[NSArray ARRAY WITH OBJECTS:@“bla 1”、“bla 2”、“bla 3”、“bla 4”、nil];
[sectionContentDict设置值:array1-forKey:[sectionTitleArray对象索引:0];
NSArray*array2=[NSArray arrayWithObjects:@“wurst 1”,“käse 2”,“keks 3”,无];
[sectionContentDict设置值:array2 forKey:[sectionTitleArray对象索引:1]];
NSArray*阵列3=[NSArray阵列,其对象为:@“banane”@“auto2”@“haus”@“eidechse”,无];
[sectionContentDict设置值:array3 forKey:[sectionTitleArray对象索引:2]];
NSArray*阵列4=[NSArray阵列,其对象为:@“hoden”@“pute”@“eimer”@“wichtel”@“karl”@“dreirad”,无];
[sectionContentDict设置值:array4 forKey:[sectionTitleArray对象索引:3];
NSArray*array5=[NSArray arrayWithObjects:@“Ei”@“kanone”,nil];
[sectionContentDict设置值:array5 forKey:[sectionTitleArray对象索引:4];
}
//加载视图后,通常从nib执行任何其他设置。
}
#pragma标记-表视图
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回[sectionTitleArray计数];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
if([[ArrayForPool objectAtIndex:section]boolValue]){
返回[[sectionContentDict valueForKey:[sectionTitleArray对象索引:section]]count];
}
返回1;
}
-(UIView*)表格视图:(UITableView*)表格视图用于标题部分:(NSInteger)部分{
UIView*headerView=[[UIView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,50)];
headerView.tag=节;
headerView.backgroundColor=[UIColor whiteColor];
UILabel*headerString=[[UILabel alloc]initWithFrame:CGRectMake(10,0,self.view.frame.size.width-20-50,50)];
BOOL-manyCells=[[arrayForBool对象索引:节]boolValue];
如果(!manyCells){
headerString.text=@“单击以放大”;
}否则{
headerString.text=@“再次单击以减少”;
}
headerString.textAlignment=NSTextAlignmentLeft;
headerString.textColor=[UIColor blackColor];
[headerView添加子视图:headerString];
UITapGestureRecognizer*headerTapped=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderTapped:)];
[headerView AddGestureRecognitor:headerTapped];
//向上或向下箭头取决于bool
UIImageView*upDownArrow=[[UIImageView alloc]initWithImage:manyCells?[UIImageName:@“upArrowBlack”]:[UIImageName:@“downArrowBlack”];
upDownArrow.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
upDownArrow.frame=CGRectMake(self.view.frame.size.width-40,10,30,30);
[headerView添加子视图:向上向下箭头];
返回headerView;
}
-(UIView*)表视图:(UITableView*)表视图页脚位置:(NSInteger)部分{
UIView*footer=[[UIView alloc]initWithFrame:CGRectZero];
返回页脚;
}
-(CGFloat)表格视图:(UITableView*)表格视图头部高度部分:(NSInteger)部分{
返回50;
}
-(CGFloat)表视图:(UITableView*)表视图页脚高度:(NSInteger)部分{
返回1;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径{
if([[ArrayForPool objectAtIndex:indexPath.section]boolValue]){
返回50;
}
返回1;
}
//自定义表格视图单元格的外观。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone){
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
}
BOOL-manyCells=[[arrayForBool对象索引:indexPath.section]boolValue];
如果(!manyCells){
cell.textlab.text=@“单击放大”;
}
否则{
NSArray*content=[sectionContentDict valueForKey:[sectionTitleArray对象索引:indexPath
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
[mTableView beginUpdates];
if (indexPath.row == 0) {
BOOL collapsed = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
collapsed = !collapsed;
[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:collapsed]];
if (previousSelectedIndex != indexPath.section)
{
[mTableView reloadSections:[NSIndexSet indexSetWithIndex:previousSelectedIndex] withRowAnimation:UITableViewRowAnimationFade];
[arrayForBool replaceObjectAtIndex:previousSelectedIndex withObject:[NSNumber numberWithBool:0]];
}
//reload specific section animated
NSRange range = NSMakeRange(indexPath.section, 1);
NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
[mTableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];
}
previousSelectedIndex = indexPath.section;
[mTableView endUpdates];
}