首先点击uitableview的customcell应该扩展它,然后第二个应该收缩它 [英] First Tap on customcell of uitableview should expand it and second should contract it

查看:76
本文介绍了首先点击uitableview的customcell应该扩展它,然后第二个应该收缩它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有这样的要求,首先点击uitableview的自定义单元格,其中有一个标签应该展开它,然后应该收缩它。我能够扩展和收缩单元格并扩展单元格内的标签,但不能在第二次点击时收缩标签。

In my application I have this requirement that first tap on custom cell of uitableview with a label in it should expand it and second should contract it. I'm able to expand and contract cell and expand label inside cell, but not able to contract the label on second tap.

我正在使用此功能

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected animated:animated];
if( selected == YES ) {
    [self expandRow];
}
else {
    [self contractRow];
}

height = [lblFeed frame].size.height + 75;
}

expandRow扩展标签,contractRow收缩它。我很困惑这个函数被调用了多少行。它不仅仅被调用的单元调用,它被调用更多次单个单元格上的单击可能是其他单元格,但我没有得到哪些行。

expandRow expands the label and contractRow contracts it. I'm perplexed as for how many rows this function gets called. It doesn't get called only for the cell tapped, it gets called more number of times for single tap on single cell may be for other cells but I'm not getting which rows.

这真的很紧急。

有人可以帮忙吗?

推荐答案

点击选定的行不会导致取消选择它。当一个单元格被选中时,它会保持选中状态,直到deselectRowAtIndexPath:animated:在其表格中被调用。这就是为什么第二次调用没有调用你的方法的原因。

Tapping a selected row doesn't cause it to be deselected. When a cell gets selected, it stays selected until deselectRowAtIndexPath:animated: gets called on its table. That's why your method isn't getting called for the second tap.

在像UIKit这样的MVC架构中,建议你在控制器类中处理用户交互。如果您所做的只是自定义视图表示选定单元格的方式,那么覆盖 - [UITableViewCell setSelected:animated:]是合适的,但在这种情况下,您的扩展/合约切换行为需要更改UITableView选择的方式和取消选择它的单元格。

In an MVC architecture like UIKit, it's recommended that you handle user interactions in your controller classes. It would be appropriate to override -[UITableViewCell setSelected:animated:] if all you were doing was customizing the way the view represents a selected cell, but in this case your expand/contract toggle behavior would require a change in the way UITableView selects and deselects its cells.

您可以自己继承UITableView并实现此切换行为,或者您可以单独使用UITableView并通过执行以下操作在UIViewController级别处理它:

You could subclass UITableView and implement this toggle behavior yourself, or you can leave UITableView alone and handle it all at the UIViewController level by doing something like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.expandedIndexPath isEqual:indexPath]) {
        [(YourCustomCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath] contractRow];
        self.expandedIndexPath = nil;
    }
    else {
        if (self.expandedIndexPath) {
            [(YourCustomCell *)[self tableView:tableView cellForRowAtIndexPath:self.expandedIndexPath] contractRow];
        }
        [(YourCustomCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath] expandRow];
        self.expandedIndexPath = indexPath;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

这篇关于首先点击uitableview的customcell应该扩展它,然后第二个应该收缩它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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