将委托设置为对象

时间:2013-08-24 23:30:17

标签: ios objective-c delegates

这是我第一次尝试将UITableView委托/数据源设置为对象的实例。

我有一个由一个名为hMain的类管理的UIView,以及一个名为vTable的类实例管理的主内部的UITableView。

hMain.h:

@interface hMain : UIViewController
@property (strong, nonatomic) IBOutlet vTable *voteTbl;   
@end

hMain.m:

- (void)viewDidLoad
{
[super viewDidLoad];

voteTbl = [[vTable alloc]init];
[self.voteTbl setDelegate:voteTbl];
[self.voteTbl setDataSource:voteTbl];   

}

vTable.h:

@interface vTable : UITableView <UITableViewDelegate , UITableViewDataSource>
@end

vTable.M:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [UITableViewCell configureFlatCellWithColor:[UIColor greenSeaColor] selectedColor:[UIColor wetAsphaltColor] reuseIdentifier:CellIdentifier inTableView:(UITableView *)tableView];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell configureFlatCellWithColor:[UIColor greenSeaColor] selectedColor:[UIColor wetAsphaltColor]];
    }

    cell.textLabel.text = @"Hello there!";

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Row pressed!!");
}

这是我第一次偏离IB并以编程方式做事,所以我不确定我的设置委托和事情是否正确。这也是我第一次尝试并在自我之外设置委托/数据源。

我的问题是桌子每次都是空白的。

1 个答案:

答案 0 :(得分:1)

什么是vTable?看起来你有一个&#34; voteTable&#34; class(BTW:类应以大写char开头,实例变量应以小写开头)。无论如何,看起来你的主要问题是你忘了将表添加为子视图并设置其框架。例如:

self.voteTbl = [[VoteTable alloc] init];
self.voteTbl.delegate = self;
self.voteTbl.dataSource = self;
self.voteTbl.frame = self.view.bounds;
[self.view addSubView:self.voteTbl];
相关问题