UITableView提供空表,不加载数据

时间:2010-04-08 04:27:20

标签: iphone objective-c uitableview iphone-sdk-3.0

当持有我的表的视图是主(第一)视图时,一切正常。

然而,当它不是第一个视图并且我切换到该视图时,我的表格没有加载数据,我得到一个空表

使用NSLog我可以告诉程序没有调用numberOfRowsInSection和cellForRowAtIndexPath

我已宣布<UITableViewDataSource, UITableViewDelegate>IBOutlet UITableview *tableView。它们也在InterfaceBuilder中连接。

我尝试使用viewWillAppear[tableView reloadData],但这没有帮助。

我是iPhone开发的新手,非常感谢您的帮助!

更新:

我试过[tableView reloadData]但没有发生任何事情 我不是在任何地方发布tableView而是dealloc。

这是一些代码:
的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这样的表格。

3 个答案:

答案 0 :(得分:3)

如果未将表视图的数据源和委托链接到接口构建器中的文件所有者,则会发生此问题。在界面构建器中再次进行交叉检查,查看数据源的连接和tableview的委托是否正确地与文件所有者建立,并且您没有错误地将它们与视图相关联。

除了告诉一件事,

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

正在被召唤。如果是,那么你已经通过了0。从iphone-sdk-3.0及更高版本开始,“0”是不可接受的。

谢谢,

Madhup

答案 1 :(得分:0)

当你去显示你的tableview时,在它上面调用reloadData方法;如果您的数据源/委托被分配给正确的对象,那么将触发表视图向数据源/委托询问其单元格,并且它应该在那时显示。

答案 2 :(得分:0)

- (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];
    }
}

别忘了在ListController中为places数组编写访问器。让我知道会发生什么。