简体   繁体   中英

setting UILabel.text of a custom UITableViewCell inside ViewController

I have a UITableView that I am populating with custom UITableViewCell in interface builder. I am having some issues accessing these custom tableviewcells properties and am hoping for some help.

In Interface Builder I am setting the custom tableviewcell's class to the current View controller (so I can assign all of the label objects to the correct labels in Interface Builder), So I have also set up the IBOutlet labels to the correct labels in Interface Builder However this error occurs when I try to pass the NSString from the array object variable (which is of type NSString) to the UIlabel's text.

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

Below is the code I have used to set up my tableview with the custom tableviewcell and then try to populate the cells UILabels with the correct text..

- (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;
}

you must typeCasting UITableViewCell To AutomotiveSearchCell

I think you code somewhere is strange(There is no declaration for autoSearchCell), but you must do the following.

cell = (AutomotiveSerachCell* )autoSearchCell;

The above code does not work, should following code.


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

convert to

AutomotiveSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

If that does not work above, refer a following process.

  1. make a CustomCell Class. 在此输入图像描述

  2. make a CustomCell xib. 在此输入图像描述在此输入图像描述在此输入图像描述

  3. linked label to CustomCell Class. 在此输入图像描述

  4. import header #import "AutomotiveSearchCell.h" and following code copy and paste.


- (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;
}

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM