轉(zhuǎn)載:
http://www.2cto.com/kf/201308/238449.html
http://www.itnose.net/detail/6154013.html
UITableView繼承自UIScrollviUITableViewCell,可以讓UITableViewCell響應(yīng)一些點(diǎn)擊事件,也可以在UITableViewCell中加入
UITextField或者UITextView等子視圖障斋,使得可以在cell上進(jìn)行文字編輯。
UITableView中的cell可以有很多致燥,一般會通過重用cell來達(dá)到節(jié)省內(nèi)存的目的:通過為每個cell指定一個重用標(biāo)識符(reuseIdentifier),即指定了單元格的種類,當(dāng)cell滾出屏幕時,會將滾出屏幕的單元格放入重用的queue中瓤檐,當(dāng)某個未在屏幕上的單元格要顯示的時候赂韵,就從這個queue中取出單元格進(jìn)行重用。
但對于多變的自定義cell挠蛉,有時這種重用機(jī)制會出錯祭示。比如,當(dāng)一個cell含有一個UITextField的子類并被放在重用queue中以待重
用谴古,這時如果一個未包含任何子視圖的cell要顯示在屏幕上质涛,就會取出并使用這個重用的cell顯示在無任何子視圖的cell中,這時候就會出錯掰担。
解決方法:
方法1 將獲得cell的方法從-
(UITableViewCell*)dequeueReusableCellWithIdentifier:
(NSString*)identifier 換為
-(UITableViewCell
*)cellForRowAtIndexPath:(NSIndexPath *)indexPath
重用機(jī)制調(diào)用的就是dequeueReusableCellWithIdentifier這個方法汇陆,方法的意思就是“出列可重用的cell”,因而
只要將它換為cellForRowAtIndexPath(只從要更新的cell的那一行取出cell)带饱,就可以不使用重用機(jī)制毡代,因而問題就可以得到解
決,雖然可能會浪費(fèi)一些空間勺疼。
示例代碼:
[plain]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改為以下的方法
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根據(jù)indexPath準(zhǔn)確地取出一行教寂,而不是從cell重用隊(duì)列中取出
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//...其他代碼
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改為以下的方法
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根據(jù)indexPath準(zhǔn)確地取出一行,而不是從cell重用隊(duì)列中取出
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//...其他代碼
}
方法2 通過為每個cell指定不同的重用標(biāo)識符(reuseIdentifier)來解決执庐。
重用機(jī)制是根據(jù)相同的標(biāo)識符來重用cell的酪耕,標(biāo)識符不同的cell不能彼此重用。于是我們將每個cell的標(biāo)識符都設(shè)置為不同轨淌,就可以避免不同cell重用的問題了迂烁。
示例代碼:
[plain]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath來唯一確定cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//...其他代碼
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath來唯一確定cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//...其他代碼
}
方法3 刪除重用cell的所有子視圖
這個方法是通過刪除重用的cell的所有子視圖,從而得到一個沒有特殊格式的cell递鹉,供其他cell重用盟步。
示例代碼:
[plain]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else
{
//刪除cell的所有子視圖
while ([cell.contentView.subviews lastObject] != nil)
{
[(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
}
}
//...其他代碼
}