我创建了一个自定义类(“customClass”),它是UIView
的子类。然后,我在其子视图中添加了tableView
,并为array
创建了datasoucre
。我将delegate
和datasource
设置为自我。然后在storyboard
中,我将UIViews
课程设为customClass
。
然后在mainVC类中,我将数组设置为某个strings
并将array
设置为customClass中的数组。当我运行应用程序时,它不会崩溃或给我任何错误,但我在tableView
以下是mainVC.m
中的设置方式:
self.myArray = [[NSArray alloc] initWithObjects:@"First", @"Second", @"Third", @"Fourth", @"Fith", @"Sixths", @"Seventh", nil];
self.myView.resultArray = self.myArray;
[self.myView.tableView reloadData];
以下是customClass中的代码:
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self setupTableView];
}
return self;
}
- (void)setupTableView
{
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
[self addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.resultArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
[cell.textLabel setText:self.resultArray [indexPath.row]];
return cell;
}
当我运行该应用时,我在tableView
中看不到任何结果,尽管我确实看到了tableView
。我做错了什么,我该如何解决?
修改
刚才意识到,cellForRow
甚至没有被召唤!我做了一个reloadData
,它仍然没有被调用。
答案 0 :(得分:1)
initWithFrame: - 建议您实现此方法。您 除了或之外,还可以实现自定义初始化方法 而不是这种方法。
initWithCoder: - 如果从中加载视图,请实现此方法 Interface Builder nib文件和您的视图需要自定义 初始化。
在init和viewDidLoad方法
之前调用initWithCoder
在MainVC
viewClass *vc = [[viewClass alloc] initWithFrame:self.view.frame];
vc.resultArray = [[NSArray alloc] initWithObjects:@"First", @"Second", @"Third", @"Fourth", @"Fith", @"Sixths", @"Seventh", nil];
[vc.tableView reloadData];
[self.view addSubview:vc];
在自定义类中......
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupTableView];
}
return self;
}
- (void)setupTableView
{
self.tableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
[self addSubview:self.tableView]; // Datasource and delegate after alloc init
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.resultArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
[cell.textLabel setText:self.resultArray [indexPath.row]];
return cell;
}
使用自定义UItableviewCell时注册单元格,否则不要使用[tableView dequeueReusableCellWithIdentifier:@“cell”forIndexPath:indexPath] ;,它会崩溃。
答案 1 :(得分:-1)
您可以将customClass的backgroundColor设置为红色,然后检查mainVC视图中是否添加了customClass。