UITableViewCell點(diǎn)擊展開/關(guān)閉

第一種方法:

先說原理:
1.在cell中定義一個(gè)屬性:isSelected用來標(biāo)記cell的選中狀態(tài);
2.在Controller中定義一個(gè)屬性:lastCell,標(biāo)記上次選中的cell, 再定義另外一個(gè)屬性:selectedIndexPath,用來標(biāo)記當(dāng)前選中的cell的索引;
3.在Controller的返回cell的代理方法中,判斷cell的isSelected狀態(tài),根據(jù)狀態(tài)返回不同的cell樣式(展開或折疊樣式);
4.在返回高度的方法中同樣的根據(jù)狀態(tài)返回高度;
5.在didSelectedRowAtIndexPath代理方法中,先獲取到當(dāng)前點(diǎn)擊的cell,再判斷本次點(diǎn)擊的cell是否是之前選中的cell(即lastCell);如果不是,則說明點(diǎn)擊了一個(gè)新的cell,則將上次點(diǎn)擊的cell的選中狀態(tài)置為NO (lastCell.isSelected = NO); 再將當(dāng)前點(diǎn)擊的cell的isSelected狀態(tài)取反;
如果本次點(diǎn)擊的cell還是上次點(diǎn)擊的cell,則直接將當(dāng)前cell的isSelected狀態(tài)取反即可; 之后刷新tabelView, 最后將本次點(diǎn)擊的cell賦給lastCell;

代碼:
//dataSource方法中
//返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = nil;
    XCommodity *producte = self.selectedCommodities[indexPath.row];
    
    if (producte.isSelected){
        cell = [tableView dequeueReusableCellWithIdentifier:selectedCommodityCellIdenifier1 forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:selectedCommodityCellIdenifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
//返回高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCommodity *cc =self.selectedCommodities[indexPath.row];
    if(cc.isSelected) {
        return 164;
    }
    return 95;
}
//delegate方法中
-(void)reset {
    if (self.lastCommodity) {
        self.lastCommodity.isSelected = NO;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XCommodity *producte = self.selectedCommodities[indexPath.row];
    if (self.lastCommodity != producte ) {
        [self reset];
    }
  
    self.tableViewSelecteRow = indexPath.row;
    if (producte.isSelected) {
         producte.isSelected = NO;
    } else {
         producte.isSelected = YES;
    }
    [tableView reloadData];
    self.lastCommodity = producte;
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

第二種方法:

<這種方法有缺陷:點(diǎn)擊同一個(gè)cell,第一次點(diǎn)擊展開,再點(diǎn)擊無法折疊>
直接貼代碼吧

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //先獲取到對(duì)應(yīng)的model
    XEntityTicketModel *model = self.selectedEntityArr[indexPath.row];
    if (self.selectedIndexPath && indexPath.row == self.selectedIndexPath.row) {
        //展開
        XSelectedEntityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectedEntityCell" forIndexPath:indexPath];
        [cell configureSelectedCellWithModel:model];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //事件響應(yīng)
        [cell.cancelBtn addTarget:self action:@selector(cancleBtnActon:) forControlEvents:UIControlEventTouchUpInside];
        [cell.reduceBtn addTarget:self action:@selector(reduceBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.increaseBtn addTarget:self action:@selector(increaseBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
    } else {
        //收起
        XUnselectedEntityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"unSelectedCell" forIndexPath:indexPath];
        [cell configureUnselectedCellWithModel:model];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}
//返回高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectedIndexPath && indexPath.row == self.selectedIndexPath.row) {
            return 165;
    }
    return 90;
}

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

    //記錄下選中的索引
    self.selectedIndexPath = indexPath;
    [self.tableView reloadData];
}

以上,OVER.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,765評(píng)論 25 709
  • 1,Search Bar 怎樣去掉背景的顏色(storyboard里只能設(shè)置background顏色,可是發(fā)現(xiàn)cl...
    以德扶人閱讀 2,874評(píng)論 2 50
  • 欲望是無止盡的,而生命是有限的 精力是有限的,用在刀刃上 偷 偷 偷 偷時(shí)間,偷精力,偷知識(shí),偷財(cái)富
    查蘇的吉古勒閱讀 215評(píng)論 0 1
  • 我手機(jī)出現(xiàn)微信,qq不打開程序接收不到信息,打開之后,上方會(huì)顯示無法連接服務(wù)器,等一會(huì)才恢復(fù)正常,才能接收到信息。...
    大叔老了閱讀 209評(píng)論 0 0

友情鏈接更多精彩內(nèi)容