-2

我用 UITableView 设置了一个 ViewController(带有 xib)。我将视图控制器设置为表视图的数据源和委托。一切似乎都到位,但我无法控制单元格数据,因为 CellForRow... 从未被调用。

我的.h

#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>


@property IBOutlet UITableView *tableView;

@end

我的.m

#import "MyViewController.h"

@interface MyViewController ()

@end

@implementation MyViewController

- (id)init{
self = [super init];
if (self) {
    // Custom initialization
}
return self;
}

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.tableView.delegate = self;
self.tableView.dataSource = self;

[self.view addSubview:self.tableView];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this   view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell...

cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.text = @"SOMETHING";

return cell;
}
4

1 回答 1

11

因为你有 0 行和 0 节。由于有 0 行和 0 节......永远不需要创建或访问单元格,因此永远不会向 cellForRowAtIndexPath 发送消息。

于 2013-06-27T20:36:51.890 回答