我想在它上面展开tableview单元格。在我的tableview中,两个或多个单元格是可扩展的。但我想制作,当我点击可扩展单元格时,它应该显示它下面的单元格并再次点击它应该隐藏。这意味着点击一个单元格,单元格应该只扩展另一个单元格应该是折叠
@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;
}
请帮助我,我出错的地方
答案 0 :(得分:0)
首先,我假设您希望一次只展开一个部分,并在打开时关闭其他标题。如果我的理解正确,请更新您的问题。
在您的代码中,您只是将arrayForBool中的值设置为1,以便扩展该部分。先前扩展的部分怎么样?您需要将其设置为0.
您没有代码可以折叠之前展开的部分。请尝试下面的代码,让我知道它是否有效:
- (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];
}
希望这有帮助。