Domanda

Okay so i have been fiddling with some iOS development, I am fairly new to this. Usually more of a PHP and JavaScript guy. But here is my issue... in my app I am creating that pulls a friendslist from a local xml file (it will be pointing to a php page on a server that generates a users friendslist but for simplicity sake of debugging and development I am using local.)

I have a View Controller (storybaord) that is loaded when I click "friends", as of now, the view loads but with no data, in the debugger I can see that its pulling the data from the XML, so I thought, okay maybe the TableView is never being called, so i put a breakpoint on the

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

But the app still continued to load the page,

So to recap I have a View Controller, with a search bar, toolbar and a table view, the table view has a placeholder cell given the identifier Cell, heres the coding the for friendsListTableViewController.h

//
//  friendsListTableViewController.h
//  @ME
//
//  Created by Aaron Russell on 1/22/13.
//  Copyright (c) 2013 Aaron Russell. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "TBXML.h"
@interface friendsListTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, NSObject>{
    NSMutableArray *friendList;
    TBXML * tbxml;
    IBOutlet UIImage *imageFile;
}
@property (nonatomic, strong) NSMutableArray *_friends;
@property (nonatomic, strong) NSString *lname;

@end

and here is the friendsListTableViewController.m file

//
//  friendsListTableViewController.m
//  @ME
//
//  Created by Aaron Russell on 1/22/13.
//  Copyright (c) 2013 Aaron Russell. All rights reserved.
//

#import "friendsListTableViewController.h"

@interface friendsListTableViewController ()

@end

@implementation friendsListTableViewController


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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //USED TO CONNECT TO SERVERS XML
    //    NSData *xmlData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"www.soontobesite.com"]];
    //tbxml = [[TBXML alloc]initWithXMLData:xmlData];
    NSString *xmlData = [[NSBundle mainBundle] pathForResource:@"friendlist" ofType:@"xml"];
    NSData *data = [[NSData alloc] initWithContentsOfFile:xmlData];
    tbxml = [[TBXML alloc]initWithXMLData:data];

    //strings
    // Obtain root element
    TBXMLElement * root = tbxml.rootXMLElement;
    if (root)
    {
        TBXMLElement * elem_PLANT = [TBXML childElementNamed:@"friend" parentElement:root];
        while (elem_PLANT !=nil)
        {
            TBXMLElement * elem_BOTANICAL = [TBXML childElementNamed:@"fname" parentElement:elem_PLANT];
            NSString *botanicalName = [TBXML textForElement:elem_BOTANICAL];
            [friendList addObject:botanicalName];
            elem_PLANT = [TBXML nextSiblingNamed:@"friend" searchFromElement:elem_PLANT]; //IF I CALL BREAKPOINT ON THIS LINE THE SIMULATOR BREAKS
        }

        //TBXMLElement *fname = [TBXML childElementNamed:@"fname" parentElement:elem_PLANT];
    }

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [friendList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [friendList objectAtIndex:indexPath.row];

    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

//EDIT//

I got a little further, thanks to some advice listed in the comments below before it wouldnt break at numberOfSectionsInTableView or numberOfRowsInSection but now it will break when i put a break on the numberOfSectionsInTableView or numberOfRowsInSection but still not the cellForRowAtIndexPath

È stato utile?

Soluzione

You forgot to allocate the friendList instance variable. At the beginning of viewDidLoad (or even better in the initializer) do the following:

friendList = [[NSMutableArray alloc] initWithCapacity:0];

If friendList is not initialized it has a nil value. Sending a message to nil in Objective-C does not crash the application, it just does not do anything. Also see this SO question.

In your code, invoking [friendList addObject:botanicalName] simply does nothing. Later on, when you invoke [friendList count] in numberOfRowsInSection, you will return 0 (zero) to the table view. Since the table view thinks that there are no rows it will never call cellForRowAtIndexPath.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top