ios 如何正确初始化自定义 UITableviewCell?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20725151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly Init a custom UITableviewCell?
提问by Michael Campsall
I am using the following 2 methods to return a custom cell:
我使用以下 2 种方法返回自定义单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [self keyForIndexPath:indexPath];
UITableViewCell *cell;
if ([key isEqualToString:DoneButtonCellKey]) {
cell = [self [self doneButtonCellForIndexPath:indexPath];
return cell;
} else {
//code to return default cell...
}
}
Then:
然后:
- (DoneButtonCell *)doneButtonCellForIndexPath: (NSIndexPath *)indexPath {
DoneButtonCell *cell = [self.tableView dequeueReusableCellWithIdentifier:DoneButtonCellIdentifier forIndexPath:indexPath];
return cell;
}
What is the correct init method to use with the cell here so I can change some properties of the cell when it is initialized?
此处与单元格一起使用的正确 init 方法是什么,以便在初始化时更改单元格的某些属性?
EDIT: I found the problem, as the init/awakeFromNib methods were not being called for me. I tracked down the error and it was that I had not changed the "Custom Class" from UITableViewCell to my custom class. Now awakeFromNib AND initWithCoder work as described below.
编辑:我发现了问题,因为没有为我调用 init/awakeFromNib 方法。我找到了错误,原因是我没有将“自定义类”从 UITableViewCell 更改为我的自定义类。现在awakeFromNib AND initWithCoder 的工作如下所述。
回答by Mostafa Berg
You can do your changes inside the DoneButtonCell
's class, either in the
您可以在DoneButtonCell
's 类中进行更改,或者在
- (void)awakeFromNib
{
.. essential to call super ..
super.awakeFromNib()
//Changes done directly here, we have an object
}
Or the initWithCoder:
method:
或者initWithCoder:
方法:
-(id)initWithCoder:(NSCoder*)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
//Changes here after init'ing self
}
return self;
}
回答by TALE
If you're using Swift, remember the easy way to ensure a view is initialized when it is created is to use the didSet method. For example, to make a UIImageView into a round shape you could add code like this:
如果您使用 Swift,请记住确保视图在创建时初始化的简单方法是使用 didSet 方法。例如,要将 UIImageView 变成圆形,您可以添加如下代码:
@IBOutlet weak var profileImageView: UIImageView! {
didSet {
// Make the profile icon circle.
profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2
profileImageView.clipsToBounds = true
}
}
回答by Duncan Groenewald
This is how I am initialising custom cells
这就是我初始化自定义单元格的方式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"FileTableViewCell";
FileTableViewCell *cell = (FileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FileTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// Configure the cell here...
// Configure the cell.
FileRepresentation* fileRepresentation = _fileList[indexPath.row];
cell.textLabel.text = [self userFilename:[fileRepresentation.fileName stringByDeletingPathExtension]];
cell.detailTextLabel.text = [fileRepresentation modifiedDate];
cell.accessoryView=nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.progressIndicator setHidden:YES];
cell.imageView.image = [UIImage imageNamed:_fileImageName];
// Disable any user interaction while processing a request
if (_fileIsOpen || _creatingDocument || _deletingDocument) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.textColor = [UIColor grayColor];
} else {
cell.textLabel.textColor = [UIColor blackColor];
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
}
}
回答by Ayan Sengupta
- First try to dequeue a cell if possible using
dequeueReusableCellWithIdentifier
method of UITableView. - If cell is not available (
nil
) use[[NSBundle mainBundle] loadNibNamed:@"<#your custom cell nib name#>" owner:nil options:nil][0]
to initialize it. - In your custom cell's .m file, implement
initWithCoder:
initializer for custom initialization code:
- 如果可能,首先尝试使用
dequeueReusableCellWithIdentifier
UITableView 的方法使单元格出列。 - 如果单元格不可用 (
nil
) 用于[[NSBundle mainBundle] loadNibNamed:@"<#your custom cell nib name#>" owner:nil options:nil][0]
初始化它。 - 在自定义单元格的 .m 文件中,
initWithCoder:
为自定义初始化代码实现初始化程序:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
//your custom initialization code
return self;
}
This is the designated initializer that is called when any view is loaded from a nib with loadNibNamed
, like a custom table view cell.
这是指定的初始化程序,在从带有 的笔尖加载任何视图时调用loadNibNamed
,例如自定义表格视图单元格。