2012-02-29 2 views
0

UITableView에서 항목의 NSMUtableArray 목록을 채 웁니다.UITableView의 항목 목록 반전

표시된 목록의 순서를 뒤집을 수있는 기능이 있는지 궁금합니다.

는 내가 아마 루프 반전과 새 목록을 만들 수 있다는 것을 알고 있지만 그

감사합니다 어떻게 든 메모리의 낭비에요

답변

3

단지 순서는 내부의 데이터를 가져 오기를 반전하지 왜 데이터 소스 메서드?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    NSUInteger dataItemIndex = self.inverted ? (self.dataItems.count - 1 - indexPath.row) : indexPath.row; 

    // Fetch item at index and return cell ... 
} 

배열의 개체 순서를 뒤집을 수있는 방법이 없습니다. 이 question도 도움이 될 수 있습니다.

+0

나는 그 일을 할 수 있습니다. 단지 per-buit 기능이나 뭔가있을 거라고 생각했습니다. – user1051935

1

당신은 체크 아웃 수 :

워드 프로세서 NSSortDescriptor

가 : NSSortDescriptor의 인스턴스는 개체를 비교하기 위해 사용하는 속성을 지정하여 객체를 주문하기위한 기초를 설명, 방법은 비교하기 위해 사용하는 속성 및 비교가 오름차순인지 또는 내림차순인지를 지정합니다.

- (NSMutableArray *)reverseArrayOrder { 
    NSMutableArray *reversedArray = [NSMutableArray arrayWithCapacity:[self count]]; 
    NSEnumerator *enumerator = [self reverseObjectEnumerator]; 
    for (id element in enumerator) { 
     [reversedArray addObject:element]; 
    } 
    return reversedArray; 
} 
0

:

는 그냥 이렇게 아마도 것 내림차순 오름차순 단순한 반전 경우

비록을 (sortDescriptors 참조) 테이블보기에서 요소를 배치하는 방법을 지정하려면 NSMutableArray에 범주에 추가하십시오.

- (void) invertArray{ 
    NSUInteger operationCount = self.count/2; 
    NSUInteger lastIndex = self.count - 1; 
    id tmpObject; 
    for (int i = 0; i < operationCount; i++){ 
     tmpObject = [self objectAtIndex:i]; 
     [self replaceObjectAtIndex:i withObject:[self objectAtIndex:lastIndex - i]]; 
     [self replaceObjectAtIndex:lastIndex - i withObject:tmpObject]; 
    } 
} 

이렇게하면 새 배열을 만들지 않고 배열을 반전시킵니다. 또한, 배열의 절반 이상을 반복하는 것만으로도 상당히 효율적입니다.

는 어레이를 정렬하는 동안 (있는 NSMutableArray에 카테고리로 다시)이 방법을 사용있는 tableView를 배치해야하는 경우 :이 코드를 실행하는 방법으로 블록을 통과 할 수

- (void) invertArrayWithOperationBlock:(void(^)(id object, NSUInteger from, NSUInteger to))block{ 
    NSUInteger operationCount = self.count/2; 
    NSUInteger lastIndex = self.count - 1; 
    id tmpObject1; 
    id tmpObject2; 
    for (int i = 0; i < operationCount; i++){ 
     tmpObject1 = [self objectAtIndex:i]; 
     tmpObject2 = [self objectAtIndex:lastIndex - i]; 
     [self replaceObjectAtIndex:i withObject:tmpObject2]; 
     [self replaceObjectAtIndex:lastIndex - i withObject:tmpObject1]; 
     if (block){ 
      block(tmpObject1, i, lastIndex - i); 
      block(tmpObject2, lastIndex - i, i); 
     } 
    } 
} 

움직일 때마다. 이를 사용하여 테이블보기의 행에 애니메이션을 적용 할 수 있습니다. 예를 들면 다음과 같습니다.

[self.tableView beginUpdates]; 
[array invertArrayWithOperationBlock:^(id object, NSUInteger from, NSUInteger to){ 
    [self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:from inSection:0] toIndexPath:[NSIndexPath indexPathForRow:to inSection:0]; 
}]; 
[self.tableView endUpdates];