ios - 带有 xib 文件和自定义构造函数的自定义 UITableViewCell

标签 ios objective-c uitableview xib

关于自定义 UITableViewCell 的一个问题。

我想要一个带有 xib 文件的 UICustomTableViewCell 来设计这个单元格。

当我创建单元格时,我还会使用自定义构造函数来传递一些参数......例如:

initWithMyParamterA:(NSString*) aParameterA andParameterB:(NSString*) aParameterB;

我不想使用属性来传递此参数。

那么,我如何才能一起实现这个构造函数 loadNibName (加载 xib 文件)和默认构造函数:

initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

我该如何混合这些? :)

最佳答案

我通过类别扩展了 UIView 类,并添加了使用类名创建 XIB 的方法

+ (id) viewWithDefaultXib
{
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
return [topLevelObjects count] ? [topLevelObjects objectAtIndex:0] : nil;
}

您可以在类中定义以下方法来初始化对象。 - (id) initWithCoder:(NSCoder *)aCoder 更多信息请访问https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Protocols/NSCoding_Protocol/Reference/Reference.html

关于ios - 带有 xib 文件和自定义构造函数的自定义 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19527242/

相关文章:

android - google-cloud-messaging 的性能

iphone - UITableViewCell 附件 Checkmarx 问题,许多单元格仅通过检查一行来检查

ios - UIRefreshControl 将在除 iPhone 6+ 以外的所有手机上关闭

ios - 在界面生成器中设计的自定义 View 未显示

ios - MagicalRecord UIApplicationDidEnterBackground

ios - 如何让 uitableview 一次加载多个部分?

iphone - 从 NSMutableArray 中的对象创建 NSArray

objective-c - MBProgressHUD 在 tableView 中使用时不显示

ios - 从 collectionview 内的 Tableview 单元中导航到 View Controller

ios - SceneKit 中的阴影