2010-09-08 100 views
0

在我的程序,當我創建一對夫婦從筆尖文件加載定製UIViewCells的:EXC_BAD_ACCESS滾動的TableView

[[NSBundle mainBundle] loadNibNamed:@"CustomCells" owner:self options:nil]; 

一旦他們加載我設置起來,並從函數返回:

if (indexpath.row == 1) { 
    [nibTextInputer setupWithName:@"notes" ...]; 
    return nibTextInputer; 
} else { 
    [nibSelectInputer setupWithName:@"your_choice" ...]; 
    return nibSelectInputer; 
}; 

其中nibTextInputer是我的班級(AFTextInputer)和nibSelectInputer是我的其他班級(AFTextInputer)。這兩個類都來自UITableViewCell的子類。

這一切工作正常,但休息時,我添加緩存到:

Boolean inCache = false; 
if (indexPath.row == 1) { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nibTextInputer"]; 
    if (cell != nil) { 
     NSLog(@"%@", [cell description]); // prints out ok, correct type. 
     nibTextInputer = (AFTextInputer*) cell; 
     inCache = true; 
    }; 
}; 

if (!inCache) { 
    [[NSBundle mainBundle] loadNibNamed:@"CustomCells" owner:self options:nil]; 
} 

一旦我添加上面EXC_BAD_ACCESS開始出現在隨機的地方,通常沒有額外的信息,有時與此錯誤:

-[CALayer prepareForReuse]: unrecognized selector sent to instance 

甚至

-[UIImage prepareForReuse]: unrecognized selector sent to instance 

EXC_BAD_ACCES的位置S看起來是隨機的。有時,它的「出列」之後,有時是郊外的功能..

我想這個問題就在於我的實現定製UIViewCells之內,但我不知道從哪裏開始尋找..

想法?

回答

2

你有一個過度釋放發生在你的UITableViewCell-[UITableViewCell prepareForReuse]在從-[UITableView dequeueReusableCellWithIdentifier:]返回之前被調用,但是當它被調用時,單元不再存在,而是CALayer,UIImage或您無權訪問的內容。

問題可能出在您加載自定義單元格的方式上。對於它的價值,這是我通常如何做到這一點:

static NSString *CellIdentifier = @"CustomCell"; // This string should also be set in IB 

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
    cell = nibCell; // nibCell is a retained IBOutlet which is wired to the cell in IB 
} 

// Set up the cell... 
0

這可能是你要去的地方碰到的問題:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nibTextInputer"]; 

的的UITableView類腫塊所有的細胞進入重複使用相同的池;它不知道某些單元是一種子類(即AFTextInputer),有些單元是另一種子類(即AFTextInputer)。因此,當您在if (indexPath.row == 1)塊中取出某個單元格時,您可能會遇到錯誤的子類型單元格。 「標識符」只是一個字符串,用於向內置高速緩存指示您所引用的表單元格;它實際上並不使用該字符串挖掘緩存以找到具有匹配子類名稱的對象。

P.S.爲什麼使用名爲Boolean的類型而不是「內置」BOOL

+0

這就是..CellWithIdentifier:用於 - 標識符(「nibTextInputer」)在nib文件中設置,並且我通過NSLogging單元格類型進行雙重檢查。 – kolinko 2010-09-08 17:25:10

+0

我不知道有什麼區別wetween布爾和布爾..我會研究,謝謝。 – kolinko 2010-09-08 17:25:44