0

好的,所以我一直在摆弄一些 iOS 开发,我对此很陌生。通常更多的是 PHP 和 JavaScript 人。但这是我的问题......在我的应用程序中,我正在创建从本地 xml 文件中提取好友列表(它将指向服务器上生成用户好友列表的 php 页面,但为了调试和开发的简单起见,我是使用本地。)

我有一个视图控制器(storybaord),当我单击“朋友”时加载,截至目前,视图加载但没有数据,在调试器中我可以看到它从 XML 中提取数据,所以我想,好吧也许 TableView 永远不会被调用,所以我在

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

但是app还是继续加载页面,

回顾一下,我有一个视图控制器,带有一个搜索栏、工具栏和一个表格视图,表格视图有一个占位符单元格,给定标识符 Cell,这里是 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

这是friendsListTableViewController.m文件

//
//  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

//编辑//

多亏了下面评论中列出的一些建议,在它不会在 numberOfSectionsInTableView 或 numberOfRowsInSection 中断之前,我走得更远了,但是现在当我在 numberOfSectionsInTableView 或 numberOfRowsInSection 上中断但仍然不是 cellForRowAtIndexPath 时它会中断

4

1 回答 1

1

您忘记分配friendList实例变量。在开始时viewDidLoad(甚至在初始化程序中更好)执行以下操作:

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

如果friendList没有初始化它有一个nil值。在 Objective-C 中发送消息nil不会使应用程序崩溃,它只是不做任何事情。另请参阅此 SO question

在您的代码中,调用[friendList addObject:botanicalName]根本不执行任何操作。稍后,当您调用[friendList count]in时numberOfRowsInSection,您将向表视图返回 0(零)。由于表视图认为没有行,它永远不会调用cellForRowAtIndexPath.

于 2013-01-23T02:03:38.990 回答