简体   繁体   English

iOS通过表格视图设置扩展TableCell的高度

[英]iOS Set Height of Expanding TableCell with Table View

I created a TableView with Expandable cell as shown here . 我创建了一个具有Expandable单元格的TableView,如下所示 In my case the expanded area contains a TextView. 在我的情况下,扩展区域包含TextView。 My view contains a tag (as label text) and its' meaning (in text view). 我的视图包含一个标签(作为标签文本)及其含义(在文本视图中)。 And things are working fine. 而且一切正常。

I have come up with a situation where a single tag can have multiple meanings. 我想出了一个标签可能具有多种含义的情况。 So I need to show all the meanings. 因此,我需要显示所有含义。 So I got to have that many textview's as many meanings for each tag. 所以我必须让每个标签具有尽可能多的textview含义。

I have added a TableView (with TextView in TableCell) inside the expandable part of the table cell. 我在表格单元格的可扩展部分内添加了一个TableView(在TableCell中带有TextView)。 So can add X number of rows in the expanded table cell with each meaning of the tag in single cell. 因此,可以在扩展表单元格中添加X个行,并在单个单元格中添加标记的每个含义。 I want to set the height of TextView, TableView and finally TableViewCell. 我想设置TextView,TableView的高度,最后设置TableViewCell的高度。 I tried different ways, but am not able to achieve it. 我尝试了不同的方法,但无法实现。 Can you help me achieve it the way I am looking for. 您能以我想要的方式帮助我实现它吗?

Screen shot of something that I am trying to achieve - 我试图实现的目标的屏幕截图- 在此处输入图片说明

UPDATE :- 更新:-

My cellForRowAtIndexPath method where I am trying to set properties :- 我试图设置属性的cellForRowAtIndexPath方法:-

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"Table View TAG = %d  ROW = %d", tableView.tag, indexPath.row);

    // TABLE VIEW IN THE CELL
    if (tableView.tag == 10) {  //  cell.valuesTv
        CannedValueCell *ccell = (CannedValueCell *) [tableView dequeueReusableCellWithIdentifier:@"cannedValueCell"];

        if (ccell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CannedValueCell" owner:self options:nil];
            ccell = [nib objectAtIndex:0];
        }

        // Populate valuesTv
        ccell.valueTextView.text = [valuesArray objectAtIndex:indexPath.row];

        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(valueTextViewTapped)];
        [ccell.valueTextView addGestureRecognizer:gestureRecognizer];

        //[ccell.valueTextView sizeToFit];  // Makes no difference 
        // CGRect frame = ccell.valueTextView.frame;
        // frame.size.height = ccell.valueTextView.contentSize.height;
        // ccell.valueTextView.frame = frame;

        NSLog(@"*** TEXTView HEIGHT = %f  C_CELL HEIGHT = %f   ** TV HEIGHT = %f", ccell.valueTextView.frame.size.height, ccell.frame.size.height, cell.valuesTv.frame.size.height );  
        // *** TEXTView HEIGHT = 79.000000  C_CELL HEIGHT = 80.000000   ** TV HEIGHT = 80.000000  
        //  AS SET HEIGHT IN STORY BOARD - NO DIFFERENCE
        return ccell;

    } else {

        //  MAIN TABLE VIEW
        static NSString *cellIdentifier = @"CannedCell";

        cell = (CannedCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if(cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CannedCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        // Set datasource and delegate for the cell's TableView
        cell.valuesTv.dataSource = self;
        cell.valuesTv.delegate = self;

        .......
    }
}

Table view delegate method 表格视图委托方法

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.cannedTV) {
        if (selectedIndex == indexPath.row)
            return 150;     //(30 + (80 * valuesArray.count));
        else
            return 30;
        }
        return 30;
    }
}

This is what the result is :- 这就是结果:

在此处输入图片说明

I tried various things, searched a lot on net, but couldn't find solution for the following :- 我尝试了各种方法,在网上进行了很多搜索,但找不到以下解决方案:-

  1. Set height of Text View as much as the contents of it. 设置文本视图的高度与其内容的高度一样。
  2. The inner table should be completely visible - in all cells full textview should be visible with its complete contents properly. 内部表应该是完全可见的-在所有单元格中,完整的textview及其正确内容应可见。 So each cell may have different height. 因此,每个单元格可能具有不同的高度。
  3. Thus the outer/main table expandable cell should also be accordingly set. 因此,外部/主表可扩展单元格也应相应设置。 - For this I tried (30 + (80 * valuesArray.count));, but the inner table doesn't occupy that much space. -为此,我尝试了(30 +(80 * valuesArray.count));,但是内部表并没有占用那么多空间。
  4. How to get which textview is tapped ? 如何获取哪个textview? When textview is tapped, the method valueTextViewTapped is fired, but yet am not able to find which textview is tapped on. 轻按textview时,将触发valueTextViewTapped方法,但无法找到轻按了哪个textview。 How can I pass the indexPath.row to valueTextViewTapped method, so can find which textview was tapped on ?? 如何将indexPath.row传递给valueTextViewTapped方法,以便可以找到哪个textview被点击了?

Can you please guide me set the height of the components accordingly and achieve the requirements. 您能指导我相应地设置组件的高度并达到要求吗? Any help is highly appreciated. 非常感谢您的帮助。 Thanks. 谢谢。

A table view and table view controller are very powerful tools. 表格视图和表格视图控制器是非常强大的工具。

Based on the information provided, your suggested implementation is satisfactory. 根据提供的信息,您建议的实施令人满意。

A UITableViewCell can have its height set dynamically using the table view delegate method heightForRowAtIndexPath . 可以使用表视图委托方法heightForRowAtIndexPath动态设置UITableViewCell的高度。

You can respond to a user selecting the UITableViewCell using the table view delegate method didSelectRowAtIndexPath . 您可以使用表视图委托方法didSelectRowAtIndexPath响应选择UITableViewCell的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM