2012-01-29 62 views
0

我有一個tableVIew,其中每個單元格的高度爲90.0f(高度)。我的代碼;TableView單元格外觀問題

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 90.0; 
} 

現在,當我刪除表中的所有記錄。單元格的高度縮小到它的默認值44(我認爲)。

1.)我的問題是,即使在用戶刪除所有記錄後,我仍然需要單元格的高度保持爲90.0f。我如何以編程方式執行此操作?

2.)我給了我的細胞2種顏色。

cell.contentView.backgroundColor = indexPath.row % 2 ? [UIColor whiteColor] : [UIColor blackColor] ; 

當我刪除第二個單元格,它是黑色的,那麼第一個和第三個單元格都是白色的。我怎麼能重新安排這是一個訂單黑色和白色。

編輯:

好這個工作,但是當我嘗試刪除表中的最後一個記錄和[tableview indexPathsForVisibleRows]我最終得到斷言失敗

-[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:961

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. Attempt to delete more rows than exist in section.'

這是爲什麼?

CODE:

[self.tableView beginUpdates]; 

     [self.myArray removeObjectsInArray:discardedItems ]; 

     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];  

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES]; 

     [self.tableView endUpdates]; 
+0

你知道你可以使用blackColor而不是黑色寫出的RGB值。 – 2012-01-29 16:18:56

+0

還有一些其他的顏色,我不得不改變它發佈到SO – sharon 2012-01-29 16:20:35

回答

1

1)我不認爲你可以做到這一點。其中一種方法是顯示x個空白單元格以填充頁面。

2)刪除單元格後調用[tableView reloadData],或在刪除單元格前後的行上調用[tableView reloadCellAtIndex ...]。

+0

我編輯了我的問題。 – sharon 2012-01-30 16:19:11

1

如果行的高度確實是靜態的(如您所示),請考慮不實施tableView:heightForRowAtIndexPath:,而是使用表視圖的rowHeight屬性。
這可以在代碼或界面生成器中設置,應該是「如果沒有問題,行縮小」的解決方案。

您的第二個問題:
您是否知道-[UITableView indexPathsForVisibleRows]

您可以使用它來只重新加載需要更新的表視圖部分。它會是這樣的:

- tableView:(UITableView *)table commitEditingStyle:(UITableViewCellEditingStyle)style forRowAtIndexPath:(NSIndexPath *)path 
{ 
    NSArray *visibleCellPaths = [table indexPathsForVisibleRows]; 

    // replace this comment with whatever else needs to be done in any case... 

    if (style == UITableViewCellEditingStyleDelete) { 
     // replace this comment with the deletions of the model object 

     NSUInteger affectedPathIndex = [visibleCellPaths indexOfObject:path]; 
     NSMakeRange updateRange = NSMakeRange(affectedPathIndex, [visibleCellPaths count] - affectedPathIndex); 
     NSArray *cellPathsToReload = [visibleCellPaths subarrayWithRange:updateRange]; 

     // replace this comment with the bounds-checking. 
     // e.g. if you only had one section and the backing store was an array, it may look like this: 
     // while ([self.backingStore count] <= [[cellPathsToReload lastObject] row]) { 
     // NSUInteger remainingPathCount = [cellPathsToReload count]; 
     // if (remainingPathCount) break; 
     // cellPathsToReload = [cellPathsToReload subarrayWithRange:NSMakeRange(0, remainingPathCount - 1)]; 
     // } 

     [table reloadRowsAtIndexPaths:cellPathsToReload withRowAnimation:UITableViewRowAnimationFade]; 
    } 

} 
+0

確定這個工程,但當我嘗試刪除表中的最後一條記錄和'[tableview indexPathsForVisibleRows]'我最終得到'斷言失敗 - [UITableView _endCellAnimationsWithContext:],/SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m :961'和'終止應用程序,由於未捕獲的異常'NSInternalInconsistencyException',原因:'無效的更新:無效的行數在第0節。試圖刪除更多的行比節中存在'.'爲什麼? – sharon 2012-01-30 16:17:32

+0

我已經更新了我的答案,並用我實際測試過的東西替換了代碼。這在我的設置中效果很好,但是你的里程可能會有所不同...... – danyowdee 2012-01-31 03:38:26

0
  1. 設置UITableViewrowHeight財產。

文檔狀態:

在接收機的每一行(表單元格)的高度。

  1. 請提供您的數據源代碼
+0

我已將我的代碼添加爲編輯。請看看問題的編輯點 – sharon 2012-01-30 16:43:21