2014-03-02 55 views
2

我在UITableView中創建了自定義單元格。樣式設置爲自定義。標識符設置爲customCell。附件設置爲Detail。表視圖內容設置爲動態原型。單元格中的數據發生更改後,UITableView中的自定義單元格將無法正確加載

該表格載入了來自核心數據的數據,並且在第一次出現時就顯示爲完美。當用戶點擊單元格中的詳細圖標時,會出現一個新的視圖,以便用戶可以編輯單元格中的數據並將其保存爲核心數據。當用戶完成編輯並點擊保存按鈕時,視圖返回到UITableView,但剛剛編輯的單元格顯示爲「基本」單元格,而不是customCell。如果點擊單元格,它將突出顯示,並且自定義單元格內容會顯示在基本單元格內容上。

我似乎無法解決此問題。任何人都可以提出建議

這裏是prepareForSegue發送到編輯視圖控制器的部分:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell*)sender { 

if ([[segue identifier]isEqualToString:@"editPunchItemSegue"]) { 
// DEFINE SEGUE DESTINATION 
UINavigationController *navigationController = segue.destinationViewController; 
EditPunchItemViewController *editPunchItemViewController = (EditPunchItemViewController*)navigationController.topViewController; 

NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 
Punchitem *editPunchItem = (Punchitem*)[self.fetchedResultsController objectAtIndexPath:indexPath]; 

editPunchItemViewController.editPunchitem = editPunchItem; 

而現在,在目標視圖控制器,其中,所述核心數據被編輯時,這裏是saveButtonPressed方法的一部分:

if ([_editSaveButton.title isEqualToString:@"Save"]) { 
    NSLog(@"Save Button has been PRESSED"); 

    editPunchitem.punchitemRoomNumber = _editPunchitemRoomNumberField.text; 
    editPunchitem.punchitemDescription = _editPunchitemDescriptionField.text; 
    editPunchitem.punchitemLocation = _editPunchitemLocationField.text; 
    editPunchitem.punchitemRoomName = _editPunchitemRoomNameField.text; 
    editPunchitem.punchitemDate = _editPunchitemDateField.text; 

    // SAVE TO MANAGE OBJECT CONTEXT! 
    [super saveAndDismiss]; 
} 

有什麼建議嗎?

這是我在TableViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd)); 

    static NSString *CellIdentifier = @"customCell"; 

// USING CUSTOM PROTOTYPE CELL: 
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    Punchitem *punchitem = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

// USING CUSTOM PROTOTYPE CELL: 
    cell.cellLocationLabel.text = punchitem.punchitemLocation; 
    cell.cellDescriptionLabel.text = punchitem.punchitemDescription; 
    cell.cellRoomNameLabel.text = punchitem.punchitemRoomName; 

    return cell; 
} 

保存cellForRowAtIndex和罷免是一個子類的一部分。這裏是方法:

-(void) saveAndDismiss { 

    NSError *error = nil; 
    if ([self.managedObjectContext hasChanges]) { 
     if (![self.managedObjectContext save:&error]) { //SAVE FAILED 
      NSLog(@"YOUR SAVE FAILED! %@", [error localizedDescription]); 
     } else { 
      NSLog(@"SAVE SUCCEEDED!"); 
     } 
    } 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
+0

什麼保存和dissmiss做的,請出示cellAtRowIndex,它看起來像服用點被改寫你的故事板 – meda

+0

我只是說我的cellForRowAtIndex在我的原始張貼 – Jupiter869

+0

嘗試使用廈門國際銀行原型細胞。 –

回答

0

你是否嘗試重新加載表,當你從EditPunchItemViewController回來?

放在主人的viewController:

- (void)viewWillAppear:(BOOL)animated { 
    if (self.tableView) { 
     [self.tableView reloadData]; 
    } 
} 
+0

是的,我在我的TableViewController的viewWillAppear中有這條線。不幸的是,這並沒有幫助。 – Jupiter869

0

你有這樣的事情

[self.tableView beginUpdates]; 

如果是的話,你一定要調用此完成後

[self.tableView endUpdates]; 

大多數我們使用NSFetchedResultsController的時候...

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tableView beginUpdates]; 
} 
... 
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tableView endUpdates]; 
} 
+0

是的,謝謝你的建議,但我已經包含在我的代碼中的這兩種方法。 – Jupiter869

0

最後一個選項,你必須有條件的調試...

所有在主視圖控制器:

1)添加屬性

@property (strong,nonatomic) NSIndexPath *DEBUGindexPath; 

2)設置DEBUGindexPath在

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell*)sender { 

if ([[segue identifier]isEqualToString:@"editPunchItemSegue"]) { 
... 
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 
DEBUGindexPath = indexPath; 
... 

3)添加一個斷點

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

編輯斷點設置條件:

(self.DEBUGindexPath.row == indexPath.row) && (self.DEBUGindexPath.section == indexPath.section) 

不要設置自動繼續...

好了,現在運行你的應用程序。當您從EditPunchItemViewController返回時,斷點只會觸發選定的行。一步一步檢查是否可以找到東西

0

該視圖返回到UITableView,但剛剛編輯過的單元格顯示爲「基本」單元格而不是customCell。

cellForRowAtIndexPath小心,以下condtion:

//USING CUSTOM PROTOTYPE CELL: 
    CustomTableViewCell *cell = 
        [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[CustomTableViewCell alloc] 
          initWithStyle:UITableViewCellStyleDefault 
          reuseIdentifier:CellIdentifier]; 
    } 

當細胞是零,你設置它的風格UITableViewCellStyleDefault但你忘了通過代碼來設置它的附件類型。

因此,只要確保設置其附件類型,如果聲明是這樣的:

//USING CUSTOM PROTOTYPE CELL: 
    CustomTableViewCell *cell = 
        [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[CustomTableViewCell alloc] 
          initWithStyle:UITableViewCellStyleDefault 
          reuseIdentifier:CellIdentifier]; 
     //Set accessory to Detail 
     cell.accessoryType = UITableViewCellAccessoryDetailButton; 
    } 

這裏的風格的完整列表:

typedef enum : NSInteger { 
    UITableViewCellAccessoryNone, 
    UITableViewCellAccessoryDisclosureIndicator, 
    UITableViewCellAccessoryDetailDisclosureButton, 
    UITableViewCellAccessoryCheckmark, 
    UITableViewCellAccessoryDetailButton 
} UITableViewCellAccessoryType; 
+0

我不明白。我正在使用自定義單元格。當表視圖加載時,我得到一個基本視圖,而不是我的自定義視圖。我在哪裏添加行cell.accessoryType = UITableViewCellAccessoryDe​​tailButton以確保自定義單元格出現,而不是基本單元格?我應該使用UITableViewCellStyleDefault以外的東西嗎? – Jupiter869

+0

@ Jupiter869對不起,我不是很清楚,看到我的編輯 – meda

+0

不幸的是,這沒有效果。當我從EditView返回到TableView時,單元格顯示爲一個「Basic」單元格,其中有一行數據,而不是我在故事板中設置的「customCell」。如果我點擊單元格,它會突出顯示,'Basic'和'customCell'會出現在對方的頂部,看起來好像'Basic'單元格位於'customCell'的TOP上面,令人莫名其妙,令人沮喪。 。我 – Jupiter869

相關問題