如何显示此UITableView?

时间:2016-03-31 06:11:01

标签: ios objective-c uitableview

我无法弄清楚如何显示UITableView

除了nib之外,代码以编程方式完全创建表。

nib是一个简单的单元格,可以在同一个应用程序的其他UITableView中使用。

创建表格视图并将其作为子视图添加到view并调用reloadData时,系统会调用numberOfSectionsInTableView:tableView:numberOfRowsInSection:。他们都返回1。 (我检查了调试器。)

但是永远不会调用tableView:cellForRowAtIndexPath:

我将tableView添加到它的父视图中,其中NSLayoutConstraint强制它的高度,但该点没有显示任何内容。

视图层次结构调试器在该点中没有显示任何内容,并且表格顶部没有视图。

该应用运行时没有任何错误或崩溃。

感谢您的帮助。

@interface MyViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *button;
@end

@implementation MyViewCell
@end

@interface MyDataTableEntry : NSObject
@property (nonatomic, strong) NSString* title;
@end

@implementation MyDataTableEntry
@end

@interface MyDataTable : NSObject <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSArray *rows;
@property (strong, nonatomic) UINib* nib;
@end

@implementation MyDataTable

- (UINib*)getAndRegisterNib:(NSString*)reuseIdentifier {
    UINib* nib = [UINib nibWithNibName:reuseIdentifier bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:reuseIdentifier];
    return nib;
}

- (id)initWithRows:(NSArray*) rows andView:(UIView*)view {
    if (self = [super init]) {
        self.rows = rows;
        self.tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain];
        self.nib = [self getAndRegisterNib:@"PrototypeCell"];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor grayColor];
    }

    return self;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.rows.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PrototypeCell" forIndexPath:indexPath];

    MyDataTableEntry* entry = self.rows[indexPath.row];

    [cell.button setTitle:entry.name forState:UIControlStateNormal];
    cell.button.backgroundColor = [UIColor blueColor];

    return cell;
}

@end

以下是创建表格的代码段:

MyDataTableEntry* entry = [[MyDataTableEntry alloc] init];
entry.title = @"hello";
self.dataTable = [[MyDataTable alloc] initWithRows:@[entry] andView:self.view];
[self.view addSubview:self.dataTable.tableView];
[self.dataTable.tableView setNeedsLayout];
[self.dataTable.tableView layoutIfNeeded];
[self.dataTable.tableView reloadData];

1 个答案:

答案 0 :(得分:3)

未在调试器中确认:

问题在于:

self.dataTable = [[MyDataTable alloc] initWithRows:@[entry] andView:self.view];
[self.view addSubview:self.dataTable];

self.dataTable是一个NSObject。第二行应该是这样的:

[self.view addSubview:self.dataTable.tableView];

我还建议您进行一些重构。 MyDataTable不是一种观点。它是一个控制器。所以重命名为MyDataTableController。然后这个bug会更加明显。

我也会问为什么你在代码中这样做?你已经在使用笔尖了。那么为什么不去故事板。这样做并使用UITableViewController作为基类应该可以删除大量代码和潜在的错误。