2013-07-26 33 views
2

我有一個tableView與一些部分和每個部分有一些rows.Every行有一個最喜歡的按鈕。如果單擊該按鈕行應該被添加到收藏夾部分。 (最初是空的)。將行復制到UITableView中的另一部分

我寫了一些代碼。但問題是它在iOS 5模擬器中工作,並且在具有無效tableView更新錯誤的iOS 6模擬器中崩潰。

有人能告訴我問題在哪裏嗎?

-(void)moveRowToFavourites:(id)sender{ 
    UIButton *button = (UIButton *)sender; 
    UITableViewCell *cell = (UITableViewCell *)button.superview.superview; 
    NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
    [tempArray addObject:[NSIndexPath indexPathForRow:favouritesArray.count inSection:0]]; 
    [favouritesArray insertObject:cell.textLabel.text atIndex:favouritesArray.count]; 
    [[self tableView] beginUpdates]; 
    [[self tableView] insertRowsAtIndexPaths:(NSArray *)tempArray withRowAnimation:UITableViewRowAnimationFade]; 
    [[self tableView] endUpdates]; 
} 

編輯

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

    if(searching){ 
     return [self.searchResults count]; 
    } 
    else{ 
     if ([[arrayForBool objectAtIndex:section] boolValue]) { 
      if(section == 0){ 
       return favouritesArray.count; 
      }else{ 
       NSString* key = [self.proCategoriesArray objectAtIndex:section - 1]; 
       NSArray *aArray = [sectionContentDict valueForKey:key]; 
       return aArray.count; 
      } 
     } 
     return 1; 
    } 
} 
+1

錯誤描述包含對問題的非常明確的解釋。 –

+0

是否嘗試reloadData而不是更新? –

+0

錯誤是什麼? –

回答

0

好像你的數據源返回0作爲部分和行數。您沒有正確插入/刪除行,插入新行時,再次調用數據源方法,並且例如嘗試以行計數爲4的方式插入對象,並且數據源tableView:numberOfRowsInSection:返回3 ,你會得到一個異常,因爲你試圖添加更多的數據源來承諾數據源。

當您嘗試更新表視圖所有這些數據源的方法再次調用:

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView; 
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath; 

你必須確保它們返回正確的值。我懷疑你甚至沒有實施它們。

編輯

的問題是,你只是插入一個新的對象favouritesArray,但這並不影響tableView:numberOfRowsInSection:返回的行數。在那裏你沒有返回favouritesArray.count,這個例外是由於tableView:numberOfRowsInSection:返回的數字小於表視圖應該有的行數。

在你的情況,我認爲你甚至不需要調用insertRowsAtIndexPaths:,你可以做一切與您的數據源:

-(void)moveRowToFavourites:(id)sender { 
    UIButton *button = (UIButton *)sender; 
    UITableViewCell *cell = (UITableViewCell *)button.superview.superview; 
    [favouritesArray insertObject:cell.textLabel.text atIndex:favouritesArray.count]; 
    [[self tableView]reloadData]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return favouritesArray.count; 
} 

tableView:cellForRowAtIndexPath:你應該返回有一個對象被拾取的表格單元格favouritesArray作爲文本。

+0

是,那就是錯誤所在。但一切似乎都很好,並在iOS5模擬器工作,我得到這個錯誤在iOS6和以上的模擬器 –

+0

@ChandraSekhar我可以看到數據源的方法嗎?我很確定這個錯誤在那裏。 –

+0

你確定,我已經更新了代碼 –

0

你剛纔應該調用如下

[favouritesArray addObject:cell.textLabel.text]; 
[tableView reloadData]; 

確保你實現了這個功能:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    switch(section){ 
     case 0: 
      return favouritesArray.count; 
     //Your other sections have to placed here, too 
     default: 
      return 0; 
    } 
} 

那麼您的tableView應該進行自我更新。問題是你插入整個單元格,但你只需要在數組中插入數據。希望能幫助到你!

相關問題