有關UITableView--cell復用問題

?

近來用Tableview做了一個九宮格。過程中碰到了兩個cell復用問題。

問題一:

在cell中為button添加addTarget點擊事件時,出現后面的cell會重疊它前面cell的事件。代碼如下:

C代碼??收藏代碼
static NSString *CellWithIdentifier = @"DiscoverHomeTableViewCell";  DiscoverHomeTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier forIndexPath:indexPath];  cell1.delegate = self;  [cell1.btnMore addTarget:self action:@selector(btnMoreDisplay) forControlEvents:UIControlEventTouchUpInside];  cell1.labTitle.text = @"熱門";  cell1.listExhibit = _homeVo.listExhibit;  cell1.dType = D_TYPE_1;  cell1.navigationController = self.navigationController;  [cell1.tableView reloadData];  return cell1;

?

??

經過調試確實是復用了之前cell的事件。在此用協議代理可解決這一問題,用協議來進行處理點擊事件。

C代碼??收藏代碼
#pragma mark DiscoverHomeTableViewCellDelegate  
- (void)ActionWithTap:(NSString *)type withData:(id)data{  if ([type isEqualToString:D_TYPE_1]) {  [self btnMoreDisplay];  }  
}  

?

?

問題二:

在UITableViewCell中,進行手寫代碼方式添加控件,這時在cell復用時,會出現重疊控件及控件中的內容。因為每一個cell都是重新添加的,前面的cell會覆蓋在后面的cell上。于是強制對cell中添加的控件進行了清空,cell復用不變,只是新的cell加載前,都會對上一個cell的內容進行清空。代碼如下:

C代碼??收藏代碼
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  static NSString *CellIdentifer = @"DiscoverHomeInnerTableViewCell";  DiscoverHomeInnerTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:CellIdentifer forIndexPath:indexPath];  //TODO 解決cell復用重疊問題  for (UIView *subview in [cell.contentView subviews]) {  [subview removeFromSuperview];  }  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];  
//button相關設置  
[cell.contentView addSubview:button];  
UILabel *lab = [[UILabel alloc] init];  
//lab相關設置  
[cell.contentView addSubview:lab];

?

?

轉載于:https://www.cnblogs.com/henusyj-1314/p/5984500.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542369.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542369.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542369.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python客戶端和服務端實驗_結合服務器和客戶端python

我正在嘗試使用python(稍后可能用c語言)和TCP套接字制作一個本地網絡聊天程序。我的目的是讓服務器監聽當前計算機的地址以獲取傳入消息,并將這些消息轉發給客戶端(我現在還不確定)。客戶端將是一個簡單的gui,可以通過本地連接向活動服務器發送消息。實際…

python常用語法和示例_C語言切換案例教程,語法,示例和規則

python常用語法和示例使用默認情況下的決策 (Decision making using switch-case-default) Many times in our daily lives, we face conditions where we are required to choose between a number of alternatives rather than just two or three. For example, which school…

android so abi適配,Android NDK學習(六): so文件兼容之abiFilters的使用

最近項目中遇到了要使用JavaCV的情況,涉及到了abi兼容的選擇。因為如果全部都適配的話,包很大,這樣兼容那些用戶數極少的cpu就很不劃算,所以我只適配了armeabi-v7a這一個。但是今天在x64-v8a的模擬器上看的時候,提示我…

python中doc=parased.getroot()_python中執行sed命令操作源文件時出現錯誤

我想在python中執行一個sed命令,第一種方法直接指定文件時,可以正確輸出結果,但是第二種我打開文件操作的時候就有問題,不知道什么原因,求高手解答?(1)>>> sedcmd"sed -n \s/{//g; p\ /qye/p…

JavaScript基礎之Number對象和Math對象

2019獨角獸企業重金招聘Python工程師標準>>> //Math對象//屬性float Math.E; //返回自然對數的底數e,約2.718float Math.LN2; //返回2的自然對數,約0.693float Math.LN10; //返回10的自然對數,約2.302fl…

c++ stl 獲取最小值_如何在C ++ STL中找到向量的最小/最小元素?

c stl 獲取最小值Given a vector and we have to minimum/smallest element using C STL program. 給定一個向量,我們必須使用C STL程序最小/最小元素。 尋找向量的最小元素 (Finding smallest element of a vector) To find a smallest or minimum element of a …

android studio panic,Android Studio模擬器PANIC錯誤

Android Studio模擬器突然停止工作.當我嘗試運行虛擬設備時,我在事件日志中收到以下錯誤.模擬器:PANIC:找不到AVD系統路徑.請定義ANDROID_SDK_ROOT仿真器:處理完成,退出代碼為1所以我檢查了ANDROID_SDK_ROOT環境變量設置的值,它是空的.所以我把它設置為/Users/{username}/Libra…

linux特殊權限之訪問權限

特殊權限如/etc/passwd:sSuid:普通用戶以管理員身份運行命令(chmod us FILE、chmod u-s FILE)如果FILE本身原來就有執行權限,SUID顯示為s;否則顯示SSgid:基本組以管理組身份運行命令(chmod gs FILE、chmod g-s FILE&am…

vb.net變量值變化觸發事件_Angular變化檢測的理解

獲取臟檢查的時機Angular 使用NgZone獲取變化的通知,然后進行全面的變化檢測,進而更新Dom臟檢查的過程Angular的數據流是自頂而下,從父組件到子組件單項流動,單項數據流保證了高效可預測的變化檢測。盡管檢查了父組件之后&#xf…

python 算術右移_Python算術序列| 競爭編碼問題

python 算術右移Question: 題: In mathematics, when in an arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant then it is called arithmetic constant. 在數學中,當在算術序列中是…

Android8內測申請,小米 6 安卓 8.0 來了 內測開始招募

Android 8.0 已經正式發布多時,目前不少廠商已經啟動了旗下進行的 Android 8.0 適配計劃。但令人納悶的是,一向對系統升級比較熱心的小米卻遲遲沒有動靜。好消息是,此前網友曝光的消息顯示,MIUI 已經悄然在官方論壇中招募小米 6 的…

My linux

為什么80%的碼農都做不了架構師?>>> 1.linux 命令方式修改機器名稱 # hostname newHostName # vi /etc/sysconfig/network 修改或增加配置:hostnamenewHostName # vi /etc/hosts 修改對應的本地HOST映射 xx.xxx.xxx.xxx newHostName 2.Redha…

狂神說es筆記_人教版七上英語Unit5電子課本音頻+課堂筆記+課后同步習題

1人教 七上英語Unit5單詞七年級英語上冊Unit 5單詞默寫1做;干(助動詞)__________2做,干(助動詞第三人稱單數形式)__________3有__________4網球__________5球__________6乒乓球______7球棒;球拍__________8(英式)足球____________________9排…

Java RandomAccessFile getFilePointer()方法與示例

RandomAccessFile類getFilePointer()方法 (RandomAccessFile Class getFilePointer() method) getFilePointer() method is available in java.io package. getFilePointer()方法在java.io包中可用。 getFilePointer() method is used to get the current pointer in the Rando…

先進技術android,React Native實戰(JavaScript開發iOS和Android應用)/計算機科學先進技術譯叢...

導語內容提要本書作者Nader Dabit是AWS Mobile開發人員、React Native Training創始人和React Native Radio播客主持人。本書旨在幫助iOS、Android和Web開發人員學習使用React Native框架,構建高質量的iOS和Android應用程序。書中介紹了React Native入門基礎知識&am…

開發類似vs的黑色風格_傳聞:2020年《使命召喚》將是《黑色行動》重啟作品

據可信度較高的消息源透露,2020 年的《使命召喚》將是《黑色行動》的重啟作。而據之前的報道,《黑色行動》開發商 Treyarch 正在開發今年的《使命召喚》, Sledgehammer Games 和 Raven Software 負責輔助工作。該項目代號為“宙斯”&#xff…

ubuntu中 不同JDK版本之間的切換

Ubuntu中JDK 的切換前提是同時安裝了多個版本,如jdk7和jdk8,若要切換,在終端輸入: sudo update-alternatives --config javasudo update-alternatives --config javac

osi模型:七層模型介紹_聯網| OSI模型能力問題和解答 套裝1

osi模型:七層模型介紹1) There are the following statements that are given below, which of them are correct about the OSI model? The OSI model is a reference model that describes the network functionalities.The OSI model is an implemented model that describ…

華為鴻蒙系統正式登場,華為自研鴻蒙系統將于8月9日正式登場,還有全新的鴻鵠芯片...

最近華為發布了很多手機:榮耀20系列手機、榮耀9X系列、華為Nova 5系列,以及7月26日發布的華為Nova5i Pro和華為首部5G手機Mate20 X 5G版,這些手機將成為華為下半年的出貨主力,市場份額能否超過50%就看這些手機的表現了。華為還將在…

pythonencode和decode_Python3的decode()與encode()

python3的decode()與encode()Tags: Python Python3對于從python2.7過來的人,對python3的感受就是python3對文本以及二進制數據做了比較清晰的區分。文本總是Unicode,由str類型進行表示,二進制數據使用bytes進行表示,不會將str與bytes偷偷的混…