UITableView给出空表,不加载数据 [英] UITableView gives empty table, does not load data
问题描述
当保存我的表的视图是主(第一)视图时,一切正常.
Everything works fine when the view that holds my table is the main (first) view.
但是,当它不是第一个视图时,我切换到该视图时,我的表不会加载数据,并且会得到一个空表.
However, when it's not the first view and I switch into that view, my table does not load data and I get an empty table.
使用NSLog,我可以确定程序没有未调用numberOfRowsInSection和cellForRowAtIndexPath .
Using NSLog I can tell that the program is not invoking numberOfRowsInSection and cellForRowAtIndexPath.
我已经声明了<UITableViewDataSource, UITableViewDelegate>
,IBOutlet UITableview *tableView
.它们也连接在InterfaceBuilder中.
I have <UITableViewDataSource, UITableViewDelegate>
, IBOutlet UITableview *tableView
all declared. They are also connected in the InterfaceBuilder.
我尝试使用viewWillAppear
和[tableView reloadData]
,但这没有帮助.
I tried using viewWillAppear
and [tableView reloadData]
but that did not help.
我是iPhone开发的新手,感谢您的帮助!
I'm new to iPhone development and your help is appreciated!
更新:
我尝试了[tableView reloadData],但没有任何反应.
我没有释放tableView,而是释放分配.
I tried [tableView reloadData] but nothing happened.
I'm not releasing tableView anywhere but dealloc.
以下是一些代码:
appDelegate
Here is some code:
appDelegate
//
// tpbAppDelegate.m
// tpb
#import "listController.h"
#import "tpbAppDelegate.h"
#import "tpbViewController.h"
@implementation tpbAppDelegate
@synthesize window;
@synthesize viewController;
@synthesize navController;
@synthesize toolbar;
@synthesize btnMyLoc;
@synthesize places; //array that holds data from the XML file
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//add places into an array that can be used by other views (Table)
rssList = [[NSMutableArray alloc] initWithCapacity:1];
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *xmlFile = [paths stringByAppendingPathComponent:@"tourplay.xml"];
NSURL *xmlURL = [NSURL fileURLWithPath:xmlFile isDirectory:NO];
NSLog(@"DATA URL: %@", xmlURL);
NSXMLParser *firstParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[firstParser setDelegate:self];
[firstParser parse];
// Resize window for toolbar:
CGRect frame = viewController.view.frame;
frame.size.height -= toolbar.frame.size.height;
viewController.view.frame = frame;
//[window addSubview:viewController.view];
navController.viewControllers = [NSArray arrayWithObject:viewController];
navController.view.frame = frame;
[window addSubview:navController.view];
[window makeKeyAndVisible];
}
- (IBAction)showList:(id)sender{
//Switch to table view on segment control change
UISegmentedControl *segmentControl = (UISegmentedControl *)sender;
NSString *curSelection = [NSString stringWithFormat:@"%d", [segmentControl selectedSegmentIndex]];
//[segmentControl titleForSegmentAtIndex: [segmentControl selectedSegmentIndex]]];
NSLog(@"pressed button %@", curSelection);
if ([curSelection isEqualToString:@"1"]){
NSLog(@"TABLE SELECTED");
listController *listTable = [[listController alloc] init];
[navController pushViewController:listTable animated:YES]; //SWITCH TO TABLE VIEW (listController)
} else {
tpbViewController *tpb = [[tpbViewController alloc] init];
NSLog(@"MAP SELECTED");
[navController pushViewController:tpb animated:YES];
}
}
#pragma mark Praser Methods
//Parse XML into an array
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
//NSLog(@"Started parsing");
if ([elementName compare:@"tour_point"] == NSOrderedSame) {
[self.places addObject:[[NSDictionary alloc] initWithObjectsAndKeys:
//[attributeDict objectForKey:@"tour_point_id"],@"tour_point_id",
[attributeDict objectForKey:@"name"],@"name",
[attributeDict objectForKey:@"tour_html"],@"tour_html",
[attributeDict objectForKey:@"audio_src"],@"audio_src",
nil]];
} else if ([elementName compare:@"title"] == NSOrderedSame) {
titlename = (NSString *)[attributeDict objectForKey:@"titlename"];
NSLog(@"Done parsing %@ points", titlename);
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@"Parser end");
[parser release];
}
- (void)dealloc {
[viewController release];
[rssList release];
[places release];
[toolbar release];
[window release];
[super dealloc];
}
@end
listController.h-表类
#import <UIKit/UIKit.h>
@interface listController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
UITableView *tableView;
NSMutableArray *places;
NSString *titlename;
}
@property (nonatomic, retain) IBOutlet UITableView *tableView;
@end
listController.m-表实现
//
// listController.m
// tpb
//
//
#import "listController.h"
#import "tpbAppDelegate.h"
@implementation listController
@synthesize tableView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
//- (void)viewWillAppear {
tpbAppDelegate *delegate = (tpbAppDelegate *)[[UIApplication sharedApplication] delegate];
places = delegate.places;
NSLog(@"Loaded table view");
[super viewDidLoad];
// [tableView reloadSectionIndexTitles];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tv {
NSLog(@"Number of secions");
return 1;
}
- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section {
NSLog(@"GETTING COUNT");
return [places count];
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Assigning Cells");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [[places objectAtIndex:indexPath.row] objectForKey:@"name"];
//NSLog(@"count %@", [[places objectAtIndex:indexPath.row] objectForKey:@"name"]);
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"ROW clicked");
[tv deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
NSLog(@"Unloaded tableview");
}
- (void)dealloc {
[tableView release];
[places release];
[super dealloc];
}
@end
-到目前为止,我知道viewDidLoad已加载,但是未调用诸如cellRowAtIndexPath之类的表方法.
-- So far, I know that viewDidLoad loads, but the table meathods such as cellRowAtIndexPath are not invoked.
推荐答案
- (IBAction)showList:(id)sender{
//Switch to table view on segment control change
UISegmentedControl *segmentControl = (UISegmentedControl *)sender;
NSString *curSelection = [NSString stringWithFormat:@"%d", [segmentControl selectedSegmentIndex]];
//[segmentControl titleForSegmentAtIndex: [segmentControl selectedSegmentIndex]]];
NSLog(@"pressed button %@", curSelection);
if ([curSelection isEqualToString:@"1"]){
NSLog(@"TABLE SELECTED");
listController *listTable = [[listController alloc] init];
//Try this
listTable.places = self.places; // set the array contents here and check
[navController pushViewController:listTable animated:YES]; //SWITCH TO TABLE VIEW (listController)
} else {
tpbViewController *tpb = [[tpbViewController alloc] init];
NSLog(@"MAP SELECTED");
[navController pushViewController:tpb animated:YES];
}
}
别忘了在PlaceController的ListController中编写访问器.让我知道会发生什么.
Dont forget to write the accessors in ListController for places array. And let me know what happens.
这篇关于UITableView给出空表,不加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!