點(diǎn)擊UITableView的cell展開收縮-創(chuàng)新互聯(lián)

首先要理解UITableView代理方法調(diào)用的先后順序。

創(chuàng)新互聯(lián)專注于網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點(diǎn)出發(fā),讓客戶在網(wǎng)絡(luò)營銷中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴(yán)謹(jǐn)?shù)膽B(tài)度對待客戶,用專業(yè)的服務(wù)創(chuàng)造價(jià)值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。

當(dāng)初始化UITableView后,代理回調(diào)順序如下

1://返回cell個(gè)數(shù)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

2://返回每行的高度

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

3://請求數(shù)據(jù)元代理為tableView插入需要的cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

4://監(jiān)聽點(diǎn)擊的cell

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

需要聲明一個(gè)全局BOOL變量isOpen,記錄當(dāng)前cell的狀態(tài),聲明一個(gè)NSInterge類型selectedIndex,記錄選擇的cell的row。

在heightForRowAtIndexPath代理里面實(shí)現(xiàn)//選中狀態(tài)返回的高度

if (indexPath.row == selectedIndex.row && selectedIndex != nil ) {

if (isOpen == YES) {

//cell上的label高度自適應(yīng)

CGSize size = [textStr sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(290, 1000) lineBreakMode:NSLineBreakByWordWrapping];

CGFloat f = size.height;

if (indexPath.row == [self.dataArr count]-1){

return 153.8+(f - 21);

}

return 155+(f - 21);

}else{

return 67;

}

}

同樣在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath里實(shí)現(xiàn)一樣的條件

if (indexPath.row == selectedIndex.row && selectedIndex != nil) {

//如果是展開

if (isOpen == YES) {

//xxxxxx

}else{

//收起

}

//不是自身

} else {

}

當(dāng)點(diǎn)擊時(shí)候在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//將索引加到數(shù)組中

NSArray *indexPaths = [NSArray arrayWithObject:indexPath];

//判斷選中不同row狀態(tài)時(shí)候

// if (self.selectedIndex != nil && indexPath.row != selectedIndex.row) {

if (self.selectedIndex != nil && indexPath.row == selectedIndex.row) {

//將選中的和所有索引都加進(jìn)數(shù)組中

// indexPaths = [NSArray arrayWithObjects:indexPath,selectedIndex, nil];

isOpen = !isOpen;

}else if (self.selectedIndex != nil && indexPath.row != selectedIndex.row) {

indexPaths = [NSArray arrayWithObjects:indexPath,selectedIndex, nil];

isOpen = YES;

}

//記下選中的索引

self.selectedIndex = indexPath;

//刷新

[tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];

}

經(jīng)過不斷調(diào)試,終于實(shí)現(xiàn)了點(diǎn)擊任意一個(gè)cell展開收縮效果

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

標(biāo)題名稱:點(diǎn)擊UITableView的cell展開收縮-創(chuàng)新互聯(lián)
分享地址:http://muchs.cn/article6/djhsig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站排名、動態(tài)網(wǎng)站ChatGPT、服務(wù)器托管靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管