Note that there are some explanatory texts on larger screens.

plurals
  1. POExpanding UITableViewCell not working as intended
    text
    copied!<p>After browsing the internet for various different ways of making expanding/collapsible UITableViewCells, I started my approach earlier today.</p> <p>I have an array that holds the state of each row in my UITableView(COLLAPSED=0, EXPANDED=1). When the expand button is clicked, I reverse the state in the array, and call reloadSection on whichever section needs to be updates. </p> <p>In my delegate method heightForRowAtIndexPath, I check to see whether the cell should be expanded or not and return the proper height. The problem I am facing is that my cell does indeed expand, but instead of showing my content hid underneath(I made the cell much bigger with extra info underneath shortened it in IB, and enabled clip subviews to fix the cell overlap), it shows two other grouped UITableViewCells with a similar background as mine and throws the positioning of some labels/buttons off. </p> <p>I am including pictures of what it looks like normally and expanded. I have gone through the code and it all looks okay, I think there may be a setting I need to change in order for this to render properly.</p> <p><img src="https://i.stack.imgur.com/FuZoS.png" alt="Collapsed"></p> <p><img src="https://i.stack.imgur.com/BMs8T.png" alt="Expanded"></p> <p><img src="https://i.stack.imgur.com/6c4VT.png" alt="Collapsed Cell in IB"></p> <p><img src="https://i.stack.imgur.com/LMNi1.png" alt="Expanded Cell in IB"></p> <p>Here is the source:</p> <pre><code>#define UITABLEVIEWCELLSIZE_COLLAPSED 97 #define UITABLEVIEWCELLSIZE_EXPANDED 285 #define EXPANDED 1 #define COLLAPSED 0 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSNumber *state = [cellExpansionArray objectAtIndex:[indexPath section]]; if ([state intValue] == COLLAPSED) { return UITABLEVIEWCELLSIZE_COLLAPSED; }else if([state intValue] == EXPANDED) { return UITABLEVIEWCELLSIZE_EXPANDED; } return UITABLEVIEWCELLSIZE_COLLAPSED; } //expandCell is called when the arrow indicator is clicked on the cell -(IBAction)expandCell:(id)sender { NSInteger cellNumber = [sender tag]; NSNumber *state = [cellExpansionArray objectAtIndex:cellNumber]; if (state.intValue == 0) { [cellExpansionArray replaceObjectAtIndex:cellNumber withObject:[NSNumber numberWithInt:EXPANDED]]; }else if (state.intValue == 1) { [cellExpansionArray replaceObjectAtIndex:cellNumber withObject:[NSNumber numberWithInt:COLLAPSED]]; } //[self.tableView beginUpdates]; //[self.tableView endUpdates]; //[self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:cellNumber] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tableView reloadData]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"PCFCustomCell"; PCFCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //cell.autoresizingMask = UIViewAutoresizingFlexibleHeight; [cell setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"1slot.png"]]]; PCFClassModel *course = [classesOffered objectAtIndex:[indexPath section]]; cell.courseCRN.text = [course CRN]; cell.courseDataRange.text = [course dateRange]; cell.courseDaysOffered.text = [course days]; cell.courseHours.text = [course credits]; cell.courseInstructor.text = [course instructor]; cell.courseTime.text = [course time]; cell.courseLocation.text = [course classLocation]; cell.courseSection.text = [course sectionNum]; cell.courseName.text = [course courseNumber]; cell.courseTitle.text = [course classTitle]; cell.courseType.text = [course scheduleType]; //cell.professorEmail.text = [course instructorEmail]; [cell.mailProf setTag:[indexPath section]]; [cell.showCatalog setTag:[indexPath section]]; [cell.followClass setTag:[indexPath section]]; [cell.addToSchedule setTag:[indexPath section]]; [cell.buttonDropDown setTag:[indexPath section]]; [cell.mailProf setHidden:NO]; [cell.mailProf setEnabled:YES]; if (![course instructorEmail]) { [cell.mailProf setTitle:@"NO EMAIL PROVIDED" forState:UIControlStateNormal]; [cell.mailProf setEnabled:NO]; } [cell.followClass setBackgroundImage:[UIImage imageNamed:@"search_res_11.png"] forState:UIControlStateNormal]; [cell.addToSchedule setBackgroundImage:[UIImage imageNamed:@"search_res_09.png"] forState:UIControlStateNormal]; if ([savedResults count] &gt; 0) { for (PCFClassModel *courseTwo in savedResults) { if ([[courseTwo CRN] isEqualToString:[course CRN]]) { [[cell followClass] setBackgroundImage:[UIImage imageNamed:@"favorite_activated.png"] forState:UIControlStateNormal]; break; } } } //schedule if ([savedSchedule count] &gt; 0) { for (PCFClassModel *courseTwo in savedSchedule) { if ([[courseTwo CRN] isEqualToString:[course CRN]]) { [[cell addToSchedule] setBackgroundImage:[UIImage imageNamed:@"purdue_activated.png"] forState:UIControlStateNormal]; break; } } } [cell.mailProf addTarget:self action:@selector(mailProf:) forControlEvents:UIControlEventTouchUpInside]; [cell.showCatalog addTarget:self action:@selector(showCatalog:) forControlEvents:UIControlEventTouchUpInside]; [cell.followClass addTarget:self action:@selector(followClass:) forControlEvents:UIControlEventTouchUpInside]; [cell.addToSchedule addTarget:self action:@selector(addToSchedule:) forControlEvents:UIControlEventTouchUpInside]; [cell.buttonDropDown addTarget:self action:@selector(expandCell:) forControlEvents:UIControlEventTouchUpInside]; //get internet data /* [[cell staticAvailable] setHidden:YES]; [[cell available] setHidden:YES]; [cell.available setText:@""]; [[cell remainingActivityIndicator] startAnimating]; dispatch_queue_t getSpots = dispatch_queue_create("GetSpots", nil); dispatch_async(getSpots, ^{ NSString *webData = nil; while (!webData &amp;&amp; self.view.window) webData = [PCFWebModel queryServer:[course classLink] connectionType:nil referer:@"https://selfservice.mypurdue.purdue.edu/prod/bwckschd.p_get_crse_unsec" arguements:nil]; if (!self.view.window) return; NSArray *courseRecord = [PCFWebModel parseData:webData type:3]; PCFCourseRecord *record = [courseRecord objectAtIndex:0]; dispatch_async(dispatch_get_main_queue(), ^{ [[cell remainingActivityIndicator] stopAnimating]; [[cell staticAvailable] setHidden:NO]; NSString *val = @"1"; if ([val compare:record.enrolled] &gt; 0) { //[[cell available] setTextColor:[UIColor redColor]]; }else { //[[cell available] setTextColor:[UIColor colorWithRed:.0565442 green:.430819 blue:.0724145 alpha:1]]; } [[cell available] setText:[NSString stringWithFormat:@"SLOTS: %@/%@", record.enrolled,record.capacity]]; [[cell available] setHidden:NO]; [PCFAnimationModel fadeTextIntoView:cell.available time:1]; [PCFAnimationModel fadeTextIntoView:cell.staticAvailable time:1]; }); }); */ // Configure the cell... //[PCFFontFactory convertViewToFont:cell]; [cell.courseTitle setFont:[PCFFontFactory droidSansFontWithSize:13]]; [cell.courseName setFont:[PCFFontFactory droidSansFontWithSize:38]]; [cell.courseDaysOffered setFont:[PCFFontFactory droidSansBoldFontWithSize:11]]; NSNumber *state = [cellExpansionArray objectAtIndex:indexPath.section]; if (state.intValue == COLLAPSED) { [cell.imageViewBackground setHidden:YES]; }else { //[cell.imageViewBackground setHidden:NO]; } return cell; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload