2014-09-24 37 views
0

我正在創建自定義UITableViewCell。在ios7中這工作得很好,但在iOS8中,我所有的子類屬性突然記錄爲零。自定義UITableViewCell突然記錄所有屬性值爲零

這是我的代碼:

if(indexPath.section == 0){ 
NSString *headerIdentifier = @"HeaderCell"; 

HeaderTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:headerIdentifier]; 

if (!cell) { 
    cell = [[HeaderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:headerIdentifier]; 
} 
NSLog(@"headercell: %@",cell); // this logs a neat uitableviewcell 
cell.profileImage.image = [UIImage imageNamed:@"profilePlaceholder.jpg"]; 
CALayer * l = [cell.profileImage layer]; 
[l setMasksToBounds:YES]; 
[l setCornerRadius:33.0]; 

cell.settingsView.image = [UIImage imageNamed:@"settings.png"]; 
NSLog(@"%@",cell.settingsView.image); //logs nil :( 
CALayer *settings = [cell.settingsView layer]; 
[settings setCornerRadius:12.0]; 
[settings setMasksToBounds:YES]; 
} 

當我打電話cell.textLabel = @"TEST",該單元格顯示TEST不過..

我已經把我的班,我的故事板權和進口自定義的頭文件細胞也是如此。我不知道爲什麼它不能寫入這些屬性。

編輯

至於那種Acey的意見建議,這可能是因爲我的故事板無法識別的標識,但實際上我設置是正確的。在此處查看屏幕截圖:

回答

0

特別是在故事板中,您應該使用較新的dequeueReusableCellWithIdentifier:forIndexPath:方法。如果使用正確的重用標識符在故事板中設置單元格,那麼您將始終返回單元格。

我相信問題是你在單元上調用alloc/init,所以它永遠不會正確地從xib/storyboard加載它。

+0

感謝您的回答1 :)我嘗試了您建議的方法,並得到一個運行時錯誤:''無法使標識符HeaderCell出列單元格 - 必須爲標識符註冊一個nib或類或連接故事板中的原型單元格「這是iOS8以來的新功能嗎?我無法將自定義的'UITableViewCell'連接到ViewController,因爲這是一個'非法配置' – bdv 2014-09-24 17:54:59

+0

當試圖修復它,這個答案(http://stackoverflow.com/questions/19084274/xcode-unable-to-dequeue- a-cell-with-identifier)指向相反的方向。 – bdv 2014-09-24 18:06:33

+0

啊哈。正如我懷疑的那樣,你從未註冊過你的手機。不,這不是新的。在製作單元格的故事板中,確保標識符與代碼中的相同。另外,如果要在故事板中創建單元格,請確保您在包含單元格的表格視圖中調用出列。在故事板中創建原型單元格會自動爲您註冊,但標識符必須匹配。 – Acey 2014-09-24 18:09:08