2012-01-31 48 views
0

我有一個表,我想在那裏顯示一些文本;但我不能,我不知道爲什麼。我的應用程序是基於視圖的應用程序,我不想爲表視圖控制器進行更改。下面的代碼:如何在UITableView中顯示文本

.h 

{ 
NSArray *array; 
IBOutlet UITableView *table; 
} 


.m 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    // Return the number of rows in the section. 
    return [array count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    cell.textLabel.text = [array objectAtIndex:indexPath.row]; 

    // Configure the cell 

    return cell; 
} 

請幫助我,因爲我試圖解決這個問題,我不知道什麼是錯的,爲什麼表不顯示陣!

謝謝!

+0

您是否連接了數據源和委託? – Louie 2012-01-31 20:53:25

+0

我如何連接它? – Adri 2012-01-31 20:55:18

回答

0

你幾乎肯定沒有在IB中正確連接你的數據源和委託。

你的UIViewController需要實現UITableViewDelegate和UITableViewDatasource,然後你需要在Interface Builder中將它連接到你的UITableView(表)。

詳情請參閱Apple's documentation here

+0

好的!我讀過它,它現在起作用了!謝謝你回答我愚蠢的問題! – Adri 2012-01-31 21:50:11

0

在您的.H文件,你使用這個接口<UITableViewDelegate, UITableViewDataSource>?

+0

是的,當然,我正在使用它。 – Adri 2012-01-31 21:50:35

0

答案是,你從來沒有創造過單細胞。您所做的調用dequeueReusableCellWithIndentifier僅回收您已創建的單元格。在dequeueResusable ...行後添加此代碼:

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
+0

它不適用於此,但最後的回答是正確的。 – Adri 2012-01-31 21:49:34