2013-02-12 57 views
1

我已經得到了我想要插入在刪除行一個UITableView。初始化用「插入」行一個UITableView

如果我有10個值的數組,與實現代碼如下,包括所有行,表格開始顯示正常。然後,如果我想過濾除[0,1,5]以外的所有內容。然後,我通過在索引路徑刪除行的過程:[2,3,4,6,7,8,9]和返回的行數是3.我不必更改我的單元格渲染代碼,它顯示行:[0,1,5]。但是,如果在過濾了所有內容之後,我想回到該表並從持久性加載行[0,1,5]的表,我遇到了麻煩。該表加載的行數爲3,並呈現單元格:[0,1,2]。

我已經試過初始化在viewDidLoad中的表0行,然後在viewDidAppear調用insertRows:0,1,5] - 但這將引發異常:

 
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:909 

而且我驗證過在viewDidLoad中,行數正在打印0,並且在引發此錯誤之前,打印的行數是3.

編輯 如果我插入[0,1,2],它會工作...但是再一次......我最終需要[0,1,5]出現。

 
- (void) viewDidAppear:(BOOL) animated 
{ 
    [super viewDidAppear:animated]; 

    NSArray *initializing_indeces = @[[NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0], [NSIndexPath indexPathForRow:5 inSection:0]]; 
    [self.userDataTable insertRowsAtIndexPaths:initializing_indeces withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 
 
- (int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Using different booleans, but the gist: 
    if (viewDidLoad) 
     return 0; 
    if (viewDidAppear || filtered) 
     return 3; 
    if (expanded) 
     return 10; 
} 
 
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"]; 
    } 


    [cell.textLabel setText:[NSString stringWithFormat:@"%i",indexPath.row]]; 

    return cell; 
} 
+0

放在這裏relavent代碼:) – iPatel 2013-02-12 04:20:41

+0

好看到這些鏈接並檢查: http://stackoverflow.com/questions/6241625/getting-an-assertion-failure-error&也是這個 的http://計算器.com/questions/10134841/assertion-failure-in-uitableview-endcellanimationswithcontext – Vishal 2013-02-12 04:23:06

+0

在numberOfRowsInSection你應該給10行..並且complite any oparation write [tableView reloadData]; – iPatel 2013-02-12 04:44:21

回答

0

我要保持的映射的軌道自己:

 
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"]; 
    } 


    int row = indexPath.row; 
    if (filtered) { 
     // Translate 0 - 0 
     // Translate 1 - 1 
     // Translate 2 - 5 
    } 

    [cell.textLabel setText:[NSString stringWithFormat:@"%i", row]]; 

    return cell; 
} 

我結束了使用一個NSMutableArray即,過濾時,變爲[0,1,5]所以訪問該陣列,indexPath的索引。 2返回5.

0

首先在你的情況numberOfRowsInSection回報NSIntegerint,所以請更換

我並不完全是什麼resone。 我認爲這個問題必須在你的代碼連接到這個部分,

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

通常你在這些數據源的方法之一犯了一個錯誤。 可能是它不可能說究竟什麼是錯的,但它可能是這樣的:你能有今天在thenumberOfRowsInSection表視圖(在你的案件10)和thecellForRowAtIndexPath你則只能處理10 - 1行例。 如果您向我們展示您的數據源的實現,那麼知道發生了什麼會容易得多。

0

對於每次的插入行,你需要重新加載使用[tableView realodData]

1

@Fish STIX

的實現代碼如下

[cell.textLabel setText:[NSString stringWithFormat:@「%i」,indexPath。行]];

這個方法在你的代碼中明確指出,text將是帶有indexPath.row值的NSString。現在,當你刪除你的行和數據源時,從10 = [0, 1, 5] + [2, 3, 4, 6, 7, 8, 9]3 = [0, 1, 5]。您的數據源已被減少到第3個數組。因此,您將只獲得三行,如同返回的bby numberOfRowsInSection方法。三行將有這些indexPath.row的0, 1, 2。因此你得到0,1 ,2而不是0, 1 ,5。如果你打印indexPath,在cellForRowAtIndexPath方法(過濾之後),你會看到它們分別是[0,0] [0,1] [0,2]意味着部分0,行號0,1,2 。因此你的結果

如在得到你想要的結果。做這個。採取屬性@property (保留,nonAtomic)NSArray * dataArray;

viewDidLoad中: self.dataArray = [NSArray的 arrayWithObjects:@ 「0」,@ 「1」,@ 「2」,@ 「3」,@ 「4」,@ 「5」,@ 「6」,@「7」,@「8」,@「9」, 無];

in numberOfRowsInSection return [array count];

cellForRow [cell.textLabel的setText:[self.dataArray objectAtIndex:indexPath.row]];

現在只需更改數據源(我的意思是dataArray中)來過濾或 擴張狀態)在viewWillAppear中或任何你想要和重裝 用表數據[self.tableView reloadData];

希望我很清楚。乾杯,玩得開心。