UITableViewCell 不显示布局
TableViewCell not displaying layout
这是一个关于tableViewCell 的问题。我第一次尝试 table 视图,并试图了解可重复使用的单元格等。我已经设法让它在某种程度上发挥作用。我有一个 tableView 位于 child 视图控制器上,它是它的委托和数据源。委托代码:
#import "ChildViewController.h"
@interface ChildViewController ()
@property NSArray *titles;
@property NSArray *thumblenails;
@end
@implementation ChildViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.titles = [NSArray arrayWithObjects:@"Title 1", @"Title 2", @"Title 3", @"Title 4",nil];
self.thumblenails = [NSArray arrayWithObjects:@"Image1", @"Image2", @"Image3", @"Image4", nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.titles.count;
}
-(UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// cell.textLabel.backgroundColor = [UIColor greenColor];
cell.textLabel.text = [self.titles objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[self.thumblenails objectAtIndex:indexPath.row]];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我也有一个SimpleTableViewCell.xib:
我已将身份检查器上的 class 设置为 SimpleTableViewCell,并将 SimpleTableViewCell.h 文件导入到我的 ChildViewController.h。我还在 Table 视图单元格的属性检查器中将标识符设置为 "cell",但事情就是这样。由于我的手机没有得到我想要的外观,我尝试拼写错误但没有任何改变,所以很明显标识符没有做任何事情。当我 运行 我的应用程序时,我得到这个:
所以正在加载图像和标题数组,但没有加载我在 xib 文件中为单元格设置的实际单元格大小和标签。我试过更改单元格中的某些内容,例如属性检查器中的背景颜色。
在 xib 上它看起来像这样:
但是当我 运行 它时:
我猜这一切都是因为单元格标识符实际上没有链接,但我想我已经在行中这样做了:
SimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
我是不是漏掉了一些重要的代码,在 xib 中链接了一些东西,还是只是一个错字?非常感谢任何建议。
/////////////////////////////////////////// ////////////////////////更新1//////////////////////// ////////////////////////////////////////////////////
我已经尝试了第二个建议,结果是这样的:
那么,希望这能让我明白我做错了什么?
此外,我已经更正了原文,当我将颜色更改为绿色时,它正在使用属性检查器。
感谢您的帮助。
/////////////////////////////////////////// ////////////////////////更新2//////////////////////// ////////////////////////////////////////////////////
我删除了以下行:
cell.textLabel.text = [self.titles objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[self.thumblenails objectAtIndex:indexPath.row]];
此外,添加了代码:
[self.tableView registerNib:[UINib nibWithNibName:@"SimpleTableCell" bundle:nil] forCellReuseIdentifier:@"cell"];
和代码:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [UIScreen mainScreen].bounds.size.width * (0.3);
}
现在我的 table 看起来像这样:
所以现在的问题是如何添加标题和图像的原始数组的实际值?
您忘记为单元格标识符注册 nib。
将 [self.tableView registerNib:[UINib nibWithNibName:@"SimpleTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
放入 viewDidLoad
方法。
你可以删除
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
您应该设置单元格的高度以在 nib 文件中获得完全相同的外观。使用此委托方法设置单元格的高度。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [UIScreen mainScreen].bounds.size.width * (0.3);// 0.3 should be your aspect ratio for cell:
// for cell.height/ cell.width in nib file.
}
这是一个关于tableViewCell 的问题。我第一次尝试 table 视图,并试图了解可重复使用的单元格等。我已经设法让它在某种程度上发挥作用。我有一个 tableView 位于 child 视图控制器上,它是它的委托和数据源。委托代码:
#import "ChildViewController.h"
@interface ChildViewController ()
@property NSArray *titles;
@property NSArray *thumblenails;
@end
@implementation ChildViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.titles = [NSArray arrayWithObjects:@"Title 1", @"Title 2", @"Title 3", @"Title 4",nil];
self.thumblenails = [NSArray arrayWithObjects:@"Image1", @"Image2", @"Image3", @"Image4", nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.titles.count;
}
-(UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// cell.textLabel.backgroundColor = [UIColor greenColor];
cell.textLabel.text = [self.titles objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[self.thumblenails objectAtIndex:indexPath.row]];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我也有一个SimpleTableViewCell.xib:
我已将身份检查器上的 class 设置为 SimpleTableViewCell,并将 SimpleTableViewCell.h 文件导入到我的 ChildViewController.h。我还在 Table 视图单元格的属性检查器中将标识符设置为 "cell",但事情就是这样。由于我的手机没有得到我想要的外观,我尝试拼写错误但没有任何改变,所以很明显标识符没有做任何事情。当我 运行 我的应用程序时,我得到这个:
所以正在加载图像和标题数组,但没有加载我在 xib 文件中为单元格设置的实际单元格大小和标签。我试过更改单元格中的某些内容,例如属性检查器中的背景颜色。
在 xib 上它看起来像这样:
但是当我 运行 它时:
我猜这一切都是因为单元格标识符实际上没有链接,但我想我已经在行中这样做了:
SimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
我是不是漏掉了一些重要的代码,在 xib 中链接了一些东西,还是只是一个错字?非常感谢任何建议。
/////////////////////////////////////////// ////////////////////////更新1//////////////////////// ////////////////////////////////////////////////////
我已经尝试了第二个建议,结果是这样的:
那么,希望这能让我明白我做错了什么?
此外,我已经更正了原文,当我将颜色更改为绿色时,它正在使用属性检查器。
感谢您的帮助。
/////////////////////////////////////////// ////////////////////////更新2//////////////////////// ////////////////////////////////////////////////////
我删除了以下行:
cell.textLabel.text = [self.titles objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[self.thumblenails objectAtIndex:indexPath.row]];
此外,添加了代码:
[self.tableView registerNib:[UINib nibWithNibName:@"SimpleTableCell" bundle:nil] forCellReuseIdentifier:@"cell"];
和代码:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [UIScreen mainScreen].bounds.size.width * (0.3);
}
现在我的 table 看起来像这样:
所以现在的问题是如何添加标题和图像的原始数组的实际值?
您忘记为单元格标识符注册 nib。
将 [self.tableView registerNib:[UINib nibWithNibName:@"SimpleTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
放入 viewDidLoad
方法。
你可以删除
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
您应该设置单元格的高度以在 nib 文件中获得完全相同的外观。使用此委托方法设置单元格的高度。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [UIScreen mainScreen].bounds.size.width * (0.3);// 0.3 should be your aspect ratio for cell:
// for cell.height/ cell.width in nib file.
}