我正在做一个关于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]; } @endI 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.
更多推荐
发布评论