0

我有一个带有 UITableView 控件的简单表视图控制器。我已经在我的头文件中实现了 UITableViewDelegate 和 UITableViewDatasource 。我已将数据源和委托指定为包含 UITableView 的 ViewController。但是,没有一个表视图方法被触发。我已经发布了精简的源代码和显示委托/数据源的屏幕截图。为什么事件没有连接的可能原因是什么?

(仪表是一个包含值对象的 NSArray 的模型)

标题

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

@interface SitePickerViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong) GaugeList *gauges;

@end

执行

#import "SitePickerViewController.h"

@interface SitePickerViewController ()

@end

@implementation SitePickerViewController
@synthesize gauges;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rowCount = [gauges.gaugeList count];
    NSLog(@"numberOfRowsInSection called: %i\n", rowCount);
    return rowCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"cellForRowAtIndexPath\n");
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    return cell;
}

-(void)loadView{
    gauges = [[GaugeList alloc] initWithStateIdentifier:@"WV" andType:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    NSLog(@"viewDidLoad called: %i\n", [gauges.gaugeList count]);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

3 回答 3

0

在 viewDidLoad

tableView.delegate = self;

如果您不使用 XIB,还要分配 tableView。或者,如果 ut 使用 xib 将代表和数据源映射到文件所有者

于 2013-03-21T13:16:35.637 回答
0

使 SitePickerViewController 成为委托和数据源还不够。您还必须将该类设置为数据源和委托。确保也连接到该 IBOutlet。

@interface <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *functionTableView;
@end

@implementation
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.functionTableView.delegate = self;
    self.functionTableView.dataSource = self;
}

//Delegate and datasource methods down here
@end
于 2013-03-21T13:30:32.307 回答
0

问题在于您的loadView. 的任何实现都loadView必须实际为控制器创建主视图并将其分配给self.view. 正如您所拥有的,最终不会为您的视图控制器创建任何视图。

移动必须的方法调用viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];

    gauges = [[GaugeList alloc] initWithStateIdentifier:@"WV" andType:nil];

    // Do any additional setup after loading the view.
    NSLog(@"viewDidLoad called: %i\n", [gauges.gaugeList count]);
}
于 2013-03-21T22:03:33.677 回答