繁体   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

加载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

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

让我再提出一个罪魁祸首:“内存管理问题”。

如果TableView的DataSource对象没有保留(保留计数为零)而TableView正在从中提取数据,那么,令人惊讶的是,尽管numberOfSections和numberOfRows刚刚被调用,但不会调用tableView:cellForRowAtIndexPath:

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

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

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

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

  return height;
}

如果cellForRowAtIndexPath没有调用它,因为视图尚未在屏幕上呈现(未绘制),因此系统无法知道Cell或UITableView的大小。

要解决该问题,请确保在视图之后添加项目(如果已呈现)(并且已知尺寸)。 如果在父视图中添加UITableView以构建复杂屏幕,则会发生这种情况。

如果在初始化阶段未加载数据,则可以在设置数据后调用reloadData。

例:

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

此外,如果覆盖heightForRowAtIndexPath,请确保它返回> 0的内容

请注意,即使numberOfRowsInSection和numberOfSectionsInTableView返回正确的项目数,如果表的大小未知,系统也不会调用cellForRowAtIndexPath

另一个问题可能是tableview本身的框架大小为0或屏幕外...

我被困了多年,看着numberOfRowsInSection的日志被调用和heightForRowAtIndexPath:被调用并返回一个硬编码值,但是从未调用过cellForRowAtIndexPath:!

尝试注销tableView的框架,或在其上设置背景颜色以检查其实际可在屏幕上查看。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM