【问题标题】:cellForRowAtIndexPath method is not invokingcellForRowAtIndexPath 方法未调用
【发布时间】:2017-04-10 05:59:37
【问题描述】:

在 UIView 类上添加 UIViewControllerUITableView 类现在 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 和 iOS 10.3 版本。 您的善意意见将帮助我解决问题。

【问题讨论】:

  • 如果 numberOfRows 或 Sections 为零。它不会撤销。检查 numberOfRows
  • 确保委托方法设置正确。这是最重要的。我知道你已经做了所有这些事情。但我要求你确定一下。
  • @SivajeeBattina 我仔细检查了数据源和委托方法。
  • @Kiran 你设置断点并测试了吗?
  • 这个问题似乎是由于调用了一个编写 tableview 方法的类。您是否尝试在 ViewDidAppear 方法中设置委托和数据源。请尝试一下,如果您成功了,请告诉我。

标签: ios objective-c uitableview uiview uiviewcontroller


【解决方案1】:

请试试这个代码

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

注意事项:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:
      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)];
      }];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-04
        • 2011-12-04
        • 1970-01-01
        • 1970-01-01
        • 2017-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多