2014-03-03 90 views
0

我有一個在故事板中創建的原型單元格,所有標記都能正常工作/顯示。我想創建一個重複的原型單元格,但改變了一些可視元素,這樣我就可以在一個表格中顯示原始和新的原型單元格。我在故事板中複製了原有的原型單元,並給它一個新的標識符,並重新設計它,使其看起來像新的單元格。但是,在我的新單元格中,其中一個視圖正在初始化爲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 
+0

不應該是'UIView * subview = [cell.contentView viewWithTag:0];'? – Can

+0

另外'if(!cell)'不會被調用,因爲這是舊的方法。在iOS 6中,他們添加了您使用'dequeueReusableCellWithIdentifier:forIndexPath:'的方法,該方法會自動創建單元格,如果它不存在。 – Can

+0

你確定兩個原型單元格都有標籤== 101的子視圖嗎?搜索標籤== 0通常會產生意想不到的結果,如果程序員忘記接收者(除了它的子視圖)可以找到。即如果單元格的標記爲零,則會將子視圖指定爲單元格,而不是任何子視圖。 – danh

回答

0

要查看您的視圖層次真象,請添加以下內容:

- (void)logTags:(UIView *)view indent:(NSInteger)indent { 

    NSLog(@"%*sview is a %@ with tag = %d", indent, "", view.class, view.tag); 

    for (UIView *subview in [view subviews]) { 
     [self logTags:subview indent:indent+4]; 
    } 
} 

然後,出列單元格後,登錄該單元格。 (選擇一行來限制輸出量)

UITableViewCell *cell = // .... 

if (indexPath.row == 0) { 
    [self logTags:cell indent:0]; 
} 
+0

非常有趣!所以它花了一點才意識到我需要添加引號來使函數起作用 ,indent,「」,view.class,view.tag 但有趣的是我的子視圖存在,但它有標籤= 999!我改變了我的viewWithTag = 999,一切正常,但我不知道如何使它與我想分配的標籤號碼正確工作?我搜索了我的代碼,無處可以編程地將我的subView標籤分配到999,在故事板中它清楚地列爲等於101.再次感謝! – SimplyLearning

+0

對不起,我的空間不好。將編輯。在神祕的999上,我能想到的唯一方法就是通過代碼。您搜索了「999」的代碼?也許你在某個地方做數學? view.tag = 1000-1; ?? – danh

+0

仍然無法確定999從哪裏來,但我會繼續尋找。謝謝您的幫助! – SimplyLearning

相關問題