UITableView的numberOfRowsInSection方法没有被调用

10
我遇到了UITableView不刷新的问题,我已经重新连接了输出口以确保那不是问题。我也尝试使用[self table] reloadData],但似乎也不起作用。我已经调试了这个问题,但它只是跳过了reloadData,应用程序像代码不存在一样继续运行。
.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;

感谢任何帮助!


你设置了表视图的数据源吗? - UIAdam
表格在xib中是否与“delegate”和“dataSource”连接?您是否在.h文件中添加了“UITableViewDelegate”和“UITableViewDataSource”? - Mat
6个回答

29

添加这些代码:

- (void)viewDidLoad {
  [super viewDidLoad];

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

不过,更简单的方法是在界面构建中设置数据源,也就是在 XIB 文件中。


1
也帮了我大忙。忘记连接这个了!哎呀 - Robert J. Clegg
这对我帮助很大。 - Alec.

1

试试这个

@interface SettingsCourses : UIViewController <UITableViewDelegate,UITableViewDataSource>

0

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


0
对我来说,错误在于我在func numberOfSections(in tableView: UITableView) -> Int中返回了0

0
我在IB中设置了一个UITableViewController,但它的类名与我拥有的UITableViewController文件不匹配。新手错误!

0

您似乎没有设置表格的代理或数据源。要做到这一点,只需在您的单元方法中添加以下代码:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接