在网上浏览了各种制作可扩展/可折叠 UITableViewCells 的方法后,我今天早些时候开始了我的方法。
我有一个数组来保存 UITableView 中每一行的状态(COLLAPSED=0,EXPANDED=1)。单击展开按钮时,我反转数组中的状态,并在需要更新的任何部分上调用 reloadSection。
在我的委托方法 heightForRowAtIndexPath 中,我检查单元格是否应该展开并返回正确的高度。我面临的问题是我的单元格确实扩展了,但没有显示隐藏在下面的内容(我使单元格更大,下面的额外信息在 IB 中缩短了它,并启用了剪辑子视图来修复单元格重叠),它显示另外两个分组的 UITableViewCells 与我的背景相似,并抛出了一些标签/按钮的定位。
我包括它正常的样子和扩展的图片。我已经浏览了代码,看起来一切正常,我认为可能需要更改一个设置才能正确呈现。
这是来源:
#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] > 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] > 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 && 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] > 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;
}