Search code examples
objective-cjsonuitableviewnsmutablearraynsdictionary

Objective C JSON not leaving out first entry


Having an issue using a table view downloading data from a JSON file. The JSON has 4 entries and I want my code to ignore the first entry and just put the ones after the first one into the table view. With my code below it is getting the full 4 entries from the JSON and placing them all into the table view. I have tried everything but cannot seem to work my head around this.

Thanks,

CurtisB

    #import "TutorProfileView.h"

    @interface TutorProfileView ()

    @end

    @implementation TutorProfileView
    NSDictionary *TutordizDict = @"";
    NSMutableArray *TutordaObj;
    NSDictionary *Tutordadict;
    NSArray *Tutortrackinbud;


    - (void)viewDidLoad {
        [super viewDidLoad];
        [self downloadreviews];
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    -(UIStatusBarStyle)preferredStatusBarStyle{
        return UIStatusBarStyleLightContent;
    }
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {
        NSArray *udropofftrack = [TutordizDict objectForKey:@"tutific"];
        return [udropofftrack count];

    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return 88;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        static NSString *simpleTableIdentifier = @"TableViewCell2";



        TableViewCell2 *cell = (TableViewCell2 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil)

        {

            NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell2" owner:self options:nil];

            cell = [nibArray objectAtIndex:0];

        }
       cell.name.text = [NSMutableString stringWithFormat:@"%@",[Tutortrackinbud[indexPath.row] valueForKey:@"username"]];

      return cell;   
    }

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

    {

    }
    -(void)downloadreviews
    {

        NSString *subjects2 = @"tutor1";

        NSURL *blogURL = [NSURL URLWithString:[NSString stringWithFormat:@"getreviews.php?username=%@", subjects2]];
        NSLog(@"URL = %@", blogURL);

        NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

        if(jsonData == nil)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Please check your internet connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            return;
        }
        else{

            NSError *error = nil;
            TutordizDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
            Tutortrackinbud = [TutordizDict objectForKey:@"tutific"];

            NSUInteger *elements = [Tutortrackinbud count];
            NSLog(@"Number of Shows : %d", elements);

            if (elements == 0)
            {
                //[udropofftrack removeAllObjects];
                Tutortrackinbud=nil;
                [_tableViewObject reloadData];
               // self.noorder.hidden=FALSE;

            }


            else{

                NSString *success = [Tutortrackinbud[0] valueForKey:@"success"];
                if ([success  isEqual: @"0"])
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Your application failed to connect to uDropOff servers. Please update your application in the App Store." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert show];
                    return;

                }
                else
                {

                    {
                        for (int i = 1 ; i < [Tutortrackinbud count]; i++){
                            {
                              TutordaObj = [[NSMutableArray alloc] init];

                            }
                        }
                    }
                }
            }
        }
        [_tableViewObject reloadData];
    }

    @end

Solution

  • If I understand your question correctly you just wanted to skip the first element/entry from the array. why not just use Tutortrackinbud

    sample:

     -(void)downloadreviews{
    
       //<fetch data>...
    
            NSError *error = nil;
            TutordizDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
           NSMutableArray *temp = [[TutordizDict objectForKey:@"tutific"]mutableCopy];
    
            if([temp count] > 0){ // if there is an entry should we remove it, if we should just perform this; otherwise if we only remove if there are more than 1 change condition to 1
               [temp removeObjectAtIndex:0];
            }
    
            Tutortrackinbud = [temp copy];
            NSUInteger *elements = [Tutortrackinbud count];
            NSLog(@"Number of Shows : %d", elements);
    
            if (elements == 0)
            {
                //[udropofftrack removeAllObjects];
                Tutortrackinbud=nil;
                [_tableViewObject reloadData];
               // self.noorder.hidden=FALSE;
    
            }
    
    
            else{
    
                NSString *success = [Tutortrackinbud[0] valueForKey:@"success"];
                if ([success  isEqual: @"0"])
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Your application failed to connect to uDropOff servers. Please update your application in the App Store." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert show];
                    return;
    
                }
                else
                {
    
                    {
                        for (int i = 1 ; i < [Tutortrackinbud count]; i++){
                            {
                              TutordaObj = [[NSMutableArray alloc] init];
    
                            }
                        }
                    }
                }
            }
        }
        [_tableViewObject reloadData];
    }`