简体   繁体   English

UITableViewController不会调用cellForRowAtIndexPath但是numberOfSectionsInTableView和numberOfRowsInSection确实设置得当

[英]UITableViewController will not call cellForRowAtIndexPath BUT numberOfSectionsInTableView and numberOfRowsInSection are indeed set properly

#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

Console output when the AssignmentsViewController is loaded and at least one homework assignment is in the assignments array: 加载AssignmentsViewController并且至少有一个家庭作业在assignments数组中时,控制台输出:

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

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. 由于某种原因,即使我确定赋值数组中有一个家庭作业分配对象,也从不调用cellForRowAtIndexPath。 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. 当发生这种情况时,另外两个与UITableView相关的方法运行,但不是cellForRowAtIndexPath。 Please help me solve this problem I am having. 请帮我解决这个问题。

Let me contribute one more culprit: "Memory management issue". 让我再提出一个罪魁祸首:“内存管理问题”。

If TableView's DataSource object is not kept (retain count is zero) while TableView is pulling data from it, then, suprisingly, tableView:cellForRowAtIndexPath: is not called although numberOfSections and numberOfRows do just get called 如果TableView的DataSource对象没有保留(保留计数为零)而TableView正在从中提取数据,那么,令人惊讶的是,尽管numberOfSections和numberOfRows刚刚被调用,但不会调用tableView:cellForRowAtIndexPath:

Check to see if [self.assignments count] is returning 0 (check if self.assignments is nil . 检查[self.assignments count]是否返回0(检查self.assignments是否nil

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

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

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

  return height;
}

If cellForRowAtIndexPath is not call it's because the view is not yet rendered on the screen (not drawed), so the system is not able to know the size of the Cell or the UITableView. 如果cellForRowAtIndexPath没有调用它,因为视图尚未在屏幕上呈现(未绘制),因此系统无法知道Cell或UITableView的大小。

To solve that issue make sure that you add your items AFTER the view if rendered (and dimension are known). 要解决该问题,请确保在视图之后添加项目(如果已呈现)(并且已知尺寸)。 It can happen if the UITableView is added in a parent view to build complex screen. 如果在父视图中添加UITableView以构建复杂屏幕,则会发生这种情况。

If you don't load the data during the init phase, you can call reloadData after setting the data. 如果在初始化阶段未加载数据,则可以在设置数据后调用reloadData。

Example: 例:

OfferTableViewController * offerList = [[OfferTableViewController alloc] init];

// add the view in the parent view to make sure that the dimension are calculated
[fullSiteView.offersView addSubview: offerList.view]; 
offerList.delegate = self;
// Set the data
offerList.offers = product.offers;
// Force reload to render
[offerList.tableView reloadData];

In addition, if you overwrite heightForRowAtIndexPath make sure that it's return something > 0 此外,如果覆盖heightForRowAtIndexPath,请确保它返回> 0的内容

Note that even if numberOfRowsInSection and numberOfSectionsInTableView return the right count of items, if the size of the table is unknown, the system will no call cellForRowAtIndexPath 请注意,即使numberOfRowsInSection和numberOfSectionsInTableView返回正确的项目数,如果表的大小未知,系统也不会调用cellForRowAtIndexPath

Another problem might be the frame of the tableview itself either having a size of 0 or being offscreen... 另一个问题可能是tableview本身的框架大小为0或屏幕外...

I was stuck for ages watching logs of numberOfRowsInSection: being called and heightForRowAtIndexPath: being called and returning a hardcoded value but cellForRowAtIndexPath: was never called! 我被困了多年,看着numberOfRowsInSection的日志被调用和heightForRowAtIndexPath:被调用并返回一个硬编码值,但是从未调用过cellForRowAtIndexPath:!

Try logging out the frame of the tableView, or setting a background colour on it to check its actually viewable on-screen. 尝试注销tableView的框架,或在其上设置背景颜色以检查其实际可在屏幕上查看。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UITableViewController不会调用cellForRowAtIndexPath但是numberOfSectionsInTableView和numberOfRowsInSection设置正确 - UITableViewController will not call cellForRowAtIndexPath BUT numberOfSectionsInTableView and numberOfRowsInSection are set properly UITableView不调用numberOfRowsInSection也不调用cellForRowAtIndexPath - UITableView doesn't call numberOfRowsInSection nor cellForRowAtIndexPath numberOfSectionsInTableView之后,UITableViewController中崩溃 - Crash in UITableViewController after numberOfSectionsInTableView Swift:动态填充numberOfSectionsInTableView和numberOfRowsInSection - Swift: Populating numberOfSectionsInTableView and numberOfRowsInSection dynamically iOS Swift numberOfSectionsInTableView VS numberOfRowsInSection - iOS Swift numberOfSectionsInTableView VS numberOfRowsInSection 未调用 cellForRowAtIndexPath 但调用了 numberOfRowsInSection - cellForRowAtIndexPath not called but numberOfRowsInSection called tableView中的cellForRowAtIndexPath和numberOfRowsInSection冲突 - cellForRowAtIndexPath and numberOfRowsInSection conflicting in tableView numberOfRowsInSection和cellForRowAtIndexPath的2个问题 - 2 problems with numberOfRowsInSection and cellForRowAtIndexPath 在初始加载时,三次调用UITableView numberOfSectionsInTableView和numberOfRowsInSection - UITableView numberOfSectionsInTableView and numberOfRowsInSection called thrice on initial load cellForRowAtIndexPath永远不会被调用,但是numberOfRowsInSection是 - cellForRowAtIndexPath never gets called, but numberOfRowsInSection is
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM