【问题标题】:double cell button action双单元按钮操作
【发布时间】:2013-07-04 11:06:41
【问题描述】:

我有一个 UITableView,它有一个用于 UITableViewCell 的 xib 文件。

在 UITableViewCell 上,我添加了 2 个单元格数据 - 因为我想实现两列样式。所以不能修改。单元格有点复杂,也有更多组件,我想处理按钮 UIControlEventTouchUpInside 事件的操作。它将下载一些东西,并且需要在任务竞争后将按钮文本从“下载”更改为“查看”,例如。

如果我不处理多线程、异步的东西,它会解决的问题,但需要。所以我需要将按钮和索引传递给操作方法,或者为按钮设置一个属性(在Java中很容易)但找不到它。

一半代码如下所示:

    [buttonAction addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

并且需要这样的东西:

-(void)buttonPressed:(UIButton*)button index: (int) index
{

}

在其他问题中,索引是通过buttonAction.tag = index 传递的,但是如果我更改该属性,那么我认为它不会起作用

UIButton *buttonAction = (UIButton *)[cell viewWithTag:205];声明不再在

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

任何建议如何处理这种情况?

【问题讨论】:

    标签: ios uitableview uibutton


    【解决方案1】:

    您可以继承UIButton 类并拥有所有您需要的信息的属性。然后你用@selector(buttonPressed:)为你的TouchUpInside按钮添加动作

    -(void)buttonPressed:(YourCustomButton *)sender  // or id and then you check it's class and cast to your custom button class
    {
        // work with sender.yourProperty
    }
    

    编辑

     [button setTitle:@"Download" forState:UIControlStateNormal];
     [button setTitle:@"View" forState:UIControlStateReserved];
    

    然后根据你的标题工作:button.currentTitle

    【讨论】:

    • +1,我有点懒得继承UIButton,添加到XCode组件编辑器,在xib文件中更改UIButton等等,寻找其他解决方案
    • 这种方法的一个问题是,您必须使用 alloc/init 而不是 buttonWithType: 方法创建按钮对象。 From the docs: 如果你是 UIButton 的子类,这个方法不会返回你的子类的实例。如果要创建特定子类的实例,则必须直接分配/初始化按钮。
    • 感谢 Amar 的宝贵意见。
    • 我没试过,但我想你可以使用setTitle:forState: 方法。可以使用的状态有很多..UIControlStateNormal ,UIControlStateHighlighted,UIControlStateApplication,UIControlStateReserved
    • @Tala 我没有看到您的评论并找到了解决方案,这与您在评论中建议的相同。请添加您的评论作为新答案(使用我的代码)以被接受!
    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 2021-03-23
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    相关资源
    最近更新 更多