實(shí)際效果圖:
定義屬性
@property (nonatomic, assign) BOOL isSelect; // 判斷是否選擇
@property (nonatomic, assign) NSInteger currentSection;//當(dāng)前選擇的(section)
@property (nonatomic, assign) NSInteger currentIndex;//當(dāng)前選擇的(row)
@property (nonatomic, assign) NSInteger lastCurrentSection;//上一次打開的(section)
@property (nonatomic, assign) NSInteger lastCurrentIndex;//上一次打開的(row)
首先初始化值: 這個(gè)隨便了只要不與當(dāng)前的tableView里的scetion 和 row 重復(fù)就行
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];
根據(jù)選擇返回高度
if (indexPath.row == _currentIndex && indexPath.section == _currentSection) {
if (self.isSelect == YES) {
把當(dāng)前的選擇保存下來
_currentIndex = indexPath.row;
_currentSection = indexPath.section;
返回當(dāng)前實(shí)際需要的高度
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{
//判斷選中狀態(tài)
if (indexPath.row == self.currentIndex && indexPath.section == self.currentSection) {
判斷是否為新打開的一個(gè)選項(xiàng)
if (self.isSelect == YES) {
打開狀態(tài)的設(shè)置
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 {
恢復(fù)默認(rèn)狀態(tài)
cell.titleLable.textColor = [UIColor blackColor];
cell.round.backgroundColor = [UIColor blackColor];
cell.backgroundColor = [UIColor whiteColor];
cell.line.backgroundColor = [UIColor grayColor];
}
}else{
不選中的都為默認(rèn)狀態(tài)
cell.titleLable.textColor = [UIColor blackColor];
cell.round.backgroundColor = [UIColor blackColor];
cell.backgroundColor = [UIColor whiteColor];
cell.line.backgroundColor = [UIColor grayColor];
}
讓上一次恢復(fù)默認(rèn)狀態(tài)
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;
}
}
選擇點(diǎn)擊事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
判斷當(dāng)前的選擇
if (indexPath.row == _currentIndex && indexPath.section == _currentSection) {
選擇狀態(tài)置為非
self.isSelect = ! self.isSelect;
}else{
記住上一行并刷新
self.lastCurrentIndex = _currentIndex;
self.lastCurrentSection = _currentSection;
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:_currentIndex inSection:self.lastCurrentSection];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
選擇狀態(tài)恢復(fù)默認(rèn)
self.isSelect = YES;
}
刷新當(dāng)前選擇行
_currentIndex = indexPath.row;
_currentSection = indexPath.section;
NSIndexPath *indexPathOne=[NSIndexPath indexPathForRow:_currentIndex inSection:_currentSection];
[tableView reloadRowsAtIndexPaths:@[indexPathOne] withRowAnimation:UITableViewRowAnimationAutomatic];
}