Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.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 首先点击uitableview的customcell应该将其展开,第二个应该将其收缩_Iphone_Uitableview - Fatal编程技术网

Iphone 首先点击uitableview的customcell应该将其展开,第二个应该将其收缩

Iphone 首先点击uitableview的customcell应该将其展开,第二个应该将其收缩,iphone,uitableview,Iphone,Uitableview,在我的应用程序中,我有这样一个要求,即首先点击带有标签的uitableview的自定义单元格,然后将其展开,然后将其收缩。我能够展开和收缩单元格,并在单元格内展开标签,但无法在第二次点击时收缩标签 我正在使用这个函数 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; if( selected == YES ) { [se

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

我正在使用这个函数

- (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收缩标签。我不知道这个函数被调用了多少行。它并不是只为被点击的单元格被调用,它被调用的次数更多,因为单个单元格上的单个点击可能是为其他单元格,但我不知道是哪一行

这真的很紧急


有人能帮忙吗?

我建议您不要在单元格的选定属性上添加功能,因为它的行为与您预期的稍有不同


只需添加您自己的
BOOL expanded
属性,看看它是如何工作的。您可能也应该从
UITableView委托
方法调用它。

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

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

您可以将UITableView子类化并自己实现此切换行为,也可以不使用UITableView,通过执行以下操作在UIViewController级别处理这一切:

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

在您的代码中,您从何处调用我在tableview单元格上呈现的customCell类对象中的函数“setSelected”。但您所说的“subclass UITableView”是什么意思呢?我已经有一个类将UITableView子类化并处理我的所有tableview行为。我应该将其包含在该类中,还是创建一个新类?另外,由于这是tableView委托方法,如何在uiviewcontroller中处理此问题?Thanx..如果您已经对UITableView进行了子类化,那么您可以实现在用户触摸选定单元格时取消选择该单元格的代码。这就是你丢失的那一块。当我说UIViewController时,我应该说UITableViewController,它实现了UITableViewDelegate和UITableViewDataSource。如果您使用其他类作为UITableViewDelegate,您可以在那里实现相同的功能。