2013-08-28 99 views
16

這開始發生在藍色之外。任何想法:代碼:表格單元沒有被重用的索引路徑

CUSTOMCLASSNAME(我已經更換了實際的類名稱,因爲它包含了客戶端的名稱。)

正開始我的tableView:

[self.tableView registerClass:[CUSTOMCLASSNAME class] forCellReuseIdentifier:[self reuseIdentifier]]; 

在單元格爲行:

嗨,標題正在控制檯中打印。這是我的cellForRow:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    AVTCheckListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self reuseIdentifier] forIndexPath:indexPath]; 

    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 

- (void)configureCell:(AVTCheckListTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    ChecklistGroup *group = [self.checklistFetchedResultsController.fetchedObjects objectAtIndex:indexPath.section]; 

    ChecklistItem *item = [self getChecklistItemForIndexPath:indexPath]; 

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    [[cell cellTextView] setAttributedText:[[item checked] boolValue] ? [[NSAttributedString alloc] initWithString:[item name] attributes:@{ NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle) } ] : [[NSAttributedString alloc] initWithString:[item name]]]; 
    [[cell cellTextView] setUserInteractionEnabled:[[group isUserDefined] boolValue]]; 
    [[cell cellTextView] setTag:indexPath.row]; 
    [[cell cellTextView] setDelegate:self]; 
    [[cell tickImage] setHidden:![[item checked] boolValue]]; 
} 

//Method that returns re-use: 

- (NSString *) reuseIdentifier { 
    return @"CheckListTableView"; 
} 
+0

什麼開始發生了藍色的 – rdelmar

+0

喜誤差標題被重複使用的表格單元格沒有索引路徑。 表格視圖在滾動時會出現不穩定的行爲。直到所有的單元格被加載,但由於它們沒有被重用,這是一個問題。 – CW0007007

+0

根本不清楚你遇到了什麼問題。請在您的帖子中添加更多信息來解釋您的問題。如果你的應用程序拋出一個異常,也包括這一點。 – Aaron

回答

1

我幾天後工作了這一點。在我的自定義單元格我有一個TextView,當我將它添加到內容查看我這樣做:

[self.cellTextView setClearsOnInsertion:YES]; 

這是問題的原因;任何人都有類似的問題。

快樂編碼:-)

+2

任何人都可以解釋爲什麼這會導致問題? –

+0

我懷疑這是與表視圖單元出隊。但我無法確定。 – CW0007007

-1

可以嘗試以下的(前提是你沒有在該indexpath細胞中的任何特定配置): -

CustomClassName *cell=[tableView dequeueReusableCellWithIdentifier:[self reuseIdentifier]]; 
if(cell==nil) 
{ 
cell=[[CustomClassName alloc] initWithStyle:whatever_style reuseIdentifer:[self reuseIdentifier]]; 
} 

還請後你做了什麼錯誤如果上述不起作用。

也有看看下面的鏈接: -

What is the meaning of the "no index path for table cell being reused" message in iOS 6/7?

+0

相同的控制檯日誌。 – CW0007007

+0

編輯我的答案 – Max

+0

內部錯誤似乎不是一個好答案......不會與其他表格一起使用。 – CW0007007

7

我剛剛找到了此錯誤消息的原因。

A UITableViewCell被用作正常視圖。因此它的'indexPath未設置。在我們的情況下,正在通過viewForHeaderInSection返回:

希望這會有所幫助。

克里斯

+3

那麼你做了什麼來解決它? – shim

+4

我停止使用UITableViewCell作爲普通視圖 - 即我需要創建一個普通的視圖,我用UIView而不是UITableViewCell。 – chrisoneiota

59

返回[cell contentView]而不是celltableView:viewForHeaderInSection:

編輯:這在某種程度上導致了事故對UI按鍵長按手勢。爲了解決這個問題,我放棄了使用單個項目的另一部分作爲假部分標題。

+0

這解決了它對我來說。在我的情況下,它是viewForFooterInSection方法。謝謝! – GivP

+0

我可以確認,在此部分上長時間按下會導致應用程序崩潰 –

+1

但是,我最終所做的是將LongPressGesture添加到內容視圖中,這會阻止將LongPress事件傳遞給已經解除分配的Cell正在返回內容視圖,這是導致崩潰並解決問題的原因。 –

1

那麼,我只是用了一天的好幾天試圖解決這個問題,所以希望這種替代解釋可以節省一些人的一些時間。

我有一個tableview,它在加載的時候有時會給出這個消息。事實證明,這是由加載視圖時觸發核心數據對象的KVO通知引起的。 (在觀察一個變化時,我的控制器試圖在桌面視圖上調用reloadData,通過在視圖完成加載之前不觀察對象來修復(之前,我通過訪問器分配了對象之後開始觀察對象)

TLDR:檢查是否可能試圖從主線程以外的地方重新加載數據。

更妙的是:

這是你會不會碰上,如果你(因爲我發現你真的應該)使用兒童託管對象上下文,只有合併更改回對你的主要MOC有問題主線在明智的時候。

2

就我而言,此錯誤僅在iPhone(不是iPad)上提供,因爲我已將[self.tableView beginUpdates];及更高版本[self.tableView endUpdates];放入[UIView transitionWithView:duration:options:animations:compeletion];之內。爲了解決這個問題,我只是刪除了開始/結束更新,因爲我更喜歡淡入淡出的動畫,而不是淡入淡出,加上動畫細胞高度的變化。

0

我在tableView:viewForHeaderInSection中返回了UITableViewCell,但返回[cell contentView]導致單元格內的任何按鈕崩潰,並且在單元格周圍設置視圖包裝看起來很浪費。相反,我把我的UITableViewCell課改爲UITableViewHeaderFooterView,它的功能就像一個魅力!

如果你得到這個問題:

設置上UITableViewHeaderFooterView背景顏色已被棄用。請改用contentView.backgroundColor。

只要記住將您的筆尖上的背景顏色更改爲默認值。

1

如果您使用單元格作爲標題視圖節標題,則必須將其放入容器中,然後返回容器視圖以防止「表格單元格沒有被重用的索引路徑」。示例代碼如下。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    CustomerCellHeader *headerViewCell = [tableView dequeueReusableCellWithIdentifier:kCustomerCellHeaderIdentifier]; 
    headerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    [headerView addSubview:headerViewCell]; 
    return headerView; 
} 
+0

初始化視圖的代碼在哪裏? –

0

我有同樣的問題。當我滾動並重用單元格時出現此消息。

回答了很多關於viewForHeaderInSection的問題,但我沒有使用它們。如果有人有類似的問題,我特此表明我的解決方案。

我將觀察者添加到每個單元格內的文本字段。我想知道這是否是導致這個問題的觀察者,因爲我在回收細胞時沒有移除觀察者。

所以我刪除了觀察者,當一個細胞是deinited。

看來問題解決了。我無法保證。

10

我在執行viewForHeaderInSection期間發現了此警告。

這是我在斯威夫特2.X解決方案:

let headerView = self.tableView.dequeueReusableCellWithIdentifier("MyCell") as! MyCell 
let containerView = UIView(frame:headerView.frame) 
headerView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] 
headerView.myLabel.text = "section title 1" 
containerView.addSubview(headerView) 
return containerView 

這是斯威夫特3版本:

let headerView = self.tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell 
let containerView = UIView(frame:headerView.frame) 
headerView.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
headerView.myLabel.text = "section title 1" 
containerView.addSubview(headerView) 
return containerView 
+1

此解決方案在Objective C中爲我工作,謝謝:-)想要補充一點,我必須在我的標題單元格上將「TransTranslatesAutoresizingMaskIntoContraints」設置爲「NO」,並將它限制到我的containerView。也爲我的viewWillTransitionToSize旋轉:withSizeCoordinator:我不得不添加一個調用,讓我的表視圖重新加載它的數據,然後在我的視圖setNeedsLayout。希望這個信息也能幫助別人。 – CodenameDuchess

+0

謝謝你!!!! –

0

它幫助我在SWIFT 2.1

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView { 
      let headerCell:SBCollapsableTableViewHeader = self.friendsInfoTableView.dequeueReusableCellWithIdentifier("HeaderCell") as! SBCollapsableTableViewHeader 
      let containerView = UIView(frame:headerCell.frame) 
      headerCell.delegate = self 
      headerCell.titleLabel.font = UIFont.boldSystemFontOfSize(15) 
      headerCell.titleLabel.text = "\(self.ObjectArray[section].sectioName)" 
      headerCell.collapsedIndicatorLabel.text = collapsed ? "+" : "-" 
      headerCell.tag = section 
      headerCell.backgroundColor = UIColor(red:72/255,green:141/255,blue:200/255,alpha:0.9) 
      containerView.addSubview(headerCell) 
      return containerView 
    } 
0

我收到了同樣的錯誤,但對於differen理由。

我有一個自定義的UITableViewCell設置其中一個UITextField成爲FirstFoundResponder。當此自定義單元格的多個實例被返回時發生錯誤。

我只是將自定義單元格的第一個實例設置爲FirstResponder並解決了問題。

0

在我的情況下(非常愚蠢的一個),我用if let語法內cellForRowAt這樣的:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if let cell = tableView.dequeueReusableCell(withIdentifier: "carCell", for: 
    indexPath) as? carTableViewCell { 
     // Did some stuff here 
     return carTableViewCell() 
    else { 
     return carTableViewCell()  
    } 
} 

正如你可以看到,我回到我一般carTableViewCell(),而這正是給了我卑鄙錯誤... 我希望它永遠不會幫助別人哈哈!(:?

編碼快樂

相關問題