numberOfRowsInSection没有被调用UITableView

问题描述:

我有一些问题,我的UITableView没有重新加载,我已经重做了连接的插座,以确保这不是问题。我也尝试过使用[self table] reloadData],但似乎也没有效果。我已经调试过这些问题,但它只是跳过reloadData,应用程序仍然像代码甚至不在那里运行。numberOfRowsInSection没有被调用UITableView

我.m文件的顶部,没有在viewDidLoad中

#import "SettingsCourses.h" 

@implementation SettingsCourses 
@synthesize settingsCourses; 
@synthesize Delegate; 
@synthesize BackButton; 
@synthesize DeleteButton; 
@synthesize table; 
@synthesize courses; 
@synthesize dataHandler; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
    // Custom initialization 
    dataHandler = [[DataHandler alloc] init]; 
    dataHandler.coursesDelegate = self; 
    courses = [dataHandler fetchCourses]; 
    NSLog(@"Debug Count: %i", [courses count]); 
} 
[table reloadData]; 
return self; 
} 

- (void) viewWillAppear:(BOOL)animated { 
[table reloadData]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
if (courses) { 
    NSLog(@"Count: %i", [courses count]); 
    return [courses count]; 
} 
else { 
    NSLog(@"Count: 0"); 
    return 0; 
} 
} 

我.h文件中做

#import <UIKit/UIKit.h> 
#import "dataHandler.h" 

@interface SettingsCourses : UIViewController 

@property (strong, nonatomic) DataHandler *dataHandler; 
@property (strong, nonatomic) NSMutableArray *courses; 
@property (strong, nonatomic) IBOutlet UIView *settingsCourses; 
@property (nonatomic, strong) id Delegate; 
@property (weak, nonatomic) IBOutlet UIButton *BackButton; 
@property (weak, nonatomic) IBOutlet UIButton *DeleteButton; 
@property (weak, nonatomic) IBOutlet UITableView *table; 


- (IBAction)Delete:(id)sender; 
- (IBAction)Back:(id)sender; 

感谢您的帮助!

+0

您是否设置了表视图的dataSource? – UIAdam 2012-04-14 17:22:46

+0

该表是否与xib中的'delegate'和'dataScource'连接?您是否在.h文件中添加了'UITableViewDelegate'和'UITableViewDataSource'? – Mat 2012-04-14 17:24:50

添加此行:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.table.dataSource = self; 
    self.table.delegate = self; 
} 

,但很容易将设置在DataSource接口,建立又名XIB文件。

+0

谢谢你的工作! :) – 2012-04-14 17:44:30

+0

你也帮我解决了 – RollRoll 2012-08-22 01:53:47

+1

也帮助过我。忘记连接这个!哎呀 – Tander 2014-07-21 11:07:54

您似乎没有设置表委托或数据源。要做到这一点只需添加以下代码时,在你的单位方法

table.dataSource = self or wherever your data source is; 
table.delegate = self: 

试试这个

@interface SettingsCourses : UIViewController <UITableViewDelegate,UITableViewDataSource> 

你可能想尝试断开并重新连接数据源和委托在界面生成器。 IB有时会“忘记”连接。

我已经在IB中设置了UITableViewController,但是它的类名与我的UITableViewController文件不匹配。新秀!