我有一個在故事板中創建的原型單元格,所有標記都能正常工作/顯示。我想創建一個重複的原型單元格,但改變了一些可視元素,這樣我就可以在一個表格中顯示原始和新的原型單元格。我在故事板中複製了原有的原型單元,並給它一個新的標識符,並重新設計它,使其看起來像新的單元格。但是,在我的新單元格中,其中一個視圖正在初始化爲nil(subView)。我真的不明白爲什麼會出現這種情況,因爲這個視圖在舊的原型單元格中正確加載,但它不會在新單元格中初始化(即使它具有相同的viewWithTag數字)。所有我一直在做閱讀,我相信這與整個小區重用的想法去做,但我不能查明問題...UITableViewCell - 顯示viewWithTag = nil的多個單元格標識符
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *AnnouncementCellIdentifier = @"AnnouncementCell";
static NSString *EventCellIdentifier = @"EventCell";
static NSString *CellIdentifier = @"TBD";
if(self.segmentedControl.selectedSegmentIndex == 0){
GenericPost *announcement = [self.announcements objectAtIndex:indexPath.section];
if ([announcement isMemberOfClass: [Event class]]){
CellIdentifier = EventCellIdentifier;
}
else {
CellIdentifier = AnnouncementCellIdentifier;
}
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSLog(@"CellIdentifier: %@",CellIdentifier);
if (!cell) { // THIS IS NEVER CALLED JUST AS AN FYI
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIView *contentView = (UIView *)[cell viewWithTag: 0];
UIView *subView = (UIView *)[cell viewWithTag:0];
if(self.segmentedControl.selectedSegmentIndex == 0){
GenericPost *announcement = [self.announcements objectAtIndex:indexPath.section];
contentView = (UIView *)[cell viewWithTag: 100];
subView = (UIView *)[cell viewWithTag:101]; // THIS LINE RIGHT HERE SHOWS MY SUBVIEW IS INITIALIZED TO NIL WHEN CELLIDENTIFIER = EventCell, yet it works fine for AnnouncementCell
打印輸出:
2014-03-03 19:17:47.480 MyApp[23213:70b] CellIdentifier: EventCell
2014-03-03 19:17:47.481 MyApp[23213:70b] view is a UITableViewCell with tag = 0
2014-03-03 19:17:47.481 MyApp[23213:70b] view is a UITableViewCellScrollView with tag = 0
2014-03-03 19:17:47.482 MyApp[23213:70b] view is a UITableViewCellContentView with tag = 100
2014-03-03 19:17:47.482 MyApp[23213:70b] view is a UIView with tag = 106
2014-03-03 19:17:47.482 MyApp[23213:70b] view is a UISegmentedControl with tag = 107
2014-03-03 19:17:47.482 MyApp[23213:70b] view is a UISegment with tag = 0
2014-03-03 19:17:47.483 MyApp[23213:70b] view is a UISegmentLabel with tag = 0
2014-03-03 19:17:47.483 MyApp[23213:70b] view is a UIImageView with tag = -1030
2014-03-03 19:17:47.483 MyApp[23213:70b] view is a UISegment with tag = 0
2014-03-03 19:17:47.484 MyApp[23213:70b] view is a UISegmentLabel with tag = 0
2014-03-03 19:17:47.484 MyApp[23213:70b] view is a UIImageView with tag = -1030
2014-03-03 19:17:47.484 MyApp[23213:70b] view is a UISegment with tag = 0
2014-03-03 19:17:47.485 MyApp[23213:70b] view is a UISegmentLabel with tag = 0
2014-03-03 19:17:47.485 MyApp[23213:70b] view is a UIImageView with tag = -1030
2014-03-03 19:17:47.485 MyApp[23213:70b] view is a UIView with tag = 999
2014-03-03 19:17:47.486 MyApp[23213:70b] view is a UILabel with tag = 0
2014-03-03 19:17:47.486 MyApp[23213:70b] view is a UILabel with tag = 0
2014-03-03 19:17:47.486 MyApp[23213:70b] view is a UILabel with tag = 0
2014-03-03 19:17:47.487 MyApp[23213:70b] view is a UILabel with tag = 0
2014-03-03 19:17:47.487 MyApp[23213:70b] view is a UILabel with tag = 0
2014-03-03 19:17:47.487 MyApp[23213:70b] view is a UIScrollView with tag = 103
2014-03-03 19:17:47.487 MyApp[23213:70b] view is a UITextView with tag = 104
2014-03-03 19:17:47.488 MyApp[23213:70b] view is a _UITextContainerView with tag = 0
2014-03-03 19:17:47.488 MyApp[23213:70b] view is a UITextSelectionView with tag = 0
不應該是'UIView * subview = [cell.contentView viewWithTag:0];'? – Can
另外'if(!cell)'不會被調用,因爲這是舊的方法。在iOS 6中,他們添加了您使用'dequeueReusableCellWithIdentifier:forIndexPath:'的方法,該方法會自動創建單元格,如果它不存在。 – Can
你確定兩個原型單元格都有標籤== 101的子視圖嗎?搜索標籤== 0通常會產生意想不到的結果,如果程序員忘記接收者(除了它的子視圖)可以找到。即如果單元格的標記爲零,則會將子視圖指定爲單元格,而不是任何子視圖。 – danh