【问题标题】:setting UILabel.text of a custom UITableViewCell inside ViewController在 ViewController 中设置自定义 UITableViewCell 的 UILabel.text
【发布时间】:2012-08-15 12:30:44
【问题描述】:

我有一个UITableView,我在界面生成器中使用自定义UITableViewCell 进行填充。我在访问这些自定义 tableviewcells 属性时遇到了一些问题,希望能得到一些帮助。

在 Interface Builder 中,我将自定义 tableviewcell 的 class 设置为当前 View 控制器(因此我可以将所有标签对象分配给 Interface Builder 中的正确标签),因此我还设置了 IBOutlet 标签到 Interface Builder 中的正确标签 但是,当我尝试将 NSString 从数组对象变量(类型为 NSString)传递到 UIlabel 的文本时,会发生此错误。

Property 'cellDescription' not found on object of type 'UITableViewCell *'

下面是我用自定义 tableviewcell 设置我的 tableview 的代码,然后尝试用正确的文本填充单元格 UILabels..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (indexPath.section == 0) {      
        // Set cell height
        [self tableView:tableView heightForRowAtIndexPath:indexPath];

        // Configure the cell using custom cell
        [[NSBundle mainBundle] loadNibNamed:@"AutomotiveSearchCell" owner:self options:nil];

        cell = autoSearchCell;

        //call dataArrayOfObject that has all of the values you have to apply to the custom tableviewcell
        SearchResultItem* myObj = (SearchResultItem*)[dataArrayOfObjects objectAtIndex:indexPath.row];

        cell.cellDescription.text = myObj.seriesDescription; // This is where I am receiving the error

        NSLog(@"%@", myObj.seriesDescription); // This logs the correct value

        //Disclosure Indicator
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    return cell;
}

【问题讨论】:

    标签: iphone ios uitableview


    【解决方案1】:

    您必须将UITableViewCell 键入到AutomotiveSearchCell

    我认为你在某处的代码很奇怪(autoSearchCell 没有声明),但你必须执行以下操作。

    cell = (AutomotiveSerachCell* )autoSearchCell;
    

    上面的代码不行,应该下面的代码。


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    

    转换成

    AutomotiveSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    

    如果上述方法不起作用,请参考以下流程。

    1. 创建一个 CustomCell 类。

    2. 制作一个 CustomCell xib。

    3. 将标签链接到 CustomCell 类。

    4. 导入标头#import "AutomotiveSearchCell.h" 和下面的代码复制粘贴。


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        AutomotiveSearchCell *cell = nil;
    
        if(!cell)
        {
            UINib *nib = [UINib nibWithNibName:@"AutomotiveSearchCell" bundle:nil];
            NSArray *arr = [nib instantiateWithOwner:nil options:nil];
            cell = [arr objectAtIndex:0];
            cell.cellDescription.text = @"Test~!~!~!";
        }
    
        // Configure the cell...
    
        return cell;
    }
    

    【讨论】:

    • hrmm.. 它在右括号上出现expected expression.. 我需要在 if 语句之外调用它吗?
    • 我编辑了帖子。请检查。如果我看到你的完整代码,我会给出明确的答案。
    • hrmm,当我尝试使用cell /Users/imac/Documents/Iphone applications/ii//MSeriesViewController 时,似乎将它从 UITableViewCell 更改为 AutomotiveSearchCell 会给我这个错误。 m:154:27:使用未声明的标识符“单元格”
    • 您是否导入了“AutomotiveSearchCell.h”?我的代码没有问题。请重新检查。 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 转换为 AutomotiveSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    • 啊,AutomotiveSearchCell 只是 .xib 我已将 currentViewController 作为 xibs 类传递,因此我在当前类中声明了所有 UIabel,但我的 AutomotiveSearchCell.xib 可以从 currentViewController 中看到声明的 UILabel
    猜你喜欢
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多