2016-10-20 2 views
0

내 IOS 응용 프로그램에서 배열 (dataArray)를 사용하는 한 섹션에서 다른 셀 (followingArray)을 사용하는 다른 섹션으로 각 셀 내의 단추로 셀을 이동하려고합니다. 버튼을 설정했지만 버튼 액션으로 셀을 이동하는 코드를 찾을 수 없습니다. 버튼을 사용하여 두 섹션 사이에서 셀을 이동하는 방법은 무엇입니까?

는 내가있는 NSMutableArray에서 개체를 추가하고 제거하는 몇 가지 코드를 추가하는 시도하지만 때문에 캐치되지 않는 예외 'NSInternalInconsistencyException'응용 프로그램 종료, 이유는 오류


***를 가지고 : 삽입 '시도 섹션 0으로 행 0하지만, 섹션 0 만 0 행이 업데이트 '


코드에 간다 후가 - (무효) followButtonClick (ID) 보낸 다음

내가 서버에서 내 배열을 얻을 수동으로 입력하지 때문에 태그에 의해 각 셀에가는 내 버튼에 추가 시도하는 코드입니다

 [myTableView beginUpdates]; 

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

    // Inserting Cell to followedArray Section 
    [followedArray insertObject:indexPath atIndex:indexPath.row]; 
    NSInteger rowToAdd = indexPath.row; 
    [myTableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES]; 

    [myTableView endUpdates]; 

.

여러 수준에서 이것이 틀렸다는 것이 확실하지만 여기서는 공란을 그리기로합니다. 누군가가 올바른 방향으로 나를 가리킬 수 있기를 바랍니다.

코드 :

-(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) 
{ 
    NSLog(@"Current Row = %@", indexPath); 

    // Showing Status Labels 
    CustomCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath]; 
    cell.firstStatusLabel.hidden = NO; 
    cell.secondStatusLabel.hidden = NO; 

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

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

    // ----- Inserting Cell to Section 0 ----- *NOT WORKING* 
    [followedArray insertObject:[gamesArray objectAtIndex:indexPath.row] atIndex:indexPath.row]; 
    NSInteger rowToAdd = indexPath.row; 
    [self.myTableView insertRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES]; 

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

    [self.myTableView endUpdates]; 
    } 
} 

filteredArray 제에 대한 각 셀 내의

cellForRowAtIndexPath

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

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

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

팔로우 버튼을 클릭 검색 막대, 항목은 dataArray에서 필터링됩니다. 내 모든 데이터가 내 서버

followedArray에서에서입니다

dataArray 내가 버튼을 dataArray 또는 filteredArray에서 클릭 할 때 셀을 넣어 원하는 위치이입니다.

더 많은 코드가 필요하면 알려 주시기 바랍니다. 잘하면이 문제를 해결할 수 있습니다. 감사합니다.

답변

0

이 날 @AliOmari &에, @Rachel

[self.myTableView beginUpdates]; 

    [followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:0]; 
    [myTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; 

    [dataArray removeObjectAtIndex:indexPath.row]; 
    NSInteger rowToRemove = indexPath.row; 
    [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES]; 

    [self.myTableView endUpdates]; 
1

한 섹션에서 셀을 삭제하고 다른 섹션에 셀을 삽입 할 수 있습니다.

//update data Array1 
[xxx removeObject:xxx]; 
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:rowToRemove inSection:0]] withRowAnimation:YES]; 

//update data Array2 
[xxx insertObject:xxx]; 
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:rowToAdd inSection:0]] withRowAnimation: YES]; 
+0

난은 insertObject에 대한 오류 받고 있어요 크레딧 오류를 수정 도움 것입니다 : "NSArray를위한 눈에 띄는 @ 인터페이스가 선택기를 선언하지를은 insertObject : " – BroSimple

+1

@BroSimple NSArray는 변경할 수 없으므로 수정할 수 없습니다. NSMutableArray를 사용할 수 있습니다.아래 예제 : [anArray removeObjectAtIndex : index]; [anArray removeObject : item]; [anArray removeLastObject]; – Rachel

+0

좋아, NSMutableArray를 만들었고 [followingArray insertObject : indexPath atIndex : 0];을 사용했다. indexPathForRow : rowToRemove 또는 rowToAdd에 대해이 내용이 정확하고 또 하나입니다. 나는 상당히 새로운 것입니다. – BroSimple