2016-10-26 5 views
0

내 테이블 뷰에서 dataArray라는 배열이 있고 JSON을 사용하여 서버에서 해당 개체를 가져옵니다. 각 셀에 버튼을 추가하고 버튼을 클릭하면 셀이 다른 섹션 또는 더 정확한 후속 정렬 명령이라는 다른 배열로 이동해야합니다. 이제 세포가 다른 섹션으로 옮기고 있지만 6 개의 셀을 이동 한 후에 나는 이것을 말한 오류가납니다. 모든 배열은 NSMutableArray입니다. 또한, 나는 IOS에 익숙해 져서 나와 함께 일하려고합니다. 고마워요. 때문에 캐치되지 않는 예외 'NSRangeException'응용 프로그램 종료, 이유 * 27 : 셀이 스위칭 섹션 일 때 tableview 내 오류

2016년 10월 26일

17 : 19.569 CustomCellApp [198,103 (3212)], 「*
- [__ NSArrayM objectAtIndex :] 범위를 넘는 인덱스 6 [0 .. 4]

---------------------

측면 참고 I 별도 오류 포스트 수 하지만 다른 문제가 있습니다. 셀에 표시되는 이미지가 섹션을 전환 할 때 다른 이미지로 변경되고, 창을 사용하여 SDWebImag 이자형.

---------------------

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

if (!isFiltered) { 

    if (section == 0) { 
     return [followedArray count]; 
    } 
    else if (section == 1) { 
     return [dataArray count]; 
    } 
} 
return [filteredArray count]; 

} 

--------------- ------

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
if (section == 0) { 
    return @"Followed Data"; 
} 
else { 
    return @"All Data"; 
} 
} 

---------------------

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

static NSString *CellIdentifier = @"Cell"; 
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (!cell) { 
    cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

// Configuring the cell 
Data * dataObject; 
if (!isFiltered) { 

    if (indexPath.section == 0) { 
     dataObject = [followedArray objectAtIndex:indexPath.row]; 
    } 
    else if (indexPath.section == 1) { 
     dataObject = [dataArray objectAtIndex:indexPath.row]; 
    } 
} 
else { 
    dataObject = [filteredArray objectAtIndex:indexPath.row]; 
} 

// Loading Images 
if (!isFiltered) { 
    // Exception Breakpoint Here 
    NSURL * imgURL = [[dataArray objectAtIndex:indexPath.row] valueForKey:@"dataURL"]; 
    [cell.myImageView setContentMode:UIViewContentModeScaleAspectFit]; 
    [cell.myImageView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"no-image.png"] options:SDWebImageRefreshCached]; 
}else{ 
    NSURL * imgURL = [[filteredArray objectAtIndex:indexPath.row] valueForKey:@"dataURL"]; 
    [cell.myImageView setContentMode:UIViewContentModeScaleAspectFit]; 
    [cell.myImageView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"no-image.png"] options:SDWebImageRefreshCached]; 
} 

// Loading Follow Button 
cell.followButton.tag = indexPath.row; 
[cell.followButton addTarget:self action:@selector(followButtonClick:) forControlEvents:UIControlEventTouchUpInside]; 
cell.followButton.hidden = NO; 

return cell; 
} 

---------------------

-(void)followButtonClick:(UIButton *)sender { 

// Adding row to tag 
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView]; 
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition]; 

// Creating an action per tag 
if (indexPath != nil) 
{ 

    // Change Follow to Following 
    [sender setImage:[UIImage imageNamed:@"follow.png"] forState:UIControlStateNormal]; 
    cell.followButton.hidden = YES; 
    cell.followedButton.hidden = NO; 


    // ----- ERROR BEGINS HERE ----- // 
    [self.myTableView beginUpdates]; 

    // ----- Inserting Cell to Section 0 ----- *CAUSING PROBLEMS* 
     // Exception Breakpoint Here 
    [followedArray addObject:[dataArray objectAtIndex:indexPath.row]]; 
    [myTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:followedArray.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; 

    NSLog(@"indexPath.row = %ld", (long)indexPath.row); 

    // ----- Removing Cell from Section 1 ----- *WORKING* 
    [dataArray removeObjectAtIndex:indexPath.row]; 
    NSInteger rowToRemove = indexPath.row; 
    [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES]; 

    NSLog(@"Array =%@",followedArray); 

    [self.myTableView endUpdates]; 
    // ----- ERROR ENDS HERE ----- // 
} 
} 

------------------ ---

+0

전 셀 번호 4를 클릭하면 셀 번호 8의 위치가 같아 질 수 있습니다. 따라서 indexPath가 무엇을 기대합니까? 셀 내부에서 단추 동작을 구현하고 단추를 클릭하여 올바른 인덱스 경로를 얻을 때 위임하게해야합니다. –

+0

@AliOmari 오류의 원인이 될 수 있습니까? 문제를 해결하려면 어떻게해야합니까? – BroSimple

+0

어쩌면 그건 이유가 아니지만 잘못된 방식으로하고있는 것일 수 있습니다. 먼저이 문제를 해결하고 다음 단계로 넘어 갑시다. 셀 안의 단추 동작을 추가하고 단추를 눌렀을 때보기 제어기를 호출 할 수 있도록보기 제어기를이 셀의 위임자로 전달해야합니다. –

답변

0

이것은 내가 가지고있는 과실을 고치는 것을 도운 무슨이다. 나를 도와 주신 @AliOmari에 대한 크레딧. 내 버튼 내부 cellForRowAtIndexPath

// Configuring the cell 
Data * dataObject; 
if (!isFiltered) { 

    if (indexPath.section == 0) { 
     dataObject = [followedArray objectAtIndex:indexPath.row]; 
     [cell populateCell:dataObject isFollowed:YES indexPath:indexPath parentView:self]; 
    } 
    else if (indexPath.section == 1) { 
     dataObject = [dataArray objectAtIndex:indexPath.row]; 
     [cell populateCell:dataObject isFollowed:NO indexPath:indexPath parentView:self]; 
    } 
} 
else { 
    dataObject = [filteredArray objectAtIndex:indexPath.row]; 
    [cell populateCell:dataObject isFollowed:NO indexPath:indexPath parentView:self]; 
} 
return cell; 

에서

당신이 실제로있는 tableview의 세포가 그래서 만약 재사용, 세포를 얻기 위해 사용자가 클릭의 위치를 ​​얻을 수있는 이유

[self.myTableView beginUpdates]; 

    // ----- Inserting Cell to Section 0 ----- 
    [followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:0]; 
    [myTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; 
    //NSLog(@"indexPath.row = %ld", (long)indexPath.row); 

    // ----- Removing Cell from Section 1 ----- 
    [dataArray removeObjectAtIndex:indexPath.row]; 
    NSInteger rowToRemove = indexPath.row; 
    [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES]; 
    //NSLog(@"Array =%@",followedArray); 

    [self.myTableView endUpdates];