【问题标题】:TableView reloadData doesn't call cellForRowAtIndexPathTableView reloadData 不调用 cellForRowAtIndexPath
【发布时间】:2012-12-30 18:04:28
【问题描述】:

我有两个视图控制器/ 1.设置RingBoardViewController 2. SetupRingBoard*添加*ViewController

第一个视图控制器是 UITableViewController。 当我们第一次启动视图时 - ViewController 有 1 个固定部分和 1 个固定行。 在那个 ViewController 中,有一个 UIBarButton 正在调用 SetupRingBoardADDViewController(模式 - 默认,我正在使用情节提要)。

第二个视图控制器是 UIView 控制器。 这个 viewController 包含一个 UITableView 和一个 UINavigationBar。 UITableView 实际上是一个很大的表单,用户可以在其中输入数据。 UINavigationBar 包含一个“添加”UIBarButton。 按下此按钮时,将调用方法“addButton”。

'addButton' 方法应该刷新 SetupRingBoardViewController 中的 UITableView。 最后,按下“addButton”按钮后,SetupRingBoardViewController 的 UITableView 中应该有 2 个部分: 1. 固定部分,其中有 1 行。 2. 一个section,里面有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。

希望你能帮帮我,我想了很久:/

【问题讨论】:

    标签: objective-c ios uitableview


    【解决方案1】:

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

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

    NSLog 在numberOfRowsInSection 方法中,并使用breakpointsstep over 进行检查。

    【讨论】:

    • 委托和数据源连接到文件的所有者。按下按钮时数组包含一个值。
    • 甚至可以尝试实现tableView.dataSource = self; tableView.delegate = 自我;在 viewDidLoad 方法中,仅用于确认
    • 好的,我尝试了断点和一切,我很绝望。
    • 这里是日志,我注意到在开始时它重复了 'numberOfSections' 方法两次: [3027:c07] numberOfSectionsInTableView: 1 [3027:c07] numberOfSectionsInTableView: 1 [3027:c07] NumberfRowsInSection0: 1 [3027:c07] 它到达第一个 [3027:c07] ("A row!") c07] ("A row!") 2012-12-30 20:38:23.037 [3027:c07] numberOfSectionsInTableView: 2 2012-12-30 20:38:36.586 [3027:c07] NumberfRowsInSection1: 1 2012-12-30 20:38:40.173 [3027:c07] NumberfRowsInSection0: 1
    • 有一些非常小的问题...您将 tableView 插座连接到正确的表吗?
    【解决方案2】:

    我认为您需要进行 2 处更改。一,将数组的创建放在 viewDidLoad 而不是 init 中(如果您在情节提要中创建表视图控制器,则不会调用 initWithStyle: 或 init - initWithCoder: 将是,因此您可以使用它而不是 viewDidLoad):

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

    其次,在您的按钮方法中,您需要返回到您来自的同一个实例,而不是创建一个新实例。您可以使用 presentingViewController 属性来做到这一点:

    - (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];
    }
    

    【讨论】:

    • 首先 - 谢谢!我试过了,但出现以下运行时错误:-[UINavigationController listOfStudyHours]: unrecognized selector sent to instance 0x7550f40 这很奇怪,因为我有一个 NSMutableArray 而不是 NSArray。
    • @NoamSolovechick,您在问题中没有提到您的控制器嵌入在导航控制器中(您说第二个控制器有一个导航栏,但这可以是一个独立的项目)。那么,两个控制器是否都嵌入在导航控制器中?如果是这样,你为什么要做一个模态而不是推动从第一个到第二个的过渡?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多