UIButton 在 UITableView 的一部分中以编程方式创建以隐藏/显示该部分的内容

Posted

技术标签:

【中文标题】UIButton 在 UITableView 的一部分中以编程方式创建以隐藏/显示该部分的内容【英文标题】:UIButton created programmatically in a section of a UITableView to hide/show contents of the section 【发布时间】:2012-03-02 19:26:29 【问题描述】:

我以这种方式以编程方式创建了一个 UIbutton:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(hideOrShowWithButtonId:)forControlEvents:UIControlEventTouchDown];

这个按钮的目的是,当它被按下时,uitableview 中的一个部分的内容消失,当点击它时,内容又回来了。 因此,当单击此按钮时,它正在调用以下函数: (注意:self.cinemaButton、self.taxiButton 和 self.foodButton 是字符串而不是按钮)

-(void)hideOrShowWithButtonId:(id)sender;

NSArray *dummy=[[[NSArray alloc] initWithObjects:nil] autorelease];
NSArray *dummy2=[[NSArray alloc] initWithObjects:self.cinemaButton,self.taxiButton,self.foodButton,nil];
NSLog(@"%@",self.taxiButton);
if([[dummy2 objectAtIndex:[sender tag]]isEqual:@"Hide"])

    NSLog(@"Want to hide");
    [self.sections removeAllObjects];
    switch ([sender tag]) 
        case 0: 
            [self.sections addObject:dummy];
            [self.sections addObject:self.taxiFavorite];
            [self.sections addObject:self.foodFavorite];
            [self.tableView reloadData];
            self.cinemaButton=[NSString stringWithString:@"Show"];
            break;            
        case 1: 
            [self.sections addObject:self.cinemaFavorite];
            [self.sections addObject:dummy];
            [self.sections addObject:self.foodFavorite];
            [self.tableView reloadData];
            self.taxiButton=[NSString stringWithString:@"Show"];
            break;
        case 2: 
            [self.sections addObject:self.cinemaFavorite];
            [self.sections addObject:self.taxiFavorite];
            [self.sections addObject:dummy];
            [self.tableView reloadData];
            self.foodButton=[NSString stringWithString:@"Show"];
            break;
    
    NSLog(@"%@",self.taxiButton);

else

    NSLog(@"Want to show");
    switch ([sender tag]) 
        case 0: 
            [self.sections replaceObjectAtIndex:0 withObject:self.cinemaFavorite];
            [self.tableView reloadData];
            self.cinemaButton=[NSString stringWithString:@"Hide"];
            break;            
        case 1: 
            [self.sections replaceObjectAtIndex:1 withObject:self.taxiFavorite];
            [self.tableView reloadData];
            self.taxiButton=[NSString stringWithString:@"Hide"];
            break;
        case 2: 
            [self.sections replaceObjectAtIndex:2 withObject:self.cinemaFavorite];
            [self.tableView reloadData];
            self.foodButton=[NSString stringWithString:@"Hide"];
            break;
    

[dummy2 release];
NSLog(@"%@",self.taxiButton);

这个函数的问题是字符串(例如在我的例子中:self.taxi)正在以值@“Show”退出,但是当再次按下按钮时,它的值是@“Hide” 字符串 self.taxibutton 的值没有改变。因此该功能只能隐藏该部分的内容而不再显示它们。为什么会发生这种情况? 有没有更简单的方法来执行隐藏/显示 UItableView 中特定部分内容的任务?

【问题讨论】:

【参考方案1】:

在回答您的问题“有没有更简单的方法”时,您可能想看看来自 Apple at this link 的一些工作演示代码。此代码显示如何拥有可折叠部分。我尝试自行开发,但遇到了一些困难,最后得出结论,我的问题已通过此代码中使用的方法消除。关键是维护一个包含每个部分的信息的表,包括指向每个部分的标题视图的指针。我修改了这个演示代码,它立即运行良好。您应该能够自己编译和运行代码。

这里有一个不请自来的建议:如果您希望其他人查看并提供建议,您应该为您的变量选择更具描述性的名称。 dummydummy2 的目的并不明显,它给人的直接印象是提供帮助需要付出比应有的更多努力。

【讨论】:

以上是关于UIButton 在 UITableView 的一部分中以编程方式创建以隐藏/显示该部分的内容的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 UITableView 需要在场景大纲中的 UIButton 下面

如何在 UITableVIew 下添加 UIButton?

如何管理 UITableView + 动态自定义单元格(UIBUtton + UITextField + UIBUtton)插入?

在 UITableView 顶部按下时,UIButton 不会更改其状态

在 uibutton 的单击上动态重新创建 uitableview 部分

动态更改 UITableView 单元格中 UIButton 的图像