简体   繁体   English

连接DidFinishLoading后,其他功能无法运行?

[英]After connectionDidFinishLoading other function don't run?

I am using NSUrlConnection to go to a .ashx file to do a MYSQL Statement and return the result. 我正在使用NSUrlConnection转到.ashx文件,以执行MYSQL语句并返回结果。 Then put it in a table. 然后把它放在桌子上。

In this particular one i am getting Categories, which is all working fine and getting the details it should but after the connectionDidFinishLoading function nothing else happens. 在这种特殊的情况下,我得到的是Categories,它可以正常工作并获得应有的详细信息,但是在connectionDidFinishLoading函数之后,什么都没有发生。

I put breakpoints in everywhere after it and in it but nothing happens it just ends before it goes to the table entering. 我在断点之后和之后的任何地方都放置了断点,但是什么也没有发生,断点只是在进入表之前结束了。

Below is my code and if you need any information i have missed out, please ask. 以下是我的代码,如果您需要我错过的任何信息,请询问。

.h file .h文件

    #import <UIKit/UIKit.h>

    @interface Category_TableView : UIViewController <NSURLConnectionDelegate>
    {
        sqlite3 *database;
    }
    @property (retain, nonatomic) NSMutableArray *Category_Array;
    @property (strong, nonatomic) IBOutlet UITableView *Category_Table;
    @property (retain, nonatomic) NSString *type;
    @property (retain, nonatomic) NSString *Category;


    @property (retain, nonatomic) NSMutableData *CatData;

    @end

.m file .m文件

            #import "Category_TableView.h"
            #import "Cateogry_Cell.h"
            #import "JobList_Table.h"
            #import "JobList_Cell.h"
            #import <QuartzCore/QuartzCore.h>

            @interface Category_TableView ()

            @end

            @implementation Category_TableView

            - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
            {
                self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
                if (self) {
                    // Custom initialization


                }
                return self;
            }

            - (void)viewDidLoad
            {
                [super viewDidLoad];
                self.navigationController.navigationBar.hidden = YES;
                // Do any additional setup after loading the view.

                NSString *requestURL = [NSString stringWithFormat:@"http://"URL"/forms/AppForms/app_latest.ashx"];
                NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: requestURL]];
                NSURLConnection *con =[[NSURLConnection alloc] initWithRequest:request delegate:self];

                if(con)
                {
                    self.CatData = [[NSMutableData alloc] init];
                    self.Category_Array = [[NSMutableArray alloc] init];
                }
                else
                {
                    [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get connection. Please try again later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
                }

            }
            - (void)didReceiveMemoryWarning
            {
                [super didReceiveMemoryWarning];
                // Dispose of any resources that can be recreated.
            }
            - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
            {
                [self.CatData setLength:0];
            }

            - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
            {
                [self.CatData appendData:data];
            }
            - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
            {

                [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get connection. Please try again later"  delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];


            }
            - (void)connectionDidFinishLoading:(NSURLConnection *)connection
            {
                NSString *temp = [[NSString alloc] initWithData:self.CatData encoding:NSUTF8StringEncoding];
                NSArray *jobs = [temp componentsSeparatedByString:@"||||"];
                NSMutableDictionary *temp_dict = [[NSMutableDictionary alloc] init];
                NSArray *jobInfo = [[NSArray alloc] init];
                int count = 0;
                for(int i = 1; i < [jobs count]; i++)
                {
                    jobInfo = [[jobs objectAtIndex:i] componentsSeparatedByString:@"||"];
                    @try {

                        temp_dict = [[NSMutableDictionary alloc]init];
                        [temp_dict setObject:[jobInfo objectAtIndex:0] forKey:@"Category"];
                        [self.Category_Array addObject:temp_dict];
                        count++;
                    }
                    @catch (NSException *exception) {

                        [[[UIAlertView alloc] initWithTitle:@"Exception Thrown" message:exception.reason delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
                    }
                }
            }


            //#pragma mark - Table view data source


            - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
            {

                // Return the number of sections.
                return 1;
            }

            - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
            {
                // Return the number of rows in the section.
                return [self.Category_Array count];
            }


            - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
            {
                static NSString *CellIdentifier = @"Cateogry_Cell";
                Cateogry_Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
                // Configure the cell...


                [[[UIAlertView alloc] initWithTitle:@"In Table Function" message:@"Failed to try again later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];

                if(cell == nil) {
                    cell = [[Cateogry_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cateogry_Cell"];
                }

                NSDictionary *temp = [self.Category_Array objectAtIndex:[indexPath row]];
                cell.Category_Cell_Label.text = [temp objectForKey:@"Category"];

                return cell;
            }

            -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
            {
                if ([[segue identifier] isEqualToString:@"Cat_Cell_Push"])
                {
                    JobList_Table *DetailController = [segue destinationViewController];
                    NSIndexPath *MyIndexPath = [_Category_Table indexPathForSelectedRow];
                    int row = [MyIndexPath row];
                    DetailController.JobList_Array = @[_Category_Array[row]];


                }
            }

            @end

Issue is the NSURLConnection will work asynchronously. 问题是NSURLConnection将异步工作。 So when you get the data your tableview will be already populated with no data. 因此,当您获取数据时,表视图将已经没有数据。 So you need to reload your tableview, just add the folowing code as the last line of connectionDidFinishLoading: 因此,您需要重新加载表格视图,只需将以下代码添加为connectionDidFinishLoading:的最后一行connectionDidFinishLoading:

[Category_Table reloadData];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 connectionDidFinishLoading后返回collectionView函数 - Return to collectionView function after connectionDidFinishLoading 在connectionDidFinishLoading之后收到NSURLConnection数据 - NSURLConnection data received after connectionDidFinishLoading 在connectionDidFinishLoading:,NSURLConnection委托方法中发布后,无法收到通知 - can't receive a notification after posting inside connectionDidFinishLoading:, NSURLConnection delegate method 不要返回并等待其他函数的结果-目标c - Don't return and wait the result from other function - objective c 在connectionDidFinishLoading请求后选择其他xib - Choose different xib after connectionDidFinishLoading request 调用connectionDidFinishLoading后更新View Controller - Update View Controller After connectionDidFinishLoading is Called 15次快速调用后,NSURLConnection委托仅获得一次connectionDidFinishLoading - NSURLConnection delegate gets connectionDidFinishLoading only once after 15 rapid calls 在connectionDidFinishLoading完成后尝试使用performSegueWithIdentifier锁定到ViewController - Trying to use performSegueWithIdentifier to segue to ViewController after connectionDidFinishLoading is finished 上传所有数据之后和connectionDidFinishLoading之前,NSURLConnection崩溃 - NSURLConnection crashes after all the data is uploaded and before connectionDidFinishLoading sendSynchronousRequest / connectionDidFinishLoading - sendSynchronousRequest/connectionDidFinishLoading
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM