2013-10-25 4 views
3

나는 세 개의 섹션 (섹션 1, 섹션 2 및 섹션 3)이있는 테이블이 있습니다. 드래그 & 드롭 (동일한 섹션 내부 또는 다른 섹션에서) 끌어서 셀 위치를 변경하는 메커니즘을 구현하고 싶습니다.UITableView : 끌어서 놓기로 다른 위치로 셀 이동

예제 1 : 섹션 1에는 두 개의 행 (sec1_row1 및 sec1_row2)이 있습니다. 섹션 2에서 드래그하여 하나의 행 (sec2_row1)을 가지고 & drop function 나는 sec2_row1로 sec1_row2를 반전시키고 싶습니다. sec2_row1을 선택하고 sec2_row1로 드래그하여 행을 거꾸로 놓으십시오.

이 기능은 같은 섹션의 행에도 사용할 수 있습니다.

이 기능을 구현하는 가장 좋은 방법은 무엇입니까?

미리 감사드립니다. 아래 링크 자세한 내용 확인을 위해

+1

내장 된 jQuery과 이동 세포 기능을 사용 https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/ instm/UITableView/moveRowAtIndexPath : toIndexPath : 주변에서 검색 ... – Tim

답변

2
- (NSIndexPath *)tableView:(UITableView *)tableView 
    targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
    toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath 
    { 
     NSDictionary *section = [data objectAtIndex:sourceIndexPath.section]; 
     NSUInteger sectionCount = [[section valueForKey:@"content"] count]; 
     if (sourceIndexPath.section != proposedDestinationIndexPath.section) 
     { 
      NSUInteger rowInSourceSection = 
      (sourceIndexPath.section > proposedDestinationIndexPath.section) ? 
      0 : sectionCount - 1; 
      return [NSIndexPath indexPathForRow:rowInSourceSection inSection:sourceIndexPath.section]; 
     } 
     else if (proposedDestinationIndexPath.row >= sectionCount) 
     { 
      return [NSIndexPath indexPathForRow:sectionCount - 1 inSection:sourceIndexPath.section]; 
     } 

    // Allow the proposed destination. 
    return proposedDestinationIndexPath; 
    } 

:

Managing the Reordering of Rows

당신은 다음 링크를 참조하시기 바랍니다 섹션 외부 재 주문을 방지하려면 : How to limit UITableView row reordering to a section

+0

@ 네하 편집을 감사드립니다. –

0

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath 
{ 

    // Do not allow any movement between section 
    if (sourceIndexPath.section != proposedDestinationIndexPath.section) 
     return sourceIndexPath; 
    // You can even control the movement of specific row within a section. e.g last row in a  Section 

    // Check if we have selected the last row in section 
    if ( sourceIndexPath.row < sourceIndexPath.length) { 
     return proposedDestinationIndexPath; 
    } else { 
     return sourceIndexPath; 
    } 
    // You can use this approach to implement any type of movement you desire between sections or within section  
} 
0

이 코드가 작동 해보십시오

targetIndexPathForMoveFromRowAtIndexPath에 조건을 관리 할 필요가 벌금.

#import "ViewController.h" 

    @interface ViewController()<UITableViewDelegate,UITableViewDataSource> 
    { 
     NSMutableArray *NameArray; 
    } 

    @property (weak, nonatomic) IBOutlet UITableView *NameListTableView; 
    @end 

    @implementation ViewController 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 

     _NameListTableView.delegate=self; 
     _NameListTableView.dataSource=self; 

     NameArray=[NSMutableArray arrayWithObjects:@"apple", @"b", @"c", @"d", @"e",@"f", @"g", @"h",@"i",@"j", @"k", nil]; 



     [self.NameListTableView setEditing:YES animated:YES]; 
     // Do any additional setup after loading the view, typically from a nib. 
    } 


    - (void)didReceiveMemoryWarning { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 

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

    } 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *[email protected]"NameCell"; 

     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId]; 
     if(cell!=nil) 
     { 
      cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId]; 
     } 

     NSString *abc=[NameArray objectAtIndex:indexPath.row]; 

     cell.textLabel.text=abc; 


     return cell; 


    } 
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     return UITableViewCellEditingStyleNone; 
    } 
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     return YES; 
    } 
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 
    { 
     [NameArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; 

     [self.NameListTableView reloadData]; 
    } 
    @end 
관련 문제