objective-c - TableView reloadData 不调用 cellForRowAtIndexPath

标签 objective-c ios uitableview

我有两个 View Controller / 1.设置RingBoardViewController 2.设置RingBoard*ADD*ViewController

第一个 View Controller 是 UITableViewController。 当我们第一次启动 View 时 - ViewController 有 1 个固定部分和 1 个固定行。 在那个 ViewController 中,有一个 UIBarButton 正在调用 SetupRingBoardADDViewController(模态 - 默认情况下,我正在使用 Storyboard)。

第二个 View Controller 是 UIView Controller 。 这个 viewController 包含一个 UITableView 和一个 UINavigationBar。 UITableView 实际上是一个大窗体,用户可以在其中输入数据。 UINavigationBar 包含一个“添加”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 没有被调用。

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

最佳答案

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

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

numberOfRowsInSection 方法中使用 NSLog 并使用断点跳过 检查它。

关于objective-c - TableView reloadData 不调用 cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14093169/

相关文章:

ios - 关闭 UITabBarVC 并返回登录屏幕

ios - NSAutoReleasePool 释放 View Controller ?

ios - 自定义 uitableviewcell 中的交互式流行手势识别器阻止按钮 touchupinside 事件

iOS 8 UITableViewCell 调整大小问题

ios - 错误 : implementing a method which will also be implemented by its primary class

ios - 如何使用 Objective-C 将自定义类访问到主类中?

ios - 如何在呈现 Controller 的顶部显示 UIAlertController

ios - 如何同时滚动多个tableview

ios - 正确的 : returning valid data of nil?

ios - 如何快速将 JSON 字符串转换为 JSON 对象?