2

在网上浏览了各种制作可扩展/可折叠 UITableViewCells 的方法后,我今天早些时候开始了我的方法。

我有一个数组来保存 UITableView 中每一行的状态(COLLAPSED=0,EXPANDED=1)。单击展开按钮时,我反转数组中的状态,并在需要更新的任何部分上调用 reloadSection。

在我的委托方法 heightForRowAtIndexPath 中,我检查单元格是否应该展开并返回正确的高度。我面临的问题是我的单元格确实扩展了,但没有显示隐藏在下面的内容(我使单元格更大,下面的额外信息在 IB 中缩短了它,并启用了剪辑子视图来修复单元格重叠),它显示另外两个分组的 UITableViewCells 与我的背景相似,并抛出了一些标签/按钮的定位。

我包括它正常的样子和扩展的图片。我已经浏览了代码,看起来一切正常,我认为可能需要更改一个设置才能正确呈现。

折叠

展开

IB 中的折叠单元格

IB中的扩展单元

这是来源:

#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;
}
4

3 回答 3

0

这对我来说似乎是一个自动调整大小的问题!您的单元格的子视图具有灵活的边距和宽度、高度。我可以给出一些建议,而不是确切的解决方案。希望他们帮助..

  1. 尝试将自动调整大小属性删除到子视图(在 XIB 中单击视图->转到大小检查器->自动调整大小)或
  2. 创建 2 个不同的单元格模板以在状态展开和折叠期间使用。具有 2 个不同单元格视图的相同单元格 XIB。使用 UINib 加载 xib。
  3. 如果图像是固定大小的,则不要使用图案图像作为单元格背景。使用 stretchableImageWithLeftCapWidth:topCapHeight图像 API。

干杯!
阿马尔。

于 2013-05-03T13:09:26.033 回答
-1

你还没有发布你的cellForRowAtIndexPath,但我猜你在那里错误地重用了单元格。检查类似问题的答案:

Objective C UITableView - 更改单元格高度后表格单元格显示错误内容


我尝试使用类似于您的代码,它似乎按预期工作。

- (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;
    }
}
于 2013-04-26T02:31:15.410 回答
-1

不确定这是否会对您的情况有所帮助。但通常,如果您想在更改高度后为表格中的重新加载单元格设置动画而不进行重新加载等,您可以执行以下操作。

[tableview beginUpdates];
[tableview endUpdates];
于 2013-04-26T02:31:28.903 回答