簡體   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