2014-02-24 75 views
-1

我剛剛完成了一個簡單的5分鐘UITableView教程。我甚至將教程的源代碼文件導入到我自己當前的xcode項目中,但仍然無法使其工作。我不確定自己是否做錯了什麼,或者是因爲該教程是使用不同版本的xcode或者什麼創建的。UITableView單元格不顯示文字

無論如何,我創建了一個名爲「TVTViewController」的新的Objective-C文件,它是UIViewController的一個子類。然後,我將一個UIViewController拖放到故事板上,並將它的屬性檢查器中的自定義類設置爲「TVTViewController」。

接下來,我將一個UITableView對象拖到剛纔拖到故事板上的UIViewController上。

我將UITableView的「內容」設置設置爲「動態」,然後將其設置爲「原型單元格」設置爲「1」。

然後我選擇了故事板上的原型單元格,並將其「Style」設置更改爲「Subtitle」,並將其「Identifier」設置更改爲「SettingsCell」。

最後,這裏是我的頭文件中的代碼:

#import <UIKit/UIKit.h> 

@interface TVTViewController : UIViewController <UITableViewDataSource> 

@end 

這裏是我的主要文件的代碼:

#import "TVTViewController.h" 

@interface TVTViewController() 
@property (weak, nonatomic) IBOutlet UITableView *tableView; 
@property (strong, nonatomic) NSArray *tweetsArray; 
@end 

@implementation TVTViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    //1 
    self.tableView.dataSource = self; 
    //2 
    self.tweetsArray = [[NSArray alloc] initWithObjects: 
         @"Always put your fears behind you and your dreams in front of you.", 
         @"A relationship with no trust is like a cell phone with no service, all you can do is play games.", 
         @"People should stop talking about their problem and start thinking about the solution.", 
         @"Dear Chuck Norris, Screw you. I can grill burgers under water. Sincerely, Spongebob Squarepants.", 
         @"My arms will always be open for you, they will never close, not unless you're in them.", 
         nil]; 

} 

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

//3 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.tweetsArray count]; 
} 

//4 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //5 
    static NSString *cellIdentifier = @"SettingsCell"; 
    //6 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 


    NSString *tweet = [self.tweetsArray objectAtIndex:indexPath.row]; 
    //7 
    [cell.textLabel setText:tweet]; 
    [cell.detailTextLabel setText:@"via Codigator"]; 
    return cell; 
} 
@end 

當我在我的iPhone上運行我的應用程序,它顯示了表視圖,但它是空的。沒有任何文字顯示在表視圖的單元格中。這幾乎是從教程的源代碼中複製和粘貼的,當我在iPhone上運行教程的源代碼應用程序時,它會在單元格中顯示文本。

我不明白爲什麼它不適合我,只要我將相同的代碼添加到我自己的應用程序。

感謝您的幫助。

編輯:

我想通了我自己。本教程作者忽略了告訴您將稱爲「tableView」的IBOutlet連接到UITableView對象。我只是連接了它們,現在一切都很好。我會回答我自己的問題,但stackoverflow不會讓我再過8個小時。

回答

0

添加UITableViewDelegate,像

@interface TVTViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

self.tableView.delegate = self; 

,打造小區

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

一個可能的解決辦法:

a)檢查日Ë細胞實際上是創建或不:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if(!cell) 
{ 

    cell = [[UITableViewCell alloc] init]; 
} 

b)將其delegatedatasource

退房other ways出列表視圖細胞

相關問題