2014-02-24 73 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出列表视图细胞

相关问题