单个UiTableView中有多个UiTableViewCell子类?为什么这段代码不起作用

时间:2009-01-29 17:18:21

标签: cocoa-touch

好的,这是我的问题。我有一个表,有点像Apple的addressBook.app,它使用不同的单元子类以不同的方式显示信息。在这个特定的应用程序中,我有3个不同的UiTableViewCell子类,都有自己的笔尖。

我无法正确获取代码:-(如果我尝试在CellForRowPath方法中访问我的单元格的实例变量,它会说'(错误请求成员'类型',而不是结构或联合')现在我假设“某事”这个词是我问题的根源。我即将粘贴的代码显然没有正确设置我的单元格。这是代码:

根据被调用的部分,有3个if语句来获取正确的单元子类:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *PoolFacilityCellIdentifier = @"PoolFacilityCellIdentifier";
static NSString *PoolFacilityAddressCellIdentifier = @"PoolFacilityAddressCellIdentifier";
static NSString *PoolFacilityPoolCellIdentifier = @"PoolFacilityPoolCellIdentifier";
NSUInteger section = indexPath.section;

//Creat a generic container for cell which will be cast during one of the next two statements
id cell;

//Change the identifier depending on cell type. MIGHT NEED TO ADD SOMETHING ELSE FOR POOLS AND PRICES
if (section == KAddressIndex) {
    cell = (PoolFacilityAddressCell *)[tableView dequeueReusableCellWithIdentifier:PoolFacilityAddressCellIdentifier];

    if (cell == nil) {

        //Load the appropriate nib for the type of cell being asked for.

            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PoolFacilityAddressCell" owner:self options:nil];

            for (id object in nib) {

                if ([object isMemberOfClass:[PoolFacilityAddressCell class]]) 
                    cell = (PoolFacilityAddressCell *) object;

            }
    }

}
else if (section == KPoolIndex) {
    cell = (PoolFacilityPoolCell *)[tableView dequeueReusableCellWithIdentifier:PoolFacilityPoolCellIdentifier];

    if (cell == nil) {

        //Load the appropriate nib for the type of cell being asked for.

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PoolFacilityPoolCell" owner:self options:nil];

        for (id object in nib) {

            if ([object isMemberOfClass:[PoolFacilityPoolCell class]]) 
                cell = (PoolFacilityPoolCell *) object;


        }
    }
}
else {
    cell = (PoolFacilityCell *)[tableView dequeueReusableCellWithIdentifier:PoolFacilityCellIdentifier];

    if (cell == nil) {

        //Load the appropriate nib for the type of cell being asked for.

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PoolFacilityCell" owner:self options:nil];

        for (id object in nib) {

            if ([object isMemberOfClass:[PoolFacilityCell class]]) 
                cell = (PoolFacilityCell *) object;
        }
    }

}

任何帮助将不胜感激!!!我导入了我的单元子类的所有头文件。

抛出我的错误的代码就是这个(特别是cell.type.text = thePool.type)cell.type是一个Iboutlet UiLabel:

if (section == KPoolIndex) {

    NSMutableArray *thePools = [allSections objectForKey:sectionAsNumber];

    if ([thePools count] > 0) {
        [cell setPromptMode:NO];
        //Set the label and Pool from the Pool Object
        Pool *thePool = [thePools objectAtIndex:row];
        cell.type.text = thePool.type;


    }

}

谢谢,

4 个答案:

答案 0 :(得分:2)

Matt Gallagher有一篇文章提出了一种更简洁的方法来完成这种类型的表格。 他将特定于单元格的代码分解为自己的类。使管理更容易。

cocoawithlove.com - Heterogeneous cells in a UITableViewController

在您的代码中,此行看起来很麻烦

cell.type.text = thePool.type;

thePool是一个“Pool”对象,它有一个名为“type”的属性吗? 检查@property和@synthesize是否在“Pool”对象中为“type”正确设置。

答案 1 :(得分:1)

我认为这可能是因为您的单元格被声明为id并且您正试图访问其中一个单元格子类的属性。您可以将所需的所有消息发送到id类型的对象,但尝试访问类型为id的对象的属性将在编译时失败。另外,请务必导入包含子类的.h文件。

答案 2 :(得分:1)

我可能会对自己的问题有一些答案:

1 - 正如drew提到的那样,单元格仍然是ID类型。我认为如果函数会输入类型将单元格转换为特定的单元格类型,但是情况似乎并非如此。

2 - 解决方案是为每种类型的单元格提供3个大的if语句。每个if语句以

结尾

返回细胞;

呼叫。

3 - 我现在要尝试的解决方案是只有一个单元子类而不是3个,并在我的单元子类中有一个名为

的方法

- (无效)setCellBehaviour:(INT)definedNumber

这样我就可以隐藏图层/标签等,并按照我的意愿自定义它。

<强>更新

我已经取得了如下所需的结果:

我只有一个单元子类,它有一个带有许多不同UI标签的NIB。

我在uiviewcontroller的cellForRowAtIndexPath:方法中调用它,一旦单元格已经是alloc / init。

//Set the behaviour
    [cell setCellBehaviour:KCellBehaviourName];

这引用了我在视图控制器的头文件中设置的定义:

//Definitions for cell type behaviour. Passed to cell during creation.

(HASH)定义KCellBehaviourStandard 0 (HASH)定义KCellBehaviourName 1 (HASH)定义KCellBehaviourAddress 2 (HASH)定义KCellBehaviourPool 3

(这篇文章中似乎没有哈希)

然后我的UITableViewCellSubclass有了我们之前调用的这个方法:

-(void)setCellBehaviour:(NSUInteger)definedBehaviour {

switch (definedBehaviour) {
    case KCellBehaviourStandard:
        self.label.hidden = NO;
        self.value.hidden = NO;
        self.length.hidden = YES;
        self.poolType.hidden = YES;
        break;
    case KCellBehaviourName:
        self.label.hidden = NO;
        self.value.hidden = NO;
        self.length.hidden = YES;
        self.poolType.hidden = NO;
        break;
    case KCellBehaviourAddress:
        self.label.hidden = NO;
        self.value.hidden = NO;
        self.length.hidden = YES;
        self.poolType.hidden = YES;
        break;
    case KCellBehaviourPool:
        self.label.hidden = NO;
        self.value.hidden = NO;
        self.length.hidden = NO;
        self.poolType.hidden = YES;
    default:
        break;
}

}

答案 3 :(得分:0)

哦,天哪,这比所有答案都要简单得多。我不打算讨论你的整体设计,只关注你的代码无法正常工作的原因。

这是触发错误的行:

cell.type.text = thePool.type;

你可以用以下这些代替它来修复它:

PoolFacilityCell* poolCell = (PoolFacilityCell *)cell;
poolCell.type.text = thePool.type;

只有在编译器知道您正在使用哪个类时,访问属性的点语法才有效。它无法处理cell.type因为cell被声明为类型id,所以在编译时它不知道type应该是Objective-C属性。错误消息将其称为“某种结构或联合”,因为它认为您正在将cell视为普通的C结构,而不是错误。

顺便提一下,你写的行:

cell = (PoolFacilityAddressCell *) object;

毫无意义。由于cell被声明为id(任何通用对象),因此不需要强制转换。基于之前的评论,您似乎认为像这样的类型转换会以某种方式影响cell变量。它不是。上面的行汇编成指令,只是将实例的内存地址从object复制到cell。类型转换是为了避免在编译时出现警告和错误。

相关问题