简体   繁体   中英

Push to another view controller taking too much time in iphone

I am having an app in which I am opening initially a grid of images coming from webservice.

I am using DTGridView by danieltull.

Those images are from different categories like Natural, Arts, Paintings etc.

Initially when the app launches, The grid contain images from all the categories.

There is a category button on main screen, clicking on which user will be pushed to another view where different categories are mentioned like Natural, Arts, Paintings etc.

If a user clicks on any of the category he will be again lead to the Grid of images but will contain images only from that selected category.

The problem is that when the user clicks on any of the category then it takes too much time to navigate to the Grid View.

I am taking data from webservice when user clicks on any of the category.

I want to navigate user to the Gridview as soon as he clicks on category and then want to show custom Spinner.

on selecting the category i am doing the below code and calling the method of the gridview.

 if ([self isPad])
    {
         vc = [[ViewController alloc]initWithNibName:@"ViewController_iPad" bundle:nil];
    }
 [self.navigationController pushViewController:vc animated:NO];
[vc getCategoryNames];

Please help me on this.

** Edited **

- (NSInteger)numberOfRowsInGridView:(DTGridView *)gridView
{
    if (countimages % 4 == 0)
    {
        return countimages/4;
    }
    else
    {

        return countimages/4 + 1;

    }

}

- (NSInteger)numberOfColumnsInGridView:(DTGridView *)gridView forRowWithIndex:(NSInteger)index
{
    return 4;
}

- (CGFloat)gridView:(DTGridView *)gridView1 heightForRow:(NSInteger)rowIndex
{
    return gridView1.frame.size.height / 3;
    // return 300;
   // NSLog(@"%f",gridView1.frame.size.height/3);

}

- (CGFloat)gridView:(DTGridView *)gridView1 widthForCellAtRow:(NSInteger)rowIndex column:(NSInteger)columnIndex
{
    return gridView1.frame.size.width / 4;
    //return 300;
  //  NSLog(@"%f",gridView1.frame.size.width/4);
}

- (DTGridViewCell *)gridView:(DTGridView *)gv viewForRow:(NSInteger)rowIndex column:(NSInteger)columnIndex {

    DTGridViewCell *cell = [gv dequeueReusableCellWithIdentifier:@"cell"];

    if (!cell) {
        cell = [[DTGridViewCell alloc] initWithReuseIdentifier:@"cell"];
  UIView* v=[[UIView alloc]init];
    v.frame=CGRectMake(0, 0, gridView.frame.size.width / 4, gridView.frame.size.height / 3);
    [cell addSubview:v];

    imgeIndex = 0;
    imgeIndex = (rowIndex * 4) + columnIndex;

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, gridView.frame.size.width / 4, gridView.frame.size.height / 3)];
    [btn setTag:imgeIndex+1];
    [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
    btn.contentMode = UIViewContentModeScaleAspectFit;
 if ([arr_imgurl count] <= imgeIndex)
        {
            btn.imageView.image=[UIImage imageNamed:@"logo_256.png"];
            return cell;
        }
        else
        {
            //NSLog(@"arr %d",[arr_imgurl count]);
            // NSLog(@"imgindex %d",imgeIndex);
            // NSLog(@"array %@",arr_imgurl);
            NSURL *url = [NSURL URLWithString:[arr_imgurl objectAtIndex:imgeIndex]];
            //NSLog(@"%@",url);
            [btn setImageWithURL:url placeholderImage:[UIImage imageNamed:@"logo_256.png"]];


        }
 btn.titleLabel.textColor = [UIColor whiteColor];

    [v addSubview:btn];
    [database close];

    return cell;
}

Here arr_imgurl contains imagepaths coming from webservice.

While navigating never call web services(reason: it will hold navigation till it receives the complete information we can't be sure about server speed all the time). So better navigate and then call service in thread and mean while show Screen with activity indicator on it so that user will think app is loading images.(Make sure images are loading lazily because user will not have patience to wait till all images are loaded). If you need a sample for this i'll code it and give you.

Most of the chances are that the loading of all the data for the grid in the new view controller takes the time.
There are plenty of different scenarios for this "incorrect" data loading.

It may be the web service loading synchronously in the main thread.
It may be images loading (in the grid delegate) synchronously in the main thread.
Etc.

Please post some code of the grid data initiation and grid delegate to better understand the exact problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM