繁体   English   中英

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

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

我在自定义tableview单元格中有一个类似facebook的按钮。 在表视图的类中,我具有以下功能。

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

在我的cellForRowAtIndexpath中,我尝试以以下方式调用此方法。

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

但是它抱怨我应该在我的@selector中输入':'。 有人可以帮忙吗?

提前致谢。

你可以这样尝试

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

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

如Apple文档中所述,操作中@selector唯一可用的规范是以下方法:

- (void)action;

- (void)action:(id)sender;

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

如您所见,您的选择器与此不匹配,因为您有多个参数,但只允许发送者id

作为解决方案,您可以为按钮设置tag ,然后在选择器中进行处理。

在您的cellForRow ://将自定义字符串连接到按钮

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

在您的按钮处理程序中:

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

ps不要忘记导入运行时库: #import <objc/runtime.h>

我认为您以错误的方式调用了选择器。 你实际上应该写

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

但这不会为您传递参数。 如果您想传递参数,则应像这样调用performSelector

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

希望它能帮助您解决问题。

您的问题是选择器中参数的数量和种类不正确。 您只能将目标与以下方案一起使用:

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

您将必须在发件人( UITableViewCell类实例)对象中包含您的信息。 您可以按照Stas的其他答案中的描述进行操作,但是我建议您创建一个自定义表视图单元子类,该子类为shareStringshareURL添加一个属性。 这种方法将需要更多的代码,但使其更易于阅读,维护并防止这种丑陋的Obj-C和C混淆。

暂无
暂无

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

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