VMCuongOnStackOverflow VMCuongOnStackOverflow - 1 month ago 6
iOS Question

Objective-C : 2 different custom cells in tableview

I have a

UITableView
that contain 2 different custom cell. One is ExerciseTime and one is RestTime. I want my RestTime cell to be inserted between every 2 ExerciseTime cell :

My TableView

Here is my code:

-Height for row

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// RestTime
if (indexPath.row % 2 == 1) {
return 40.0f;
}

// ExerciseTime
else {
return 65.0f;
}
}


-Number of row in section (i couldnt find a way to set number of row for both RestTime and ExerciseTime)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (self.preset.blocks.count * 2) - 1;
}


-Cell for row

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row % 2 == 1) {
RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RestTimeTableViewCellIdentifier forIndexPath:indexPath];
RestTime *restTime = (RestTime *)[self.restTimeArray objectAtIndex:indexPath.row];
//CustomCell
return restTimeCell;
}else{
ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ExerciseTimeTableViewCellIdentifier forIndexPath:indexPath];
ExerciseTime *exerciseTime = [self.exerciseTimeArray objectAtIndex:indexPath.row];
//CustomCell
return exerciseTimecell;
}
return nil;
}


How can I create a tableView with 2 custom cell like this?

Answer

First return count as total of two array in numberOfRowsInSection and then in cellForRowAtIndexPath try some thing like this.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // RestTime
    if (indexPath.row % 3 == 0) {
        return 40.0f;
    }
    // ExerciseTime
    else {
        return 65.0f;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (indexPath.row % 3 == 0) {
        RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RestTimeTableViewCellIdentifier  forIndexPath:indexPath];
        //Access you object from array like this
        NSInteger index = (NSInteger) (indexPath.row / 3);
        RestTime *restTime = (RestTime *)[self.restTimeArray objectAtIndex:index];
    }
    else {
        ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ExerciseTimeTableViewCellIdentifier  forIndexPath:indexPath];
        //Access you object from array like this
        NSInteger index = (indexPath.row - (NSInteger) (indexPath.row / 3));
        ExerciseTime *exerciseTime = [self.exerciseTimeArray objectAtIndex:(index - 1)];
    }
    return cell;
}

Output: I have try this by setting background color as Red color for RestTimeTableViewCell and Green color for ExerciseTimeTableViewCell.

enter image description here