在浏览互联网以寻找各种不同的扩展/可折叠UITableViewCells的方法之后,我今天早些时候开始了我的方法。
我有一个数组,用于保存UITableView中每行的状态(COLLAPSED = 0,EXPANDED = 1)。单击展开按钮时,我会反转数组中的状态,并在需要更新的任何部分上调用reloadSection。
在我的委托方法heightForRowAtIndexPath中,我检查单元格是否应该展开并返回正确的高度。我面临的问题是我的单元确实扩展了,但不是显示我的内容隐藏在下面(我使单元格更大,下面的额外信息缩短了它,并启用剪辑子视图来修复单元重叠),它显示另外两个分组的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;
}
答案 0 :(得分:0)
这对我来说似乎是一个自动调整问题!您的单元格的子视图具有灵活的边距和&宽度,高度。我可以提供一些建议而不是确切的解决方案。希望他们帮忙..
stretchableImageWithLeftCapWidth:topCapHeight
图片API。干杯!
阿玛尔。
答案 1 :(得分:-1)
你还没有发布你的cellForRowAtIndexPath
,但我猜你错误地重用了那里的单元格。检查类似问题的答案:
Objective C UITableView - Table cells display wrong content after changing cell's height
我尝试使用与您类似的代码,它似乎按预期工作。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
states = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++)
{
[states addObject:[NSNumber numberWithBool:false]];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
cell.textLabel.text = [NSString stringWithFormat:@"Title @%d", indexPath.section];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Subtitle @%d", indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSNumber *state = [states objectAtIndex:indexPath.section];
[states replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:!state.boolValue]];
[self.tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *state = [states objectAtIndex:indexPath.section];
if (state.boolValue == false)
{
return 100;
}
else
{
return 244;
}
}
答案 2 :(得分:-1)
不确定这是否有助于您的情况。但是,如果您想在更改高度后重新加载表格中的单元格而不进行重新加载等操作,则通常可以执行以下操作。
[tableview beginUpdates];
[tableview endUpdates];