Brittany
Brittany

Reputation: 1449

Obj-c - Return two custom cells at the same time

I have two different custom TableView Cells. That said, when the first cell type is returned, I want the other returned immediately after. Technically, the below code is what I want to occur:

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

        ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];

        UniversalAlertTableViewCell *cellUni = (UniversalAlertTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifierUni forIndexPath:indexPath];

        return cell;
        return cellUni;
    }

However, as we know, code stops executing after the first return cell. How can I accomplish this?

Upvotes: 1

Views: 188

Answers (3)

Kevin
Kevin

Reputation: 1

You cannot return two custom cells at the same time. It is not possible, because the delegate only runs one time per row. You can return a different cell on a different row or section.

Upvotes: 0

Nullable
Nullable

Reputation: 994

Your question is very interesting. First clarify your needs.

I guess your demand is probably like this, and once the layout is displayed, switch to another layout. Let me talk about my habit of using tables. I am used to using data to control display and animation behavior. For your needs, I can use two data sources, corresponding to two kinds of cells. After one data source is displayed, immediately switch to another data source, and finally refresh the table.

The code is probably like this, you need a data source array models, and then each cell corresponds to a data source.

@property (strong, nonatomic) NSArray *models;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    ChatModel *model = ChatModel.new;
    self.models = @[model];
    [self.tableView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UniversalAlertModel *universalModel = UniversalAlertModel.new;
        self.models = @[universalModel];
        [self.tableView reloadData];
    });
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.models.count;
}

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

    id model = self.models[indexPath.row];
    if ([model isMemberOfClass:ChatModel.class]) {
        ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];
        // Set UI display according to model
        return cell;
    }else if ([model isMemberOfClass:UniversalAlertModel.class]){
        UniversalAlertTableViewCell *cellUni = (UniversalAlertTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifierUni forIndexPath:indexPath];
        // Set UI display according to model
        return cellUni;
    }
}

Upvotes: 0

rmaddy
rmaddy

Reputation: 318955

You can only return one specific type at a time. Use the indexPath parameter to decide which one to return.

The following code is a rough idea of what you need. It's up to you to adapt the if statement based on your actual needs.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];

        // configure cell based on data from your data model for this indexPath

        return cell;
    } else {
        UniversalAlertTableViewCell *cell = (UniversalAlertTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifierUni forIndexPath:indexPath];

        // configure cell based on data from your data model for this indexPath

        return cell;
    }
}

Upvotes: 1

Related Questions