2

我正在尝试以手风琴单元格模式展开/折叠 tableview 单元格,这样,当用户点击行时,他们将通过扩展行高来获得单元格内所选行的详细描述。我有 2 个数组,'array' 和 'detailarray' 分别用于在 'cell.textLabel.text' 和 'cell.detailTextLabel.text' 单元格中显示它。现在最初我已将“cell.detailTextLabel.hidden = YES”设为隐藏,这样当用户点击该行时可以获得单元格的扩展文本。

我的代码,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text=[array objectAtIndex:indexPath.row];
    cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];
    cell.detailTextLabel.hidden = YES;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"indexpath is %d", indexPath.row);
    selectedIndex = indexPath.row;
    isSearching = YES;

    [self.tableview beginUpdates];

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.text=[array objectAtIndex:indexPath.row];
    cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];
    cell.detailTextLabel.hidden = NO;

    [self.tableview endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (isSearching && indexPath.row == selectedIndex)
    {       
        return 77;      
    }
    return 44;    
}

现在我创建了一个有 4 行的 tableviewcell,当点击每一行时,我会得到所选行的扩展视图。在运行时,我可以在单元格上看到“detailarray”文本,当它扩展时,它会在 didselectRowAtIndexPath 函数停止时消失,为什么会这样,任何人都可以在这方面帮助我。

4

2 回答 2

3

查看SubTable,它会为您处理展开/折叠。

如果您正在寻找在主要单元格下显示的单个详细信息单元格,则可以像这样实现委托方法:

- (NSInteger)numberOfChildCellsUnderParentIndex:(NSInteger)parentIndex {
    if (detailarray[parentIndex] contains content)
        return 1;
    else
        return 0;
}

并根据您的需要自定义单元格的高度和外观

于 2014-07-23T02:52:22.813 回答
0

选择单元格后,请记住其在 selectedIndex 属性中的索引(您必须创建一个)。

向表格视图添加功能:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.row == selectedIndex ? extendedHeight : normalHeight;
}

选择单元格后不要忘记重新加载 tableView。

于 2013-09-04T07:49:10.920 回答