tableView 點(diǎn)擊打開和關(guān)閉 (section indexPath)

實(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];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末谓谦,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌伐厌,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件佩脊,死亡現(xiàn)場離奇詭異徊哑,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)议经,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谴返,“玉大人煞肾,你說我怎么就攤上這事∩じぃ” “怎么了籍救?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長渠抹。 經(jīng)常有香客問我蝙昙,道長,這世上最難降的妖魔是什么梧却? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任奇颠,我火速辦了婚禮,結(jié)果婚禮上放航,老公的妹妹穿的比我還像新娘烈拒。我一直安慰自己,他們只是感情好广鳍,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布荆几。 她就那樣靜靜地躺著,像睡著了一般赊时。 火紅的嫁衣襯著肌膚如雪吨铸。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天蛋叼,我揣著相機(jī)與錄音焊傅,去河邊找鬼。 笑死狈涮,一個(gè)胖子當(dāng)著我的面吹牛狐胎,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播歌馍,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼握巢,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了松却?” 一聲冷哼從身側(cè)響起暴浦,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤溅话,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后歌焦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體飞几,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年独撇,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了屑墨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,852評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡纷铣,死狀恐怖卵史,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情搜立,我是刑警寧澤以躯,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站啄踊,受9級特大地震影響忧设,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜社痛,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一见转、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蒜哀,春花似錦、人聲如沸吏砂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽狐血。三九已至淀歇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間匈织,已是汗流浹背浪默。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留缀匕,地道東北人纳决。 一個(gè)月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像乡小,于是被迫代替她去往敵國和親阔加。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容