UITableView可扩展+动态单元格高度(根据UITableViewCell内的UITextView)

时间:2015-02-26 10:42:38

标签: uitableview expandablelistview

我正在开发一个项目,我必须将其作为新闻应用程序实现。我的意思是最初所有单元格都包含新闻标题,当用户点击任何标题时,内容将通过展开单元格来显示。请为此建议一些教程。我正在使用以下代码来扩展单元格。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BOOL isChild =
    currentExpandedIndex > -1
    && indexPath.row > currentExpandedIndex
    && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];

    if (isChild) {
        NSLog(@"A child was tapped, do what you will with it");
        return;
    }

    [self.tableView beginUpdates];

    if (currentExpandedIndex == indexPath.row) {
        [self collapseSubItemsAtIndex:currentExpandedIndex];
        currentExpandedIndex = -1;
    }
    else {

        BOOL shouldCollapse = currentExpandedIndex > -1;

        if (shouldCollapse) {
            [self collapseSubItemsAtIndex:currentExpandedIndex];
        }

        currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;

        [self expandItemAtIndex:currentExpandedIndex];
    }

    [self.tableView endUpdates];

}

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

试试此代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Set expanded cell then tell tableView to redraw with animation
    self.expandedRow = indexPath;
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

   if ([indexPath isEqual:self.expandedRow]) {
      return EXPANDED_HEIGHT;
   }

    return NORMAL_HEIGHT;
}