2013-07-26 5 views
2

일부 섹션 및 일부 섹션이있는 각 섹션이있는 tableView가 있습니다. 모든 행에는 즐겨 찾기 버튼이 있습니다. 버튼을 클릭하면 행이 즐겨 찾기 섹션에 추가됩니다. (처음에는 비어 있음)행을 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:를 호출 할 필요가 없습니다, 당신은 당신의 데이터 소스로 모든 작업을 수행 할 수 있습니다 tableView:cellForRowAtIndexPath:에서

-(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; 
} 

당신은 객체 가져온 양식이있는 셀을 반환해야 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 자체를 업데이트해야합니다. 문제는 전체 셀을 삽입하지만 배열에 데이터를 삽입하기 만하면된다는 것입니다. 희망이 도움이됩니다!

관련 문제