UITableViewController不会调用cellForRowAtIndexPath但是numberOfSectionsInTableView和numberOfRowsInSection确实设置正确 [英] UITableViewController will not call cellForRowAtIndexPath BUT numberOfSectionsInTableView and numberOfRowsInSection are indeed set properly

查看:141
本文介绍了UITableViewController不会调用cellForRowAtIndexPath但是numberOfSectionsInTableView和numberOfRowsInSection确实设置正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#import "AssignmentsViewController.h"
#import "Assignment.h"

@interface AssignmentsViewController ()

@property NSMutableArray *assignments;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation AssignmentsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    NSLog(@"viewDidLoad");
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.assignments = [[MySingleton sharedMySingleton] assignments];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"viewWillAppear");
    [self.tableView reloadData];
}

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"numberOfSectionsInTableView");
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"numberOfRowsInSection");
    return [self.assignments count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cellForRowAtIndexPath");
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AssignmentCell"];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AssignmentCell"];
    }
    cell.textLabel.text = [[self.assignments objectAtIndex:indexPath.row] title];
    return cell;
}

@end

当AssignmentsViewController是加载并且至少有一个家庭作业在作业数组中:

Console output when the AssignmentsViewController is loaded and at least one homework assignment is in the assignments array:

2013-11-06 18:55:09.396 Homework Planner[4756:70b] viewDidLoad
2013-11-06 18:55:09.403 Homework Planner[4756:70b] numberOfSectionsInTableView
2013-11-06 18:55:09.405 Homework Planner[4756:70b] numberOfRowsInSection
2013-11-06 18:55:09.408 Homework Planner[4756:70b] viewWillAppear
2013-11-06 18:55:09.409 Homework Planner[4756:70b] numberOfSectionsInTableView
2013-11-06 18:55:09.409 Homework Planner[4756:70b] numberOfRowsInSection

我正在申请显示用户的家庭作业。由于某种原因,即使我确定赋值数组中有一个家庭作业分配对象,也从不调用cellForRowAtIndexPath。通过模态视图将作业分配添加到数组中,并且我已经验证它们确实是由该过程添加的,并且在用户点击取消之后视图返回到由该类定义的视图。当发生这种情况时,另外两个与UITableView相关的方法运行,但不是cellForRowAtIndexPath。请帮我解决这个问题。

I am making an application that displays the users homework assignments. For some reason, cellForRowAtIndexPath is never called, even when I am sure the assignments array has a homework assignment object in it. The homework assignments are added to the array by a modal view, and I have verified that they are indeed added by that process and after the user taps "cancel" the view returns to the view defined by this class. When that happens the other two methods related to the UITableView run, but not cellForRowAtIndexPath. Please help me solve this problem I am having.

推荐答案

检查 [self.assignments count] 是否正在返回0(检查 self.assignments nil

Check to see if [self.assignments count] is returning 0 (check if self.assignments is nil.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"[self.assignments count] = %d", [self.assignments count]);
}

否则,我认为正确的另一个原因现在,当委托方法返回的高度为0时(如果你正在实现它,否则默认实现总是给出非零高度,所以在这种情况下没有问题。)

Otherwise, the only other reason I can think of right now, is when height returned by delegate method is 0 (in case you are implementing it, otherwise default implementation always give non-zero height so no issue in that case.)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  CGFloat height = 0;
  // Some calculation to determine height of row/cell.

  return height;
}

这篇关于UITableViewController不会调用cellForRowAtIndexPath但是numberOfSectionsInTableView和numberOfRowsInSection确实设置正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆