UITableView自定义单元格不显示数据

时间:2014-08-11 19:57:44

标签: xcode uitableview ios7 uiviewcontroller

这是故事。我有一个简单的应用程序,其中我使用2个选项卡,它们都使用UITableView。第一个选项卡/视图称为"Favorites",第二个选项卡称为"My Profile."此外,我还有一个名为"CustomViewCell.xib"的自定义UITable单元,其标识符名称相同。 FavoritesViewController是UITableViewController的子类,并且运行完美。但对于ProfileViewController我正在使用普通的ViewController,因为我不希望整个视图都是UitableView。为了实现这一点,我将以下ProfileViewController.h

@interface ProfileViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) IBOutlet UITableView *tableView;

然后在ProfleViewController.m文件的viewDidLoad中我有:

[self.tableView registerNib:[UINib nibWithNibName:@"CustomViewCell" bundle:nil]
     forCellReuseIdentifier:@"CustomViewCell"];

以下方法的实现方式与其他工作方式相同:

- (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 [self.myArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      // Here I am customizing the cell and returning it. 
}

当我运行应用程序时,单元格的高度正确,但是为空。 为了调试我在cellForRowAtIndexPath之前放置了一个断点,并且应用程序运行时没有错误,它不应该。所以,该程序甚至没有采用这种方法。所以,我认为这是细胞空的原因。你们有什么想法可能导致它跳过这种特殊的方法吗?你能用简单的术语解释一下吗,因为我是新手,你知道吗?

2 个答案:

答案 0 :(得分:1)

您是否实现了UITableViewDataSource? UITableViewController是它管理的Table View的委托和数据源,因此您可以在View Controller本身中定义所有这些方法。

对于作为UIViewController子视图的UITableView,您需要定义表视图的委托和数据源。

见这里:http://www.aboveground.com/tutorials/adding-a-uitableview-to-a-custom-uiview

答案 1 :(得分:1)

ViewDidLoad方法中,添加以下代码:

[self.tableView setDelegate:self];
[self.tableView setDataSource:self];

然后再次检查断点是否有效