加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 反转NSArray的担心?

发布时间:2020-12-14 19:48:24 所属栏目:百科 来源:网络整理
导读:我有以下代码,我如何反转消息数组,见下文: #import "newsViewController.h"#import "DetailViewController.h"@implementation newsViewController@synthesize messageList;//############################################################################
我有以下代码,我如何反转消息数组,见下文:

#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,但所有的方法都没有为我工作,请帮助!

解决方法

你需要这样的东西:

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

然后,您可以向消息分配reverseMessage,或者执行任何您想要的消息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读