Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从nib加载UITableViewCell的特殊方式_Ios_Objective C_Uitableview_Cocoa Touch_Uikit - Fatal编程技术网

Ios 从nib加载UITableViewCell的特殊方式

Ios 从nib加载UITableViewCell的特殊方式,ios,objective-c,uitableview,cocoa-touch,uikit,Ios,Objective C,Uitableview,Cocoa Touch,Uikit,这是我发明的一种加载自定义单元格的方法 1) 我使用UITableViewCell类扩展 //.h @interface UITableViewCell (Extended) + (id) cellWithClass:(Class)class; + (id) cellWithClass:(Class)class fromNibNamed:(NSString *)nibName; @end //.m + (id) cellWithClass:(Class)class { ret

这是我发明的一种加载自定义单元格的方法

1) 我使用UITableViewCell类扩展

//.h

@interface UITableViewCell (Extended)

+ (id) cellWithClass:(Class)class;

+ (id) cellWithClass:(Class)class fromNibNamed:(NSString *)nibName;

@end

//.m

+ (id) cellWithClass:(Class)class
{
    return [UITableViewCell cellWithClass:class fromNibNamed:NSStringFromClass(class)];
}

+ (id) cellWithClass:(Class)class fromNibNamed:(NSString *)nibName {

    NSArray * nibContents = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:NULL];

    NSEnumerator * nibEnumerator = [nibContents objectEnumerator];
    NSObject * nibItem = nil;

    while ((nibItem = [nibEnumerator nextObject]) != nil) {

        if ([nibItem isKindOfClass:class]) {
            return nibItem;
        }

    }

    return nil;
}
2) 创建自定义UITableViewCell子类,使用一个名称相同的.nib(CustomCell.xib)连接所有插座

@interface CustomCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel * labelSmth;

- (void) setupWithTitle:(NSString *)title;

@end
2) 在CustomCell.xib中,我使用Interface builder拖动了一个UITableViewCell,并使其成为CustomCell类(具有重用标识符CustomCell)(我没有设置文件所有者)。。。而不是UI样式,连接插座等

3) 而不是像这样装

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"CustomCell";

    CustomCell * cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {

        cell = [UITableViewCell cellWithClass:[CustomCell class]];

    }

    [CustomCell setupWithTitle:[self.titles objectAtIndex:[indexPath row]]];

    return cell;
}
*这个方法行吗?这在许多项目中都有效,但我不确定ReuseIdentinterfier以及单元是否得到了正确的重用…*

我对此也不确定

NSArray * nibContents = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:NULL];
当我在类方法中传递所有者self时

苹果公司也提出了

- (void) registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)reuse;
这怎么可能适合我的方法

以及如何使用自定义重用标识符,比如如果我想要一个方法

+ (id) cellWithClass:(Class)class fromNibNamed:(NSString *)nibName reuseIdentifier:(NSString *)reuseIdentifier;

你不需要为此发明新东西。它已经为你发明了。你发明了一种加载自定义单元格的通用反模式

枚举nib内容以获取nib中的UITableViewCell不是正确的方法

您应该在创建UITableViewCell(通常是UIViewController)的nib的文件所有者中定义并导出UITableViewCell

然后,您可以使用以下模式访问该单元格:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
    static NSString *cellIdentifier = @"MyCustomCell"; //this should also be specified in the properties of the UITableViewCell in the nib file
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(!cell) {
        [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
        cell = self.myCustomCellOutlet;
        self.myCustomCellOutlet = nil;
    }   

    return cell;
}

新的-(void)registerNib:(UINib*)nib forCellReuseIdentifier:(NSString*)重用对您的示例有何影响?你什么意思@苹果破裂