简体   繁体   中英

numberOfRowsInSection not being called for UITableView

I am having some issues where my UITableView is not reloading, I have redone the linking of the outlet to make sure that was not the issue. I have also tried using [self table] reloadData] but that does not seem to work either. I have debugged the issues, but it simply skips reloadData and the app keeps running like the code is not even there.

Top part of my .m file, nothing is done in 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;
}
}

My .h file

#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;

Thanks for any help!

add this lines:

- (void)viewDidLoad {
  [super viewDidLoad];

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

but much easier would be to set the datasource in interface-build aka the XIB file.

尝试这个

@interface SettingsCourses : UIViewController <UITableViewDelegate,UITableViewDataSource>

You do not appear to be setting the table delegate or datasource. To do this just add the following code when in your unit method

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

YOu might want to try to disconnect and reconnect datasource and delegate in the interface builder. IB sometimes "forgets" connections.

I had setup a UITableViewController in IB, but its class name didn't match the UITableViewController file I had. Rookie!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM