【问题标题】:numberOfRowsInSection not being called for UITableView没有为 UITableView 调用 numberOfRowsInSection
【发布时间】:2012-04-26 16:31:57
【问题描述】:

我遇到了一些我的 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;

感谢您的帮助!

【问题讨论】:

  • 你设置了table view的dataSource吗?
  • 表格是否与xib中的delegatedataScource相连? .h 文件中添加UITableViewDelegateUITableViewDataSource 了吗?

标签: iphone objective-c ios5 xcode4.2


【解决方案1】:

您似乎没有设置表委托或数据源。为此,只需在您的单元方法中添加以下代码

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

【讨论】:

    【解决方案2】:

    添加以下行:

    - (void)viewDidLoad {
      [super viewDidLoad];
    
      self.table.dataSource = self;
      self.table.delegate = self;
    }
    

    但更简单的是在 interface-build aka XIB 文件中设置数据源。

    【讨论】:

    • 也帮助了我。忘记连接了!哎呀
    • 这对我帮助无穷。
    【解决方案3】:

    试试这个

    @interface SettingsCourses : UIViewController <UITableViewDelegate,UITableViewDataSource>
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        我在 IB 中设置了一个 UITableViewController,但它的类名与我拥有的 UITableViewController 文件不匹配。菜鸟!

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-26
            • 2017-12-29
            相关资源
            最近更新 更多