UITableView单元格不显示文本

时间:2014-02-24 18:41:15

标签: ios iphone objective-c uitableview

我刚刚完成了一个简单的5分钟UITableView教程。我甚至将教程的源代码文件导入到我自己当前的xcode项目中,但我仍然无法使用它。我不确定我做错了什么,或者是不是因为教程是使用不同版本的xcode创建的。或者是什么。

无论如何,我创建了一个名为" TVTViewController"的新Objective-c文件。这是UIViewController的子类。然后我将UIViewController拖到故事板上,并在属性检查器中将它的自定义类设置为" TVTViewController"。

接下来,我将一个UITableView对象拖到我刚刚拖到故事板上的UIViewController上。

我设置了UITableView"内容"设置为"动态"然后设置它" Prototype Cells"设置为" 1"。

然后我在故事板上选择了原型单元格,并改变了它的样式"样式"设置为"字幕",并更改它"标识符"设置为" 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上运行教程的源代码应用程序时,它会在单元格中显示文本。

一旦我将相同的代码添加到自己的应用中,我就不明白为什么它不适用于我。

感谢您的帮助。

编辑:

我只是自己想出来了。教程作者忽略了告诉你连接名为&#34; tableView&#34;的IBOutlet。到UITableView对象。我只是连接它们,现在一切都很好。我会回答我自己的问题但是stackoverflow不会让我再过8个小时。

2 个答案:

答案 0 :(得分:0)

添加UITableViewDelegate,例如

@interface TVTViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

self.tableView.delegate = self;

创建单元格

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

答案 1 :(得分:0)

可能的解决方法:

a)检查是否实际创建了单元格:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if( !cell )
{

   cell = [[UITableViewCell alloc] init];
}

b)设置其delegatedatasource

查看other ways以使表格视图单元格出列