2012-07-30 91 views
1

我想创建一个包含在另一个视图中的自定义UITableView。我已经能够将视图设置成几乎和我想要的一样。我只是想知道,我最需要的两种方法(tableView:cellForRowAtIndexPathtableView:didSelectRowAtIndexPath)怎么没有被调用。请注意,我没有使用UITableViewController,但我正在使用UIViewController来控制tableView。创建一个自定义的UITableView

enter image description here

这里是我的代码

- (void)loadView { 
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 
    UIView * contentView = [[UIView alloc] initWithFrame:applicationFrame]; 
    contentView.backgroundColor = [UIColor whiteColor]; 
    self.view = contentView; 

    UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainBackground.jpeg"]]; 
    [self.view addSubview:backgroundImageView]; 

    CGRect tableFrame = CGRectMake(40, 40, 240, 310); 
    self.listView = [[UITableView alloc] initWithFrame:tableFrame]; 
    self.listView.dataSource = self; 
    self.listView.delegate = self; 
    self.listView.scrollEnabled = NO; 
    self.listView.rowHeight = 50; 
    self.listView.backgroundColor = [UIColor clearColor]; 
    self.listView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBgMid"]]; 


    [self.view addSubview:self.listView]; 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"cellRow %@", indexPath.row); 
    return [super tableView:tableView cellForRowAtIndexPath:indexPath]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"Did Select Row%@", indexPath.row); 
    [super tableView:tableView didSelectRowAtIndexPath:indexPath]; 
} 

回答

1

你是否实现了-tableView:numberOfRowsInSection:?没有它,你将无法在你的表格视图中显示任何单元格。

1

片段您需要实现tableviewdelegate和tableviewsource协议。您将需要添加一个IBOutlet到UITableView,它是xie的参考插座,用于添加协议,并将数据源设置为您的UIView控制器。

对不起,我看到你不使用xib。很奇怪你没有在设置数据源和委托的行上发出警告,所以你可能已经有了你的协议。

+0

对头文件,我已经实现了协议。我并不想为这个实际使用一个xib文件。我希望能够创建一个自定义表格视图。 – denniss 2012-07-30 03:18:55

+0

你有什么东西真的出现在细胞中吗? – Pareshkumar 2012-07-30 04:13:55