2011-09-17 5 views
9

나는 다음과 같은 코드를 가지고, 내가 messages Array을 반전 할 수있는 방법, 울부 짖는 소리를 참조 : 나는 이미있는 NSArray 역 검색 한NSArray의 걱정을 되 돌리시겠습니까?

#import "newsViewController.h" 
#import "DetailViewController.h" 

@implementation newsViewController 

@synthesize messageList; 

//############################################################################################################################## 
//#################       CUSTOM VIEW INITALIZATION          #################// 
//############################################################################################################################## 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
     lastId = 0; 
     chatParser = NULL; 
    } 
    return self; 
} 

//############################################################################################################################## 
//#################       DEALLOC - MEMORY RELEASE          #################// 
//############################################################################################################################## 
-(void)dealloc { 
    [messageList release]; 
    [super dealloc]; 
} 

//############################################################################################################################## 
//#################       DISPLAY PHP FILE INTEGRATION         #################// 
//############################################################################################################################## 
-(void)getNewMessages { 

    NSString *url = [NSString stringWithFormat:@"http://localhost/sportApp/messages.php?past=%ld&t=%ld",lastId, time(0) ]; 

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:url]]; 
    [request setHTTPMethod:@"GET"]; 

    NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    if (conn){ 
     receivedData = [[NSMutableData data] retain]; 
    }else{} 
} 

//############################################################################################################################## 
//#################        FETCHING PRAGMAS           #################// 
//############################################################################################################################## 
-(void)timerCallback { 
    [timer release]; 
    [self getNewMessages]; 
} 

//############################################################################################################################## 
//#################        CONNECTION PRAGMAS           #################// 
//############################################################################################################################## 
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    [receivedData setLength:0]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [receivedData appendData:data]; 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 

    if (chatParser) 
     [chatParser release]; 

    if (messages == nil) 

     messages = [[NSMutableArray alloc] init]; 

    chatParser = [[NSXMLParser alloc] initWithData:receivedData]; 
    [chatParser setDelegate:self]; 
    [chatParser parse]; 

    [receivedData release]; 
    [messageList reloadData]; 

    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector: @selector(timerCallback)]]; 
    //[invocation setTarget:self]; 
    [invocation setSelector:@selector(timerCallback)]; 
    timer = [NSTimer scheduledTimerWithTimeInterval:5.0 invocation:invocation repeats:NO]; 
} 

//############################################################################################################################## 
//#################       PARSING THE MESSAGE XML FILE LIST        #################// 
//############################################################################################################################## 
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 

    if ([elementName isEqualToString:@"message"]) { 

     msgAdded = [[attributeDict objectForKey:@"added"] retain]; 
     msgId = [[attributeDict objectForKey:@"id"] intValue]; 

     msgUser = [[NSMutableString alloc] init]; 
     msgText = [[NSMutableString alloc] init]; 
     msgText2 = [[NSMutableString alloc] init]; 
     msgImage = [[NSMutableString alloc] init]; 
     msgVideo = [[NSMutableString alloc] init]; 

     inUser = NO; 
     inText = NO; 
     inText2 = NO; 
     inImage = NO; 
     inVideo = NO; 
    } 
    if ([elementName isEqualToString:@"user"])  { inUser = YES; } 
    if ([elementName isEqualToString:@"text"])  { inText = YES; } 
    if ([elementName isEqualToString:@"subtext"]) { inText2 = YES; } 
    if ([elementName isEqualToString:@"image"]) { inImage = YES; } 
    if ([elementName isEqualToString:@"ytvideo"]) { inVideo = YES; } 


} 
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

    if (inUser) { [msgUser appendString:string]; } 
    if (inText) { [msgText appendString:string]; } 
    if (inText2) { [msgText2 appendString:string]; } 
    if (inImage) { [msgImage appendString:string];} 
    if (inVideo) { [msgVideo appendString:string];} 

} 
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 

    if ([elementName isEqualToString:@"message"]) { 

     [messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:msgAdded,@"added",msgUser,@"user",msgText,@"text",msgText2,@"subtext",msgImage,@"image",msgVideo,@"ytvideo",nil]]; 

     [[messages reverseObjectEnumerator] allObjects]; 

     lastId = msgId; 

     [msgAdded release]; 
     [msgUser release]; 
     [msgText release]; 
     [msgText2 release]; 
     [msgImage release]; 
     [msgVideo release]; 

    } 

    if ([elementName isEqualToString:@"user"] ) { inUser = NO;} 
    if ([elementName isEqualToString:@"text"] ) { inText = NO;} 
    if ([elementName isEqualToString:@"subtext"] ) { inText2 = NO;} 
    if ([elementName isEqualToString:@"image"] ) { inImage = NO;} 
    if ([elementName isEqualToString:@"ytvideo"]) { inVideo = NO;} 
} 

//############################################################################################################################## 
//#################       PARSING FINISHED - START DISPLAYING        #################// 
//############################################################################################################################## 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 
-(NSInteger)tableView:(UITableView *)myTableView numberOfRowsInSection:(NSInteger)section { 
    return (messages == nil) ? 0 : [messages count]; 
} 
-(UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:@"newsCustomCell"]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"newsCustomCell" owner:self options:nil]; 
     cell = (UITableViewCell *)[nib objectAtIndex:0]; 
    } 

    NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row]; 
    UILabel *timeDate = (UILabel *)[cell viewWithTag:1]; 
    timeDate.text = [itemAtIndex objectForKey:@"added"]; 
    UILabel *userL = (UILabel *)[cell viewWithTag:2]; 
    userL.text = [itemAtIndex objectForKey:@"user"]; 
    UILabel *textL = (UILabel *)[cell viewWithTag:3]; 
    textL.text = [itemAtIndex objectForKey:@"text"]; 
    UILabel *textL2 = (UILabel *)[cell viewWithTag:4]; 
    textL2.text = [itemAtIndex objectForKey:@"subtext"]; 
    UILabel *imageL = (UILabel *)[cell viewWithTag:5]; 
    imageL.text = [itemAtIndex objectForKey:@"image"]; 
    UILabel *videoL = (UILabel *)[cell viewWithTag:6]; 
    videoL.text = [itemAtIndex objectForKey:@"ytvideo"]; 

    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]]; 

    NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row]; 

    NSString *selectTime = [itemAtIndex objectForKey:@"added"]; 

    NSString *selectUser = [itemAtIndex objectForKey:@"user"]; 

    NSString *selectMessage = [itemAtIndex objectForKey:@"text"]; 

    NSString *selectMessage2 = [itemAtIndex objectForKey:@"subtext"]; 

    NSString *selectImage = [itemAtIndex objectForKey:@"image"]; 

    NSString *selectVideo = [itemAtIndex objectForKey:@"ytvideo"]; 

    dvController.selectedTime = selectTime; 
    dvController.selectedUser = selectUser; 
    dvController.selectedImage = selectImage; 
    dvController.selectedMessage = selectMessage; 
    dvController.selectedMessage2 = selectMessage2; 
    dvController.selectedVideo = selectVideo; 


    [self.navigationController pushViewController:dvController animated:YES]; 
    [dvController release]; 
    dvController = nil; 
} 

//############################################################################################################################## 
//#################       PARSING FINISHED - START DISPLAYING        #################// 
//############################################################################################################################## 
-(void)viewDidLoad {  
    messageList.dataSource = self; 
    messageList.delegate = self; 

    //@@@@@@@@TRY 
    [[messages reverseObjectEnumerator] allObjects]; 

    [self getNewMessages]; 
    [super viewDidLoad]; 

} 



@end 

을 제외한 모든 방법은 나를 위해 작동하지 않았다, 제발 도와주세요!

+0

가능한 중복 ([? 내가 목표 - C에서 NSArray를 반전시킬 수 방법]을 http://stackoverflow.com/questions/586370/how-can -i-reverse-a-nsarray-in-objective-c) – AlexVogel

답변

13

당신은 뭔가를해야합니다.

+0

그리고 내가 이것을 구현합니까? –

+0

나는 매우 혼란스러워! –

3

이 같은있는 NSArray의 순서를 취소 할 수 있습니다 : 당신은 다음 messagesreversedMessages를 할당하거나 당신이 그것으로 원하는 다른 무엇이든 할 수

NSMutableArray* reversedMessages = [NSMutableArray arrayWithCapacity:[messages count]]; 
NSEnumerator* reverseEnumerator = [messages reverseObjectEnumerator]; 
for (id object in reverseEnumerator) 
{ 
    [reversedMessages addObject:Object]; 
} 

:

- (NSArray *)reverseArray:(NSArray*)array { 
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:[array count]]; 
    NSEnumerator *enumerator = [array reverseObjectEnumerator]; 
    for (id element in enumerator) { 
     [mArray addObject:element]; 
    } 
    return mArray; 
} 
59

그것을 할 수있는 가장 쉬운 방법은 다음과 같습니다

NSArray *reversed = [[yourArray reverseObjectEnumerator] allObjects]; 
+4

간단히하기 위해 정답은 이쪽이어야합니다. – Reimius