【问题标题】:UITableView gives empty table, does not load dataUITableView 给出空表,不加载数据
【发布时间】:2011-02-05 13:20:45
【问题描述】:

当包含我的表的视图是主(第一个)视图时,一切正常。

但是,当它不是第一个视图并且我切换到该视图时,我的表不会加载数据并且我得到一个空表

使用 NSLog 我可以看出程序没有调用 numberOfRowsInSection 和 cellForRowAtIndexPath

我已经声明了<UITableViewDataSource, UITableViewDelegate>IBOutlet UITableview *tableView。它们也在 InterfaceBuilder 中连接。

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

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

更新:

我尝试了 [tableView reloadData],但什么也没发生。
除了 dealloc,我不会在任何地方发布 tableView。

这是一些代码:
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 之类的表格方法。

【问题讨论】:

  • 你是不是在 dealloc 以外的任何地方发布 tableView...?
  • 只需检查您用作数据源的数组或字典内容。我猜你里面没有任何数据。放一些代码。我们很容易为您提供帮助。
  • 如果您找到了这个问题的答案,请将其添加为这个问题的答案(并且只是一个答案)。无需编辑您的问题以包含答案。

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


【解决方案1】:

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

除了告诉一件事,如果

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

正在被调用。如果是,那么你已经通过了 0。 iphone-sdk-3.0 及更高版本不接受“0”。

谢谢,

马杜普

【讨论】:

  • 不连接delegate和数据源到table view,怎么能回调?
  • @Manjunath :我要求检查连接是否正常,然后是否调用了 numberofsections。好的。请仔细阅读帖子,然后做出反应或评论。不要仅仅为了添加而添加 cmets 和 votes。
  • 我应该告诉你在回答之前仔细阅读问题。因为他提到“当包含我的表的视图是主(第一个)视图时,一切正常。但是,当它不是第一个视图并且我切换到该视图时,我的表不会加载数据并且我得到一个空表。”不要为了发布而发布答案!!!
  • 我已经为 numberOfSections 返回 1
【解决方案2】:
- (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 数组编写访问器。让我知道会发生什么。

【讨论】:

  • 在您投反对票时发表评论。不要在这里表现出你愚蠢的态度。
  • 感谢 Manjunath,但该修复不起作用(错误:无法设置 obkect - 只读或未找到 setter)。从我的视图中访问位置数组没有问题。问题是视图没有调用加载单元数据的函数。我认为我加载视图的方式存在问题。
  • 你能检查位置数组中的对象数量吗?如果没有对象,则不会正确创建单元格:)。只需检查 Places 数组计数并通知我 :)
【解决方案3】:

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

【讨论】:

  • 我已经正确分配了数据源和委托,我检查了很多次,但它没有触发表格要求单元格。
猜你喜欢
  • 2016-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多