2014-06-16 3 views
1

저는 7 개의 정적 셀이있는 UITableview가 있고 각 셀에는 다른 뷰와 연결된 세그가 있습니다. 셀을 재정렬 할 수있게 만들고 싶습니다. 사용자가 셀을 재정렬 한 후 각 셀의 reuseID와 위치를 NSUserdefaults에 기록합니다.Reorder 코드에 정적 셀이있는 UITableview

하지만 뷰를 다시로드 할 때 Cell을 표시해야하는 위치를 Tableview에 어떻게 알릴 수 있습니까?

안부

더크

+0

순서를 변경하지 않았습니까 (정적 셀을 사용 해본 적이 없으므로 가능한지 모르겠습니다)? 재발행시 테이블을 새로운 주문으로 보는 방법은 문제입니까? – rdelmar

+0

재주문은 테이블 뷰가 표시되는 동안 작동하지만 뷰를 닫고 돌아올 때 표준 순서가 적용됩니다. 내가 tableview가 표시되기 전에 정적 셀을 재정렬하는 소프트웨어에서 메서드를 찾고 있어요. – Dirk

답변

3

일반적으로 정적 테이블보기를 사용하는 경우, 데이터 소스의 메소드를 구현하지 것이지만,이 경우에는 그렇게하는 것이 필요한 것 같다. IBOutletCollection을 만들고이 배열에 셀을 추가했습니다. 첫 번째 셀부터 마지막 ​​셀까지 순서대로 셀을 추가 했으므로 테이블을 처음로드 할 때 스토리 보드 순서로 나타납니다. cellForRowAtIndexPath에서는 정적 셀에서는 작동하지 않으므로 셀을 큐에서 제거 할 수 없으므로 대신 콘센트 콜에서 셀을 가져옵니다. 셀이 표시되어야하는 순서를 추적하는 별도의 배열이 있는데, 이것이 사용자 기본값으로 저장됩니다. 다음은 테스트에 사용 된 코드입니다.

@interface StaticTableViewController() 
@property (strong,nonatomic) NSMutableArray *cells; 
@property (strong, nonatomic) IBOutletCollection(UITableViewCell) NSArray *tableCells; 

@end 

@implementation StaticTableViewController 

-(void)viewDidLoad { 
    [super viewDidLoad]; 
    self.cells = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"cells"] mutableCopy]; 
    if (! self.cells) self.cells = [@[@0,@1,@2,@3,@4] mutableCopy]; 
} 



- (IBAction)enableReordering:(UIBarButtonItem *)sender { 
    [self.tableView setEditing:YES animated:YES]; 
} 


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


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSInteger idx = [self.cells[indexPath.row] integerValue]; 
    UITableViewCell *cell = self.tableCells[idx]; 
    return cell; 
} 



-(BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath { 
    return NO; 
} 


-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return UITableViewCellEditingStyleNone; 
} 


- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
    NSNumber *numberToMove = self.cells[fromIndexPath.row]; 
    [self.cells removeObjectAtIndex:fromIndexPath.row]; 
    [self.cells insertObject:numberToMove atIndex:toIndexPath.row]; 
    [[NSUserDefaults standardUserDefaults] setObject:self.cells forKey:@"cells"]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
} 
관련 문제