2011-09-17 37 views
9

我有下面的代码,我怎么能扭转messages Array,参见下文:扭转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 

我已经搜索了逆转的NSArray但所有方法都没有为我工作,帮助请!

+0

可能重复[?我怎样才能扭转在Objective-C的NSArray一(http://stackoverflow.com/questions/586370/how-can -i-reverse-a-nsarray-in-objective-c) – AlexVogel

回答

13

你需要的东西,如:

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

然后,您可以分配reversedMessagesmessages还是你想用它的任何东西。

+0

我在哪里实现这个? –

+0

我很困惑! –

3

可以扭转一个NSArray的顺序是这样的:

- (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