尝试从自定义单元格上的 UIButton 展开/折叠 UITableViewCell [英] Trying to expand/collapse UITableViewCell from a UIButton on the custom cell

查看:27
本文介绍了尝试从自定义单元格上的 UIButton 展开/折叠 UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用自定义 UITableViewCells 填充的 UITableView.在这些自定义单元格中,我有一个 UITextField 和一个查看更多"UIButton.UIButton 的目的是在用户希望阅读更多文本时动态扩展该特定 UITableCell.同理,当用户希望恢复到原来的大小时,用户再次点击Button,UITableViewCell就会缩小到原来的大小.

由于没有选择单元格,我在自定义单元格中设置了一个 IBAction,如下所示:

//CustomCell.m内

- (IBAction)showMoreText:(id)sender{//实例布尔变量来标记单元格是否已调整大小self.hasBeenResized = YES;//关闭掩码到边界,否则单元格似乎不会调整大小[[self.cellView 层] setMasksToBounds:NO];//计算 textView 和按钮的新大小和位置CGRect newTextViewFrame = self.textView.frame;newTextViewFrame.size.height = self.textView.contentSize.height;self.textView.frame = newTextViewFrame;CGFloat bottomYPos = self.textView.frame.origin.y + self.textView.frame.size.height;CGRect buttonFrame = self.showMoreButton.frame;buttonFrame.origin.y = bottomYPos;self.showMoreButton.frame = buttonFrame;//调用开始和结束更新[(UITableView*) self.superview beginUpdates];[(UITableView*) self.superview endUpdates];//设置蒙版并在单元格上放置圆角[[self.cellView 层] setMasksToBounds:YES];[[self.cellView 层] setCornerRadius:10.0];}

接下来,我在我的 ViewController 类中有这个:

//在 ViewController.m 中- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{NSLog(@"heightForRowAtIndexPath");CustomCell *cell = (CustomCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];if([cell hasBeenResized] == NO){返回 cell.frame.size.height + 20;}别的{返回 cell.frame.size.height + cell.textView.frame.origin.y + cell.textView.frame.size.height + cell.showMoreButton.frame.size.height + 20;}}

现在发生的是我可以看到自定义单元格更改其文本视图的大小,但是,该表不会更新该特定单元格的行高.检查那里的 If-else 语句,尽管我在 CustomCell 的 IBACtion 中将其设置为 YES,但似乎 hasBeenResized 始终为 false.

我在这里查看了其他解决方案,但它们似乎都涉及 didSelectRowAtIndexPath,我不能在本例中使用它(当单元格被选中时,我有另一种行为).

我这样做是完全错误的吗?理想情况下,我想做的是让显示更多"按钮在 textview 展开时向下动画,反之亦然.

谢谢!

解决方案

beginUpdates 方法不会为你调用 reloadData - 你必须手动调用.>

对于您的情况,最好致电:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

并将您的 showMoreText 代码放在 tableView:cellForRowAtIndexPath: 方法中(仅适用于所选单元格)

I have a UITableView populated with custom UITableViewCells. Within those custom cells, I have a UITextField and a "See More" UIButton. The purpose of the UIButton is to dynamically expand that particular UITableCell when the user wishes to read more of the text. In the same way, when the user wishes to return to the original size, the user clicks the Button again, and the UITableViewCell will shrink to the original size.

Since the cell isn't being selected, I setup an IBAction within the Custom Cell like such:

//Within CustomCell.m

- (IBAction)showMoreText:(id)sender
{
    //instance bool variable to flag whether the cell has been resized
    self.hasBeenResized = YES;

    //turn off mask to bounds, otherwise cell doesnt seem to resize
    [[self.cellView layer] setMasksToBounds:NO];

    // Calculate the new sizes and positions for the textView and the button 
    CGRect newTextViewFrame = self.textView.frame;
    newTextViewFrame.size.height = self.textView.contentSize.height;
    self.textView.frame = newTextViewFrame;

    CGFloat bottomYPos = self.textView.frame.origin.y + self.textView.frame.size.height;
    CGRect buttonFrame = self.showMoreButton.frame;
    buttonFrame.origin.y = bottomYPos;
    self.showMoreButton.frame = buttonFrame;

    // Call begin and end updates
    [(UITableView*) self.superview beginUpdates];   
    [(UITableView*) self.superview endUpdates];

    // Set mask and put rounded corners on the cell
    [[self.cellView layer] setMasksToBounds:YES];
    [[self.cellView layer] setCornerRadius:10.0];
}

Following this, I have this in my ViewController class:

// Within ViewController.m
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"heightForRowAtIndexPath");

    CustomCell *cell = (CustomCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];

    if([cell hasBeenResized] == NO)
    {
        return cell.frame.size.height + 20;
    }
    else
    {
        return cell.frame.size.height + cell.textView.frame.origin.y + cell.textView.frame.size.height + cell.showMoreButton.frame.size.height + 20;
    }
}

What happens now is I can see the custom cell change the size of its textview, however, the table does not update the row height for that particular cell. Checking on the If-else statement there, it appears that hasBeenResized is always false, even though I set it to YES within the IBACtion of the CustomCell.

I have looked at other solutions here, but they all seem to involve didSelectRowAtIndexPath, which I cannot use in this instance (I have another behavior for the cell when it is selected).

Am I doing this completely wrong? Ideally, what I would like to do is to have the "Show More" button animate downwards as the textview is expanded and vice versa when it's collapsed.

Thank you!

解决方案

Method beginUpdates won't call reloadData for you - you have to do it manually.

For your case it would be best to call:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

And place your showMoreText code in tableView:cellForRowAtIndexPath: method (for selected cell only)

这篇关于尝试从自定义单元格上的 UIButton 展开/折叠 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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