简体   繁体   English

tableview:cellForRowAtIndexPath从未调用

[英]tableview:cellForRowAtIndexPath never called

I'm very confusing with this. 我对此很困惑。

I'm trying to create an UITableView grouped programmatically, using the single view template from Xcode. 我正在尝试使用Xcode中的单个视图模板以编程方式创建一个UITableView分组。 Looking at internet for some examples, there is no secret, the approach to do this is very simple. 看看互联网上的一些例子,没有秘密,做到这一点的方法很简单。 I'm not sure if my implementation are wrong or right, because the method tableView:cellForRowAtIndexPath: is never called, but the other ones are called and return a integer > 0. 我不确定我的实现是对还是错,因为从不调用tableView:cellForRowAtIndexPath:方法,但是调用了其他方法并返回一个大于0的整数。

There is code, any suggestions are welcome. 有代码,欢迎任何建议。 Thanks, 谢谢,

#import "ViewController.h"
#import "SimpleCell.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) NSArray *sections;
@property (nonatomic, strong) NSArray *section1;
@property (nonatomic, strong) NSArray *section2;
@property (nonatomic, strong) NSArray *section3;

@end

@implementation ViewController

static NSString * const CellIdentfier = @"Cell";


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.view addSubview:self.table];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[table]|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:@{@"table": self.table}]];

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.table reloadData];
    });

}

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

- (UITableView *)table
{
    if(!_table)
    {
        _table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _table.delegate = self;
        _table.dataSource = self;
        _table.translatesAutoresizingMaskIntoConstraints = NO;
        _table.rowHeight = 34.0f;
        _table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _table.backgroundColor = [UIColor grayColor];
        _table.showsVerticalScrollIndicator = NO;
        [_table registerClass:[SimpleCell class] forCellReuseIdentifier:CellIdentfier];
    }

    return _table;
}

- (NSArray *)sections
{
    if(!_sections)
    {
        _sections = @[@"Section1", @"Section2", @"Section3"];
    }

    return _sections;
}

- (NSArray *)section1
{
    if(!_section1)
    {
        _section1 = @[@"Player a", @"Player b", @"Player c"];
    }

    return _section1;
}

- (NSArray *)section2
{
    if(!_section2)
    {
        _section2 = @[@"Zone a", @"Zone b", @"Zone c"];
    }

    return _section2;
}

- (NSArray *)section3
{
    if(!_section3)
    {
        _section3 = @[@"Area a", @"Area b", @"Area c"];
    }

    return _section3;
}


#pragma mark - UI Table View Delegate impl


#pragma mark - UI Table View Datasource impl

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DLog();
    SimpleCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentfier];

    if(!cell)
        cell = [[SimpleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentfier];

    cell.label.text = [NSString stringWithFormat:@"Section: %i Row: %i", indexPath.section, indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger total = 0;
    if(section == 0) total = self.section1.count;
    if(section == 1) total = self.section2.count;
    if(section == 2) total = self.section3.count;

    DLog(@"Rows: %i", total);

    return total;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    DLog();
    return self.sections.count;
}



@end

Delete this: 删除此:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[table]|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:@{@"table": self.table}]];

It should be called then. 然后应该调用它。

EDIT: 编辑:

Change this as well: 也更改此:

  _table = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];

This seems to fix it but I'm not sure why. 这似乎可以解决它,但我不确定为什么。 I'm guessing that it has something to do with the table frame having to be larger than 0. 我猜想这与表框架必须大于0有关。

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

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