How to make a UITableViewCell with different subviews reusable?

后端 未结 4 1748
误落风尘
误落风尘 2020-12-25 10:07

I have a UITableView in which I display, naturally, UITableViewCells which are all of the same class, let\'s call it MyCell. So I have

4条回答
  •  余生分开走
    2020-12-25 11:03

    I would add both custom subviews to the nib and connect them to an outlet. And depending on the content I would hide one of them when you configure the content of your cell.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"CellIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell = /* load from nib */
        }
        if (/*indexPath conditionForTrainCell*/) {
           cell.trainSubview.hidden = NO;
            cell.carSubview.hidden = YES;
            // configure train cell
        }
        else {
           cell.trainSubview.hidden = YES;
            cell.carSubview.hidden = NO;
            // configure car cell
        }
        return cell;
    }
    

提交回复
热议问题