2012-07-30 95 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