溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何實(shí)現(xiàn)iOS中Cell的Section展開和收起

發(fā)布時間:2021-07-21 09:23:12 來源:億速云 閱讀:568 作者:小新 欄目:移動開發(fā)

小編給大家分享一下如何實(shí)現(xiàn)iOS中Cell的Section展開和收起,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

首先,先上圖,讓大家看看效果

 如何實(shí)現(xiàn)iOS中Cell的Section展開和收起

相信大家對于TableViewd數(shù)據(jù)的設(shè)置都熟悉,這方面就不多說的,重點(diǎn)的還是來看:

1.如何實(shí)現(xiàn)cell的Section的展開和收起的效果

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [self.tableView deselectRowAtIndexPath:indexPath animated:NO];


  currentRow = indexPath.row;

  NSDictionary *sectionDic = self.dataSource[indexPath.section];
  NSArray *cellArray = sectionDic[@"sub"];

  //cell當(dāng)前的數(shù)據(jù)
  NSDictionary *cellData = cellArray[indexPath.row];

  NSString *key = [NSString stringWithFormat:@"%@", cellData[@"chapterID"]];
  CellModel *chapterModel = [self.cellOpen valueForKey:key];

  chapterModel.isShow = !chapterModel.isShow;

  [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

當(dāng)用戶點(diǎn)擊到某一個cell時候,需要判斷cell是否是展開狀態(tài),如果張開或者收起就調(diào)用

復(fù)制代碼 代碼如下:


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

讓cell的section能夠重新加載刷新;

2.如何添加cell的Section

2.1設(shè)置section的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSDictionary *sectionDic = self.dataSource[indexPath.section];
  NSArray *cellArray = sectionDic[@"sub"];
  //cell當(dāng)前的數(shù)據(jù)
  NSDictionary *cellData = cellArray[indexPath.row];

  NSString *key = [NSString stringWithFormat:@"%@", cellData[@"chapterID"]];
  CellModel *model = [self.cellOpen valueForKey:key];
  if (model.isShow) {
    return (model.pois.count+1)*60;
  } else {
    return 60;
  }

}

上面的代碼是設(shè)置section的高度,主要是以cell的isshow作為標(biāo)記,讓section的能夠隨數(shù)據(jù)的改變而變動

3.如果要在一個cell上再加一個cell,實(shí)現(xiàn)cell內(nèi)嵌cell,需要在哪里加?

答案:當(dāng)然是在cell的HeaderSection或者FooterSection上加上cell,這樣就能實(shí)現(xiàn)cell內(nèi)嵌cell。

看完了這篇文章,相信你對“如何實(shí)現(xiàn)iOS中Cell的Section展開和收起”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI