iOS:单元格未向TableView注册

时间:2013-06-17 20:32:57

标签: ios

我正在关注此tutorial并坚持使用“入门”部分。在本文中,单元格正在以编程方式进行注册。我按照所有步骤,下载了代码并对其进行了比较,但无法解决问题 的 SHCAppDelegate.m

@implementation SHCAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[SHCViewController alloc] initWithNibName:@"SHCViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

SHCViewController.m

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

        _toDoItems = [[NSMutableArray alloc] init];
        [_toDoItems addObject:[SHCToDoItem toDoItemWithText:@"Feed the cat"]];
        [_toDoItems addObject:[SHCToDoItem toDoItemWithText:@"Buy eggs"]];
        [_toDoItems addObject:[SHCToDoItem toDoItemWithText:@"Pack bags for WWDC"]];

      }
}
- (void)viewDidLoad:(BOOL)animated
{
    [super viewDidLoad];
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _toDoItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *ident = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
    int index = [indexPath row];
    SHCToDoItem *item = _toDoItems[index];
    NSLog(@"%@",item.text);
    cell.textLabel.text = item.text;

    return cell;
}

此处tableView:(UITableView *)tableView cellForRowAtIndexPath:内的NSLog未在控制台上打印。

1 个答案:

答案 0 :(得分:0)

设置数据源后,将重载表方法调用为 [self.tableview reloadData];

试试这个

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableview reloadData];
}