Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.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/25.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 使用相同xib的多个UITableViewCell子类?_Ios_Objective C_Uitableview_Xib - Fatal编程技术网

Ios 使用相同xib的多个UITableViewCell子类?

Ios 使用相同xib的多个UITableViewCell子类?,ios,objective-c,uitableview,xib,Ios,Objective C,Uitableview,Xib,如何在多个子类中共享一个xib 它们都有相同的视图界面,所以我不想为了更改单元格类而重复多次相同的xib - (void)viewDidLoad { [super viewDidLoad]; [self.tableView registerNib:[UINib nibWithNibName:@"BaseCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"BaseCell"]; } - (UITableVie

如何在多个子类中共享一个
xib

它们都有相同的视图界面,所以我不想为了更改单元格类而重复多次相同的
xib

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView registerNib:[UINib nibWithNibName:@"BaseCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"BaseCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BaseCell *baseCell = [tableView dequeueReusableCellWithIdentifier:@"BaseCell" forIndexPath:indexPath];

    ...

这里我需要的是我的
ChildCell
的一个实例,它是
BaseCell
的一个子类。有什么想法吗?

使用组合而不是继承。保持您的
BaseCell
类与其在nib中的单元格之间的1-1关系,为您创建的实现对象添加一个属性,并在将其出列后添加到单元格对象。

现在无法对多个单元格使用相同的
xib

但是你可以分离出xib的一部分,所有细胞都将使用它

在每个单元中,您将为这个类子视图设置一个子视图,现在您可以对所有单元重用这个视图

您可以看到此示例的“我的图像”:

第一单元:

第二单元:

和子视图两个单元格的使用:


现在您不需要重复的xib,可以重用UI的这一部分。

更改类的结构可能是更好的方法。为单元格添加状态并根据单元格的当前状态进行响应是另一种选择。