Jeff
Jeff

Reputation: 303

How to set property of different custom UITableViewCell

First of all, this is how I dequeue my cells:

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

    switch (indexPath.row) {
        case 0:
        {
            UINib *nib = [UINib nibWithNibName:@"TableViewCellLabel" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:@"CellLabel"];

            cell = (TableViewCellLabel *)[tableView dequeueReusableCellWithIdentifier:@"CellLabel"];
        }
            break;

        case 1:
        {
            UINib *nib = [UINib nibWithNibName:@"TableViewCellTextfield" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:@"CellTextfield"];

            cell = (TableViewCellTextfield *)[tableView dequeueReusableCellWithIdentifier:@"CellTextfield"];
        }
            break;

        default:
            break;
    }

And then, I would set their property in willDisplayCell delegate method:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // set transparent cell bg
    [cell setBackgroundColor:[UIColor clearColor]];

    // init cell view
    switch (indexPath.row) {
        case 0:
        {
            ((TableViewCellLabel *)cell).tableViewCellType = TableViewCellTypeLabel;
            ((TableViewCellLabel *)cell).delegate = self;
        }
            break;

        case 1:
        {
            ((TableViewCellTextfield *)cell).tableViewCellType = TableViewCellTypeTextfield;
            ((TableViewCellTextfield *)cell).delegate = self;
        }
            break;

        default:
            break;
    }
}

My expected result was two different tableViewCellType property value, but it ended up two cells holding the same type TableViewCellTypeLabel. I can't figure out where have I done wrong so I need some help. Thank you.

Upvotes: 0

Views: 901

Answers (1)

Sergey Fedorov
Sergey Fedorov

Reputation: 219

At first register nibs in tableView at viewDidLoad() of controller.
Then, if you separate your table view for two sections, in first you want to display cells of CellLabel type, and in second section cells of CellTextfield type, you should switch case not row of IndexPath in cellForRowAtIndexPath method, but indexPath.section (you must return 2 sections count in dataSource method) and not to register anything in there. If you need just 2 rows - don't change your switch indexPath.row code.
Next you should setup your cell (set delegate and tableViewCellType) in cellForRowAtIndexPath method right after dequeue. This will allow you not to lead type implicitly second time.
And at the end of cellForRowAtIndexPath method you return you cell instance configured.
Example in swift (in obj-c same, structure is important here):

override func viewDidLoad() {
    super.viewDidLoad()

    var nib = UINib.init(nibName: "TableViewCellLabel", bundle: nil)
    self.tableView.register(nib, forCellReuseIdentifier: "CellLabel")
    nib = UINib.init(nibName: "TableViewCellTextfield", bundle: nil)
    self.tableView.register(nib, forCellReuseIdentifier: "CellTextfield")
}

CellForRow:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = UITableViewCell()

    switch indexPath.section {
    case 0:
        guard let cellLabel = tableView.dequeueReusableCell(withIdentifier: "CellLabel") as? TableViewCellLabel else {
            return cell
        }

        cellLabel.tableViewCellType = TableViewCellTypeLabel
        cellLabel.delegate = self

        return cellLabel
    case 1:
        guard let cellField = tableView.dequeueReusableCell(withIdentifier: "CellTextfield") as? TableViewCellTextfield else {
            return cell
        }
        cellField.tableViewCellType = TableViewCellTypeTextfield
        cellField.delegate = self

        return cellField
    default:
        return cell
    }
}

And clear for me, please: you return 1 section in numberOfSections and 2 in numberOfRowsInSection? If you have static number on cells in project use static cells: initialize them separately, keep lazy properties, return when you exactly want it to be returned. And this case you will not use dequeueReusableCell.

Upvotes: 1

Related Questions