Ios UITableView数据未显示
我创建了一个自定义类(“customClass”),它是Ios UITableView数据未显示,ios,objective-c,uitableview,uiview,Ios,Objective C,Uitableview,Uiview,我创建了一个自定义类(“customClass”),它是UIView的子类。然后我在它的子视图中添加了一个tableView,并为datasoucre创建了一个数组。我将委托和数据源设置为self。然后在情节提要中,我将UIViews类设置为customClass 然后在mainVC类中,我将数组设置为一些字符串,并将该数组设置为customClass中的数组。当我运行应用程序时,它不会崩溃或给我任何错误,但我在tableView 下面是如何在mainVC.m中设置的: self.myArray
UIView
的子类。然后我在它的子视图中添加了一个tableView
,并为datasoucre
创建了一个数组。我将委托
和数据源
设置为self。然后在情节提要
中,我将UIViews
类设置为customClass
然后在mainVC类中,我将数组设置为一些字符串
,并将该数组
设置为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
甚至没有被调用!我重新加载了数据,但它仍然没有被调用。您可以将customClass的backgroundColor设置为红色,然后检查customClass是否添加到mainVC的视图中
initWithFrame:-建议您实现此方法。你
除此之外,还可以实现自定义初始化方法,或
而不是使用这种方法
initWithCoder:-如果从
Interface Builder nib文件和视图需要自定义
初始化
initWithCoder的调用比init和viewDidLoad方法早得多
在缅因州
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];,它会崩溃。谢谢你的回复。我设定了颜色,我看到了。唯一的问题是数据源。(我认为这应该是一个注释。:)在分配tableview对象后,必须设置数据源和委托。(在这里,似乎没有为tableview对象检测到数据源)调用所有内容。你能在每个点记录正确的值吗?多亏了@Jaleel Nazir,我才让它工作起来。但是谢谢anyway@Jessica:检查数组计数。您的权利,它刚刚崩溃。你是什么意思。我是否必须创建一个全新的文件,或者我可以只做initWithStyle吗?当您不使用自定义单元格时,请注释registerclass行并使用此。。。UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“cell”];在使用自定义单元格时,在registerclass中使用自定义单元格类我没有使用自定义单元格,所以我使用了if(cell==nil){cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@“cell”;}
,它崩溃时出现以下错误:“无法将具有标识符单元格的单元格出列。必须为id注册nib或类…请参阅我的答案中的setupTableView。。。。当您不使用自定义单元格时,请注释registerclass行并使用此。。。UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“cell”];在使用自定义单元格时,在registerclass中使用您的自定义单元格类–您得到了它。。。只要研究和实验什么时候使用dequeueReusableCellWithIdentifier和dequeueReusableCellWithIdentifier:forIndexPath:,什么是register类,什么是initWithFrame:,什么是initWithCoder:,这非常有帮助。何时使用?如何使用?祝你一切顺利。