自定义表格视图单元格中没有可见视图(No Visible View in Custom Table View Cell)

编程入门 行业动态 更新时间:2024-10-05 01:17:42
自定义表格视图单元格中没有可见视图(No Visible View in Custom Table View Cell)

我正在做一个关于tableView和tableViewCell的练习。 我在自定义视图单元格中遇到了一些问题。

我用.xib文件创建了自己的自定义tableViewCell ,名为newCellView.xib 。 我需要.h和.m文件,我选择超类作为UITableViewCell 。

在我的视图控制器称为TableViewController我正在创建一个这样的默认单元格的表。

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; cell.textLabel.text = [showNames objectAtIndex:[indexPath row]]; cell.imageView.image = [UIImage imageNamed:[iconArray objectAtIndex:[indexPath row]]]; return cell; }

一旦我创建了我自己的自定义视图。 我将newViewCell.h导入到viewController并且像这样更新了代码。

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { static NSString *newCellViewString = @"newCellView"; newCellView *cell = [tableView dequeueReusableCellWithIdentifier:newCellViewString]; //newCellView *cell = [[newCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newCellView]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"newCellView" owner:self options:nil]; cell = (newCellView *)[nib objectAtIndex:0]; }

但是当我运行应用程序时,我看到一个空白页面,就像我没有相关的视图。 也许我忘了一些连接添加。 我什至不能看到空单元格查看。 只有一个白色的空白页面。 任何帮助都会很棒。 谢谢。

编辑其他文件

这是我的.h和.m文件。

#import <UIKit/UIKit.h> @interface newCellView : UITableViewCell <UITableViewDelegate> @property (nonatomic, strong) IBOutlet UILabel *nameLabel; @property (nonatomic, strong) IBOutlet UIImageView *iconImageView; @property (retain, nonatomic) IBOutlet UIButton *extendButton; @end

.m文件

#import "newCellView.h" @implementation newCellView @synthesize nameLabel; @synthesize extendButton; @synthesize iconImageView; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (void)dealloc { [extendButton release]; [nameLabel release]; [iconImageView release]; [super dealloc]; } @end

I am doing an exercise about tableView and tableViewCell. And I am having some problems with custom view cells.

I have created my own custom tableViewCell with .xib file called as newCellView.xib. I have required .h and .m files and I choose super class as UITableViewCell.

In my view controller called as TableViewController I was creating a table with default cells like this.

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; cell.textLabel.text = [showNames objectAtIndex:[indexPath row]]; cell.imageView.image = [UIImage imageNamed:[iconArray objectAtIndex:[indexPath row]]]; return cell; }

Once I have created my own custom view. I imported newViewCell.h into my viewController and I updated the code like this.

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { static NSString *newCellViewString = @"newCellView"; newCellView *cell = [tableView dequeueReusableCellWithIdentifier:newCellViewString]; //newCellView *cell = [[newCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newCellView]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"newCellView" owner:self options:nil]; cell = (newCellView *)[nib objectAtIndex:0]; }

But when I run the app I see a blank page, like I have no related view. Maybe I forgot some connections to add. I can't even see empty cells to view. Only a white blank page. Any help would be great. Thanks.

EDIT FOR OTHER FILES

Here are my .h and .m files.

#import <UIKit/UIKit.h> @interface newCellView : UITableViewCell <UITableViewDelegate> @property (nonatomic, strong) IBOutlet UILabel *nameLabel; @property (nonatomic, strong) IBOutlet UIImageView *iconImageView; @property (retain, nonatomic) IBOutlet UIButton *extendButton; @end

.m file

#import "newCellView.h" @implementation newCellView @synthesize nameLabel; @synthesize extendButton; @synthesize iconImageView; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (void)dealloc { [extendButton release]; [nameLabel release]; [iconImageView release]; [super dealloc]; } @end

最满意答案

在处理自定义单元格时,我总是尝试将与单元格相关的所有内容封装在UITableViewCell子类(包括NIB / outlets / ..)中,并使用tableView registerClass: forCellReuseIdentifier:方法告诉我的表格使用哪个类它的细胞。

在你的例子中,你可以:

在newCellView.m中,在单元格init中添加nib加载:

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"newCellView" owner:self options:nil]; self = [nib objectAtIndex:0]; } return self; }

确保所有Outlet连接正确。 (即你的Nib中的你的UITableViewCell是NewCellView类的..)

然后在你的控制器的viewDidLoad中,你告诉你的表使用newCellView作为它的单元格:

[yourTableView registerClass:[newCellView class] forCellReuseIdentifier:@"newCellView"];

最后在cellForRowAtIndexpath中:

newCellView *cell = [tableView dequeueReusableCellWithIdentifier:@"newCellView"]; if (cell == nil) { cell = [[NewCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newCellView]"; }

I found the answer. Everything was working good in my code except the positioning of the cell view's.

Project was using autolayout as a default property. So whenever i see the white page actually cells were out of bounds. I disabled autolayout and set movement properties of the items from size inspector.

Thanks for efforts.

更多推荐

本文发布于:2023-07-09 10:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085606.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   自定义   单元格   表格   Visible

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!