TableView reloadData不调用cellForRowAtIndexPath

时间:2012-12-30 18:04:28

标签: objective-c ios uitableview

我有两个视图控制器/ 1. SetupRingBoardViewController 2. SetupRingBoard * ADD * ViewController

第一个视图控制器是UITableViewController。 当我们第一次启动视图时--ViewController有一个固定的部分,有一个固定的行。 在那个ViewController中,有一个调用SetupRingBoardADDViewController的UIBarButton(模态 - 默认,我使用的是storyboard)。

第二个视图控制器是UIView控制器。 这个viewController包含一个UITableView和一个UINavigationBar。 UITableView实际上是一个大的形式,用户可以在其中输入数据。 UINavigationBar包含一个'Add'UIBarButton。 按下此按钮时,正在调用方法'addButton'。

'addButton'方法应刷新SetupRingBoardViewController中的UITableView。 最后,在按下'addButton'按钮后 - 在SetupRingBoardViewController的UITableView中应该有2个部分: 1.固定部分,其中有1行。 2.一行中有X行,每行都有一个标题:@“A Row!”; (X =被按下的'addButton'的数量。)

最后,这是代码:

SetupRingBoardViewController.h:

//
//  SetupRingBoardViewController.h
// 
//
//  Created by  on 12/24/12.
//  Copyright (c) 2012 Noam. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "SetupRingBoardADDViewController.h"
//#import "StudyHour.h"

@interface SetupRingBoardViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *listOfStudyHours;

@end

SetupRingBoardViewController.m:

//
//  SetupRingBoardViewController.m
//  
//
//  Created by on 12/24/12.
//  Copyright (c) 2012 Noam. All rights reserved.
//

#import "SetupRingBoardViewController.h"
#import "SetupEmptyListViewController.h"
@interface SetupRingBoardViewController ()

@end

@implementation SetupRingBoardViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)init
{
    self = [super init];
    if(self != nil)
    {
        if(!_listOfStudyHours) _listOfStudyHours = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if(_listOfStudyHours) NSLog(@"%@",_listOfStudyHours);
    [self.tableView reloadData];
    // 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;

    //[self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    if(![_listOfStudyHours count])
    {
        NSLog(@"numberOfSectionsInTableView: 1");
        return 1;
    }
    else return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if(section == 0) return 1;
    else return [_listOfStudyHours count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(![indexPath section])
    {
        NSLog(@"It got to the first");
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // Configure the cell...

        return cell;
    }
    else
    {
        NSLog(@"It got to the second");
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        cell.textLabel.text = [_listOfStudyHours objectAtIndex:indexPath.row];
        return cell;
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

SetupRingBoardADDViewController.h:

//  SetupRingBoardADDViewController.h
//  
//
//  Created by on 12/26/12.
//  Copyright (c) 2012 Noam. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "SetupRingBoardViewController.h"
#import "UITableViewCell+Checkmark.h"
//#import "StudyHour.h"

@interface SetupRingBoardADDViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

- (IBAction)addButton:(id)sender;
@end

SetupRingBoardADDViewController.m:

- (IBAction)addButton:(id)sender {
        SetupRingBoardViewController *rbVC = [[SetupRingBoardViewController alloc] init];
        [rbVC.listOfStudyHours addObject:@"A row!"];
        NSLog(@"%@",rbVC.listOfStudyHours);
        [[rbVC tableView] reloadData];
        [self dismissViewControllerAnimated:YES completion:nil];
}

(这不是整个代码,但它是唯一相关的东西。)

问题是当我调用[tableView reloadData]时没有调用方法cellForRowAtIndexPath。

我希望你能帮助我,我想弄清楚很长一段时间:/

2 个答案:

答案 0 :(得分:5)

请检查您是否已将代理和数据源设置/连接到文件所有者。

并检查模型的数组计数,如果它包含的值不是?

NSLog采用numberOfRowsInSection方法,并使用断点跳过进行检查。

答案 1 :(得分:3)

我认为您需要进行2次更改。一,将数组的创建放在viewDidLoad而不是init中(如果在故事板中创建了表视图控制器,则initWithStyle:init也不会被调用 - initWithCoder:将是,所以你可以使用它来代替viewDidLoad): / p>

- (void)viewDidLoad
{
    [super viewDidLoad];
    if(!_listOfStudyHours) _listOfStudyHours = [[NSMutableArray alloc] init];
    if(_listOfStudyHours) NSLog(@"%@",_listOfStudyHours);
    [self.tableView reloadData];
}

其次,在你的按钮方法中,你需要回到你来自的同一个实例,而不是创建一个新实例。您可以使用presentViewController属性执行此操作:

- (IBAction)addButton:(id)sender {
    SetupRingBoardViewcontroller *rbVC = (SetupRingBoardViewcontroller *)self.presentingViewController;
    [rbVC.listOfStudyHours addObject:@"A row!"];
    NSLog(@"%@",rbVC.listOfStudyHours);
    [[rbVC tableView] reloadData];
    [self dismissViewControllerAnimated:YES completion:nil];
}
相关问题