简体   繁体   English

使用自定义表格视图单元格中的按钮

[英]Working with a button in a custom tableview Cell

I have a facebook like button in a custom tableview cell. 我在自定义tableview单元格中有一个类似facebook的按钮。 In the class of my tableview I have the following function. 在表视图的类中,我具有以下功能。

- (IBAction)sendLike:(id)sender
          WithString: (NSString *)shareString
              andUrl:(NSURL *)shareUrl{

          //Do all kinds of things


        [fbController setInitialText:shareString];
        [fbController addURL:shareUrl];
        [fbController setCompletionHandler:completionHandler];
        [self presentViewController:fbController animated:YES completion:nil];
    }
}

In my cellForRowAtIndexpath I am trying to call this method in the following way. 在我的cellForRowAtIndexpath中,我尝试以以下方式调用此方法。

 NSString *shareString = video.name;
        NSURL *shareUrl = [NSURL URLWithString:video.url];
        [cell.facebook addTarget:self action:@selector(sendLike: WithString:shareString andUrl:shareUrl)
                 forControlEvents:UIControlEventTouchUpInside];

But it is complaining that I should put ':' in my @selector. 但是它抱怨我应该在我的@selector中输入':'。 Can anybody help ? 有人可以帮忙吗?

Thanks in advance. 提前致谢。

You can try like this 你可以这样尝试

cell.facebook.tag = indexPath.row.
    [cell.facebook addTarget:self action:@selector(sendLike:)
                     forControlEvents:UIControlEventTouchUpInside];

in sendLike event 在sendLike事件中

- (IBAction)sendLike:(id)sender
  {


        //if you want to fetch data from any list then try
        //UIButton *selectedButton = (UIButton*)sender;
        //data = [dataList objectAtIndex:selectedButton.tag];
        NSString *shareString = video.name;
        NSURL *shareUrl = [NSURL URLWithString:video.url];

        [fbController setInitialText:shareString];
        [fbController addURL:shareUrl];
        [fbController setCompletionHandler:completionHandler];
        [self presentViewController:fbController animated:YES completion:nil];
    }

As its in the apple documentation, the only available specifications for an @selector in an action are the following methods: 如Apple文档中所述,操作中@selector唯一可用的规范是以下方法:

- (void)action;

- (void)action:(id)sender;

- (void)action:(id)sender forEvent:(UIEvent *)event;

As you can see, your selector doesn't match this, because you have multiple arguments, but only the sender id is allowed. 如您所见,您的选择器与此不匹配,因为您有多个参数,但只允许发送者id

As a solution, you can probably set a tag for your button and then handle it in your selector. 作为解决方案,您可以为按钮设置tag ,然后在选择器中进行处理。

In your cellForRow : //wiring a custom string to a button 在您的cellForRow ://将自定义字符串连接到按钮

objc_setAssociatedObject(yourButton, "theKey", strText, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

In your button handler: 在您的按钮处理程序中:

NSString *str = objc_getAssociatedObject(sender, "theKey");

ps don't forget to import runtime library: #import <objc/runtime.h> ps不要忘记导入运行时库: #import <objc/runtime.h>

I think you invoked the selector in wrong way. 我认为您以错误的方式调用了选择器。 You actually should write, 你实际上应该写

[cell.facebook addTarget:self action:@selector(sendLike: WithString: andUrl:)  
                                     forControlEvents:UIControlEventTouchUpInside];  

But this doesn't pass arguments for you. 但这不会为您传递参数。 If you want to pass arguments, you should call performSelector like 如果您想传递参数,则应像这样调用performSelector

[self performSelector:@selector(sendLike: WithString: andUrl:) 
                                withObject: cell.facebook
                                withObject: shareString
                                withObject: shareUrl]; 

Hope it will help solving your problem. 希望它能帮助您解决问题。

Your problem is that the number and kind of arguments are incorrect in your selector. 您的问题是选择器中参数的数量和种类不正确。 You can only use targets with the following scheme: 您只能将目标与以下方案一起使用:

 - (void)action;
 - (void)action:(id)sender;
 - (void)action:(id)sender forEvent:(UIEvent *)event;

You will have to include your information in the sender (the UITableViewCell class instance) object. 您将必须在发件人( UITableViewCell类实例)对象中包含您的信息。 You can do that as described in the other answer by Stas, but I would recommend you to create a custom table view cell subclass which adds a property for shareString and shareURL . 您可以按照Stas的其他答案中的描述进行操作,但是我建议您创建一个自定义表视图单元子类,该子类为shareStringshareURL添加一个属性。 This approach will require more code, but make it much easier to read, maintain and prevents this ugly Obj-C and C mix up. 这种方法将需要更多的代码,但使其更易于阅读,维护并防止这种丑陋的Obj-C和C混淆。

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

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