UITableView可扩展+动态单元格高度(根据UITableViewCell内部的UITextView) [英] UITableView Expandable + Dynamic cell height (According to the UITextView inside UITableViewCell)

查看:83
本文介绍了UITableView可扩展+动态单元格高度(根据UITableViewCell内部的UITextView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中执行与新闻应用程序相同的项目.我的意思是说,最初所有单元格都将包含新闻标题",并且当用户单击任何标题时,内容将通过扩展单元格来显示.请为此建议一些教程.我正在使用以下代码扩展单元格.

I am working on a project where I have to implement same as news application. I mean initially all cells will contain News Title and when user clicks on any title then the content will display by expanding the cell. Please suggest some tutorials for this. I am using following code for expansion of cell.

- (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];

}

推荐答案

尝试此代码

- (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;
}

这篇关于UITableView可扩展+动态单元格高度(根据UITableViewCell内部的UITextView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆