UItableviewController 不加载数据

Posted

技术标签:

【中文标题】UItableviewController 不加载数据【英文标题】:UItableviewController doesn't load data 【发布时间】:2011-06-10 15:06:18 【问题描述】:

大家好,我有一个 UITableviewcontroller 类,我从 otherview 调用它并将其推送到导航控制器上。

Streets_view_Controller_iPhone * street_controller = [[Streets_view_Controller_iPhone alloc]initWithStyle:UITableViewStylePlain];
        street_controller.riding_number =  [assignment objectForKey:@"Riding_Number"];
        street_controller.polling_number = [assignment objectForKey:@"Poll"];




        [self.navigationController pushViewController:street_controller
                                             animated:YES];

这是我的 tableview 控制器类 "Streets_view_Controller_iPhone.m"

#import "Streets_view_Controller_iPhone.h"


@implementation Streets_view_Controller_iPhone
@synthesize riding_number;
@synthesize polling_number;
@synthesize data;


- (NSString *)dataFilePathwithFilename:(NSString*)name

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:name];


- (id)initWithStyle:(UITableViewStyle)style

    self = [super initWithStyle:UITableViewStylePlain];
    if (self)
    
        // Custom initialization
    
    return self;


- (void)dealloc

    [riding_number release];
    [polling_number release];
    [data release];
    [super dealloc];


- (void)didReceiveMemoryWarning

    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.


#pragma mark - View lifecycle

- (void)viewDidLoad

    self.title = @"Streets";

    NSString * filepath = [self dataFilePathwithFilename:[NSString stringWithFormat:@"%@_%@.plist",self.riding_number,self.polling_number]];

    NSLog(@"%@",filepath);

    NSArray * streets = [[NSArray alloc] initWithContentsOfFile:filepath];

    self.data = streets;

    [streets release];

    [super viewDidLoad];
    [self.tableView reloadData];




- (void)viewDidUnload

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;


- (void)viewWillAppear:(BOOL)animated

    [super viewWillAppear:animated];


- (void)viewDidAppear:(BOOL)animated

    [super viewDidAppear:animated];


- (void)viewWillDisappear:(BOOL)animated

    [super viewWillDisappear:animated];


- (void)viewDidDisappear:(BOOL)animated

    [super viewDidDisappear:animated];


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return 0;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section


   // return [data count];
   // NSLog(@"%d",[data count]);

    return 2;



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

    static NSString * StreetLevelCell= @"StreetLevelCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             StreetLevelCell];

    if (cell == nil) 
    
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:StreetLevelCell] autorelease];

       // NSUInteger row = [indexPath row];
       // NSDictionary * streets_data = [data objectAtIndex:row];
        //cell.textLabel.text = [streets_data objectForKey:@"Street_name"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = @"test for streets";
        return cell;
    



    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:[NSArray arrayWithObject: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];
     [detailViewController release];
     */


@end

但由于某种原因,我在单元格中没有得到任何东西,只是视图的标题,即 Streets 。和空表

【问题讨论】:

【参考方案1】:

由于以下原因,您的表格中不会显示任何内容:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return 0;

您的表格中必须至少有一个部分才能显示任何数据。试试return 1; 看看是否有效。

【讨论】:

谢谢,我知道我问这样的问题是愚蠢的

以上是关于UItableviewController 不加载数据的主要内容,如果未能解决你的问题,请参考以下文章

重新加载 UITableViewController

UITableViewController 子类不会加载

从 UITableViewController 重新加载 tableView

从 UITableViewController 加载通用视图

UITableviewcontroller 中的 UITableview - 重新加载时未调用 cellforrowatindexpath

在 UINavigationController 内的 UIViewController 内加载 UITableViewController