使用自定义单元格时无法查看UITableViewCell';iOS中的s
我在应用程序中使用自定义UITableViewCell为每行显示一个简单的标签。但是,由于某些原因,我无法显示包含要在每个单元格中显示的文本的数组的内容。我使用Interface Builder在.xib文件中创建了自定义UITableViewCell,而使用此自定义UITableViewCell的viewController位于故事板文件中 以下是IB中我的屏幕的外观: 以下是自定义UITableViewCell类中的相关方法:使用自定义单元格时无法查看UITableViewCell';iOS中的s,ios,uitableview,Ios,Uitableview,我在应用程序中使用自定义UITableViewCell为每行显示一个简单的标签。但是,由于某些原因,我无法显示包含要在每个单元格中显示的文本的数组的内容。我使用Interface Builder在.xib文件中创建了自定义UITableViewCell,而使用此自定义UITableViewCell的viewController位于故事板文件中 以下是IB中我的屏幕的外观: 以下是自定义UITableViewCell类中的相关方法: - (id)initWithStyle:(UITableV
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
因为我已经在IB中完成了所有工作,所以在initWithStyle方法中没有包含任何代码
我使用自定义单元格的ViewController中的相关代码如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.menuArray = @[@"Apples", @"Pears", @"Bananas", @"Oranges", @"Peaches"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
MenuTableCell *cell = (MenuTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = (MenuTableCell *)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.menuLabel.text = [self.menuArray objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.menuArray count];
}
有人能看出我做错了什么吗?在
yourViewController
中添加numberOfRowsInSection
方法,如下所示
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.menuArray count];
}
在viewController中实现以下所需的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.menuArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.menuArray count];
}
我编辑了cellForRowAtIndexPath看看
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
MenuTableCell *cell = (MenuTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//Edited Code for creating the cell*****************************************
if (cell == nil)
{
NSArray *objectsArray = [[NSBundle mainBundle] loadNibNamed:@"MenuTableCell" owner:self options:nil];
for (id objectCell in objectsArray)
{
cell = (MenuTableCell*)objectCell;
}
}
cell.menuLabel.text = [self.menuArray objectAtIndex:indexPath.row];
return cell;
}
希望这对您有用如果您在xib文件中创建一个单元格,您应该在viewDidLoad中调用registerInB:forIdentifier:。如果您这样做,则无需在cellForRowAtIndexPath中检查nil单元格。我认为这将对您有效。。。。并检查标签插座连接是否显示正确答案
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return [menuArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.menuLabel.text = [self.menuArray objectAtIndex:indexPath.row];
}
为什么要创建3个单元格,1是足够的,并声明节中的行数我不会创建三个单独的单元格,这是同一个单元格,但右侧工具栏的视图不同。非常感谢您的解决方案,我的代码几乎正常工作。出于某种原因,每个表格单元格都显示文本“label”,而不是self.menuArray中的相应值。你知道为什么吗?@syedfa嘿,你可以写一个
NSLog(@“currentLabel:%@,[self.menuArray objectAtIndex:indexPath.row])代码>并查看它实际返回的内容。如果它返回您想要的,那么可能是您没有将菜单标签链接到标签,非常感谢您的回答。我快到了。出于某种原因,我的表格单元格现在显示的是“Label”,而不是数组中的正确值。你知道为什么吗?另外,如果我按照你的建议做,我将如何从viewDidLoad调用registerInB:forIdentifier,那么我在cellForRowAtIndexPath中的代码会是什么样子?我是否可以完全删除if(cell==nil)块?@syedfa,是的,您可以删除cell==nil块。它的名称如下--[self.tableView registerNib:[UINib nibWithNibName:@“SampleCell1”bundle:nil]强制重用标识符:@“SampleCell1”];