Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 尝试从自定义单元格上的UIButton展开/折叠UITableViewCell_Iphone_Uitableview - Fatal编程技术网

Iphone 尝试从自定义单元格上的UIButton展开/折叠UITableViewCell

Iphone 尝试从自定义单元格上的UIButton展开/折叠UITableViewCell,iphone,uitableview,Iphone,Uitableview,我有一个用自定义UITableViewCells填充的UITableView。在这些自定义单元格中,我有一个UITextField和一个“查看更多”UIButton。UIButton的用途是在用户希望阅读更多文本时动态扩展特定UITableCell。同样,当用户希望返回到原始大小时,再次单击按钮,UITableViewCell将收缩到原始大小 由于未选择单元格,我在自定义单元格中设置了一个iAction,如下所示: //在CustomCell.m内 - (IBAction)showMoreTex

我有一个用自定义UITableViewCells填充的UITableView。在这些自定义单元格中,我有一个UITextField和一个“查看更多”UIButton。UIButton的用途是在用户希望阅读更多文本时动态扩展特定UITableCell。同样,当用户希望返回到原始大小时,再次单击按钮,UITableViewCell将收缩到原始大小

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

//在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];
}
接下来,我的ViewController类中有以下内容:

// 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;
    }
}
现在发生的事情是,我可以看到自定义单元格更改其textview的大小,但是,表不会更新该特定单元格的行高。检查If-else语句,hasBeenResized似乎总是false,即使我在CustomCell的iAction中将其设置为YES

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

我这样做完全错了吗?理想情况下,我想做的是让“显示更多”按钮在文本视图展开时向下动画,反之亦然,当它折叠时向下动画


谢谢大家!

要更新tableView,必须重新加载它

[tableView reloadData];

方法
beginUpdates
不会为您调用
reloadData
——您必须手动执行

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

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

然后将您的
showMoreText
代码放入
tableView:cellforrow索引路径:
method(仅适用于所选单元格)

您是否检查了调用的iAction方法?是否将hasBeenResized分配到代码中的其他位置?+1是一个好问题-有用我将其放在我的自定义单元格的iAction中,但现在该单元格没有显示任何高度变化。但是行高确实生效了。是的。没关系。但是TableView数据源-(CGFloat)TableView:(UITableView*)TableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{return num;}将仅在重新加载tableView@kurisukun:不,BeginUpdate与调用heightForRowAtIndexPath无关。您需要调用其中一个重载方法。我在heightForRowAtIndexPath中放置了一个NSLog,并从我的iAction调用了BeginUpdate。当我点击按钮时,NSLog出现了。从我的IBAction中删除BeginUpdate也会在点击Show More按钮时删除NSLog输出。我想这意味着这将被调用。您可以在showMoreText结束时(或在此调用后调用showMoreText的方法中)执行此操作—您需要单击“查看更多”按钮的单元格的indexPath。无论使用此函数还是[table reloadData],我都会遇到同样的问题。行高将设置动画,但单元格大小保持不变。删除我的IBAction中的重载数据(其中也包含调整大小的CGRECT)会使单元格调整大小,但不会使行调整大小。嗯,您的showMoreText代码实际上应该在CellForRowatineXpath中实现(对于所选单元格)-抱歉之前没有注意到这一点谢谢您对该问题的所有评论,我真的很感激!关于在cellForRowAtIndexPath中放置ShowMoreText,这意味着我需要放置某种标志或跟踪点击按钮的行,对吗?@kurisukun:是的。然后在cellForRowAtIndexPath中,返回所有其他单元格“正常”,以及带有扩展内容和框架的选定单元格。