好的,所以我一直在摆弄一些iOS开发,我对此很陌生。通常更多的是PHP和JavaScript的人。但这是我的问题...在我的应用程序中我正在创建从本地xml文件中提取一个friendslist(它将指向生成用户friendslist的服务器上的php页面,但为了简单起见,我调试和开发)使用本地。)
我有一个View Controller(storybaord),当我点击“friends”时加载,截至目前,视图加载但没有数据,在调试器中我可以看到它从XML中提取数据,所以我想,好吧也许TableView永远不会被调用,所以我在
上放了一个断点 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
但该应用仍然继续加载页面,
所以回顾一下我有一个View Controller,带有搜索栏,工具栏和一个表视图,表视图有一个占位符单元格给定标识符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
时它会中断答案 0 :(得分:1)
您忘记分配friendList
实例变量。在viewDidLoad
的开头(或在初始化程序中更好)执行以下操作:
friendList = [[NSMutableArray alloc] initWithCapacity:0];
如果friendList
未初始化,则其值为nil
。在Objective-C中向nil
发送消息不会使应用程序崩溃,它不会执行任何操作。另请参阅this SO question。
在您的代码中,调用[friendList addObject:botanicalName]
根本不执行任何操作。稍后,当您在[friendList count]
中调用numberOfRowsInSection
时,您将返回0(零)到表视图。由于表视图认为没有行,因此永远不会调用cellForRowAtIndexPath
。