简体   繁体   English

双格按钮动作

[英]double cell button action

I have a UITableView which has a xib file for UITableViewCell. 我有一个UITableView,其中有一个针对UITableViewCell的xib文件。

On the UITableViewCell I am adding 2 cell data - because I want to implement a two column style. 在UITableViewCell上,我添加了2个单元格数据-因为我想实现两列样式。 so can't be modified. 因此无法修改。 The cell is a bit complex has more components too, and I want to handle the action of buttons UIControlEventTouchUpInside event. 该单元有点复杂,它还有更多的组件,我想处理按钮UIControlEventTouchUpInside事件的动作。 It will download something and need to change the button text after task competition from "Download" to "View" for eg. 它将在任务竞赛后下载一些内容并需要将按钮文本从“下载”更改为“查看”,例如。

The problem it would be solved, if I wouldn't handle the multithreading, async stuff, but needed. 如果我不处理多线程,异步的东西,但是需要的话,它将解决。 So I need to pass the button and an index to the action method, or set a property for the button ( in Java is easy ) but couldn't find it. 因此,我需要将按钮和索引传递给action方法,或者为按钮设置属性(在Java中很容易),但找不到它。

The half code looks like this: 一半的代码如下所示:

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

and need something like this: 并且需要这样的东西:

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

}

In other question the index is passed with buttonAction.tag = index but if I chnage that property, than I think it wont work the 在另一个问题中,索引是通过buttonAction.tag = index传递的,但是如果我更改该属性,那么我认为它不会起作用

UIButton *buttonAction = (UIButton *)[cell viewWithTag:205]; UIButton * buttonAction =(UIButton *)[cell viewWithTag:205]; statement anymore at 在发表声明

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

Any suggestion how to handle this case? 有什么建议如何处理这种情况?

You can subclass UIButton class and have properties for all the information you need. 您可以UIButton类,并具有所需所有信息的属性。 Then you add action to your button for TouchUpInside with @selector(buttonPressed:) 然后使用@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
}

EDIT 编辑

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

and then work depending on your title: button.currentTitle 然后根据您的标题进行工作: button.currentTitle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM