美文网首页
tableView 点击打开和关闭 (section index

tableView 点击打开和关闭 (section index

作者: 吃货_X | 来源:发表于2016-08-31 18:55 被阅读254次

实际效果图:

定义属性

@property (nonatomic, assign) BOOL isSelect; // 判断是否选择

@property (nonatomic, assign) NSInteger currentSection;//当前选择的(section)

@property (nonatomic, assign) NSInteger currentIndex;//当前选择的(row)

@property (nonatomic, assign) NSInteger lastCurrentSection;//上一次打开的(section)

@property (nonatomic, assign) NSInteger lastCurrentIndex;//上一次打开的(row)

首先初始化值: 这个随便了只要不与当前的tableView里的scetion 和 row 重复就行

self.isSelect = YES;

self.currentIndex = -1;

self.currentSection = 0;

self.lastCurrentSection = -2;

self.lastCurrentIndex = -2;

其次是返回cell的行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

//  NSDictionary *dic = self.dataSource[indexPath.section];

//   NSArray *array = dic[@"sList"];

//    NSDictionary *dic = self.dataSource[indexPath.section];

//    NSArray *array = dic[@"sList"];

//

//    CGRect titleRect = [array[indexPath.row][@"sTitle"] boundingRectWithSize:CGSizeMake(MAINSCREEN_SIZE.width - 53, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingUsesDeviceMetrics attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];

//

//    CGRect contentRect = [array[indexPath.row][@"sIntro"] boundingRectWithSize:CGSizeMake(MAINSCREEN_SIZE.width - 63, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingUsesDeviceMetrics attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil];

根据选择返回高度

if (indexPath.row == _currentIndex && indexPath.section == _currentSection) {

if (self.isSelect == YES) {

把当前的选择保存下来

_currentIndex = indexPath.row;

_currentSection = indexPath.section;

返回当前实际需要的高度

return titleRect.size.height + contentRect.size.height + 80;

}

返回正常的高度

return titleRect.size.height + 50;

}else{

返回正常的高度

return titleRect.size.height + 50;

}

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

//判断选中状态

if (indexPath.row == self.currentIndex && indexPath.section == self.currentSection) {

判断是否为新打开的一个选项

if (self.isSelect == YES) {

打开状态的设置

cell.titleLable.textColor = GetColor(17, 180, 187, 1);

cell.round.backgroundColor = GetColor(17, 180, 187, 1);

cell.backgroundColor = GetColor(230, 230, 230, 1);

cell.line.backgroundColor = GetColor(17, 180, 187, 1);

cell.contentLable.hidden = NO;

cell.linkBut.hidden = NO;

} else {

恢复默认状态

cell.titleLable.textColor = [UIColor blackColor];

cell.round.backgroundColor = [UIColor blackColor];

cell.backgroundColor = [UIColor whiteColor];

cell.line.backgroundColor = [UIColor grayColor];

}

}else{

不选中的都为默认状态

cell.titleLable.textColor = [UIColor blackColor];

cell.round.backgroundColor = [UIColor blackColor];

cell.backgroundColor = [UIColor whiteColor];

cell.line.backgroundColor = [UIColor grayColor];

}

让上一次恢复默认状态

if (indexPath.section == self.lastCurrentSection && indexPath.row == self.lastCurrentIndex  ) {

cell.titleLable.textColor = [UIColor blackColor];

cell.round.backgroundColor = [UIColor blackColor];

cell.backgroundColor = [UIColor whiteColor];

cell.line.backgroundColor = [UIColor grayColor];

cell.contentLable.hidden = YES;

cell.linkBut.hidden = YES;

}

}

选择点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

判断当前的选择

if (indexPath.row == _currentIndex && indexPath.section == _currentSection) {

选择状态置为非

self.isSelect = ! self.isSelect;

}else{

记住上一行并刷新

self.lastCurrentIndex = _currentIndex;

self.lastCurrentSection = _currentSection;

NSIndexPath *indexPath=[NSIndexPath indexPathForRow:_currentIndex inSection:self.lastCurrentSection];

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

选择状态恢复默认

self.isSelect = YES;

}

刷新当前选择行

_currentIndex = indexPath.row;

_currentSection = indexPath.section;

NSIndexPath *indexPathOne=[NSIndexPath indexPathForRow:_currentIndex inSection:_currentSection];

[tableView reloadRowsAtIndexPaths:@[indexPathOne] withRowAnimation:UITableViewRowAnimationAutomatic];

}

相关文章

网友评论

      本文标题:tableView 点击打开和关闭 (section index

      本文链接:https://www.haomeiwen.com/subject/kiwmsttx.html