cellForRowAtIndexPath方法未调用

时间:2017-04-10 05:59:37

标签: ios objective-c uitableview uiview uiviewcontroller

在UIView类上添加UIViewController现在有UITableView类的UIView类被添加到另一个UIViewController类

//MyControllerView
//added filterSideView side to it and adding JaffaViewController.view 
-(void)loadControllerJaffa
{
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main"
                                                         bundle:nil];
    JaffaViewController *jaffaViewController =
    [storyboard instantiateViewControllerWithIdentifier:@"JaffaViewController"];
    jaffaViewController.view.frame = filterSideView.bounds;
    [filterSideView addSubview:jaffaViewController.view];
    [jaffaViewController didMoveToParentViewController:self];
}


#import <UIKit/UIKit.h>

@interface JaffaViewController : UIViewController  <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *jaffaTableView;

@end

实施类

@implementation JaffaViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _colorNameList = [[ColorModelClass colorListNames]allKeys];
    _jaffaTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _jaffaTableView.dataSource = self;
    _jaffaTableView.delegate = self;
    [_jaffaTableView setBackgroundColor:[UIColor redColor]];


        // Do any additional setup after loading the view.
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    _jaffaTableView.dataSource = self;
    _jaffaTableView.delegate = self;
}

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


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;    //count of section
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleTableCell"];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:@"SimpleTableCell"];

    }
    cell.textLabel.text = @"Welcome";
    return cell;


}

问题是cellForRowAtIndexPath方法根本没有调用。 我已经设置了数据源和委托方法。

它调用numberOfRowsInSectionnumberOfSectionsInTableView两种方法,但不调用cellForRowAtIndexPath方法。 目前我正在使用Xcode 8.3&amp; iOS 10.3版本。 您的意见将帮助我解决问题。

3 个答案:

答案 0 :(得分:0)

请尝试此代码

-(void)loadControllerView {
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main"
                                                     bundle:nil];
    JaffaViewController *jaffaViewController =
    [storyboard instantiateViewControllerWithIdentifier:@"JaffaViewController"];

    [jaffaViewController willMoveToParentViewController:self];
    jaffaViewController.view.frame = filterSideView.bounds;
    [filterSideView addSubview:jaffaViewController.view];
    [jaffaViewController didMoveToParentViewController:self];
}

备注:

确保您已确认数据源并为其委派。

答案 1 :(得分:0)

为什么要设置两次数据源?删除viewDidAppear中的代码并在viewDidLoad的末尾添加[_jaffaTableView reloadData];

答案 2 :(得分:0)

tingViewController *destVC = [self.storyboard instantiateViewControllerWithIdentifier:@"RatingViewController_id"];


destVC.completionHandler = ^(NSString* string)
{
    NSLog(@"Returned strng.....%@",string);
};

destVC.productId = self.productId;

[destVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self addChildViewController:destVC];
[self.view addSubview:destVC.view];
[destVC didMoveToParentViewController:self];

[UIView animateWithDuration:0.3 animations:^{
    [destVC.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}];