UITableViewCell के अंदर बटन क्लिक करें


140

मेरे पास टेबल व्यू के साथ एक व्यू कंट्रोलर है और टेबल सेल टेम्पलेट के लिए एक अलग नीब है। सेल टेम्पलेट में कुछ बटन होते हैं। मैं व्यू कंट्रोलर के अंदर क्लिक किए गए सेल के सूचकांक के साथ बटन क्लिक को एक्सेस करना चाहता हूं जहां मैंने टेबल व्यू को परिभाषित किया है।

तो मेरे पास है ViewController.hऔर ViewController.mजहां मैं UITableViewऔर TableTemplate.h, TableTemplate.mऔर TableTemplate.xibनिब परिभाषित मैं कहाँ है। मैं सेल इंडेक्स के साथ बटन क्लिक इवेंट चाहता हूं ViewController.m

मैं कैसे कर सकता हूँ पर कोई मदद?

जवाबों:


258

1) अपनी cellForRowAtIndexPath:विधि में, अनुक्रमणिका के रूप में बटन टैग असाइन करें:

cell.yourbutton.tag = indexPath.row;

2) नीचे दिए गए अनुसार अपने बटन के लिए लक्ष्य और क्रिया जोड़ें:

[cell.yourbutton addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

3) नीचे दिए अनुसार सूचकांक के आधार पर कोड क्रियाएँ ViewControler:

-(void)yourButtonClicked:(UIButton*)sender
{
     if (sender.tag == 0) 
     {
         // Your code here
     }
}

एकाधिक अनुभाग के लिए अपडेट:

आप कई लिंक और अनुभाग के लिए तालिका दृश्य में बटन क्लिक का पता लगाने के लिए इस लिंक की जांच कर सकते हैं ।


1
यह चरण दो में इंटरफेस बिल्डर (आईबी) के माध्यम से भी किया जा सकता है। बस सुनिश्चित करें कि आपके बटन का टैग सेट है। आप वास्तव में अपने एक्शन कॉलिंग को मिलाना नहीं चाहते हैं। या तो आईबी के माध्यम से करें या अपने कोड में स्पष्ट रूप से करें।
सेंटेनिया

@ Mani यह MVC नहीं तोड़ती है - कार्रवाई TableView सेल नहीं है।
14'14

@davecom यदि आप बटन लक्ष्य को सेल के रूप में सेट करते हैं (IB के माध्यम से), तो इसे tableView से कैसे ट्रिगर किया जाएगा? या क्या उनके पास बटन लक्ष्य को टेबलव्यू से जोड़ने का कोई तरीका है जो सेल के xib में रखा गया है?
मणि

24
यह समाधान समस्याओं में चलता है जब आप पंक्तियों को सम्मिलित करना और हटाना शुरू करते हैं। पंक्तियों को स्थानांतरित करने पर टैग अपडेट नहीं किया जाता है। इसके बजाय पंक्ति के लिए एक संदर्भ रखने के। एक अद्वितीय ऑब्जेक्ट आईडी का संदर्भ रखना बेहतर हो सकता है।
विंसेंट चोंग

1
जब भी आप अपने आप को विचारों की टैग विशेषताओं के लिए मान प्रदान करते हैं, तो आपके पास बहुत खराब कोड गंध है जो आपको बाद में काट सकता है। अपने लक्ष्य को प्राप्त करने के बेहतर तरीकों की तलाश करें, न कि पहला एसओ पद जो आप पाते हैं।
टाइगरकोडिंग

148

डेलीगेट्स जाने का रास्ता हैं।

जैसा कि विचारों का उपयोग करते हुए अन्य उत्तरों के साथ देखा गया है वह पुराना हो सकता है। कल कौन जानता है कि एक और आवरण हो सकता है और उपयोग करने की आवश्यकता हो सकती है cell superview]superview]superview]superview]। और यदि आप टैग का उपयोग करते हैं तो आप सेल की पहचान करने के लिए अन्य शर्तों की संख्या के साथ समाप्त हो जाएंगे। उस सब से बचने के लिए प्रतिनिधियों को सेट करें। (ऐसा करने से आप एक पुनः प्रयोग करने योग्य सेल क्लास बना रहे होंगे। आप उसी सेल क्लास को बेस क्लास के रूप में उपयोग कर सकते हैं और आपको बस इतना करना है कि प्रतिनिधि विधियों को लागू करें।)

पहले हमें एक इंटरफ़ेस (प्रोटोकॉल) की आवश्यकता होती है जो सेल द्वारा संचार (प्रतिनिधि) बटन क्लिक करने के लिए उपयोग किया जाएगा। ( आप प्रोटोकॉल के लिए एक अलग। H फ़ाइल बना सकते हैं और इसमें टेबल व्यू कंट्रोलर और कस्टम सेल क्लास दोनों को शामिल कर सकते हैं या सिर्फ कस्टम सेल क्लास में इसे जोड़ सकते हैं जो कि टेबल व्यू कंट्रोलर में शामिल हो जाएंगे )

@protocol CellDelegate <NSObject>
- (void)didClickOnCellAtIndex:(NSInteger)cellIndex withData:(id)data;
@end

इस प्रोटोकॉल को कस्टम सेल और टेबल व्यू कंट्रोलर में शामिल करें। और सुनिश्चित करें कि तालिका दृश्य नियंत्रक इस प्रोटोकॉल की पुष्टि करता है।

कस्टम सेल में दो गुण बनाएँ:

@property (weak, nonatomic) id<CellDelegate>delegate;
@property (assign, nonatomic) NSInteger cellIndex;

में UIButtonIBAction प्रतिनिधि क्लिक: ( एक ही कस्टम सेल कक्षा में किसी भी कार्रवाई कौन सा दृश्य नियंत्रक को वापस सौंप किए जाने की आवश्यकता के लिए किया जा सकता है )

- (IBAction)buttonClicked:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickOnCellAtIndex:withData:)]) {
        [self.delegate didClickOnCellAtIndex:_cellIndex withData:@"any other cell data/property"];
    }
}

cellForRowAtIndexPathसेल को हटाने के बाद तालिका दृश्य नियंत्रक में , उपरोक्त गुणों को सेट करें।

cell.delegate = self;
cell.cellIndex = indexPath.row; // Set indexpath if its a grouped table.

और प्रतिनिधि को टेबल व्यू कंट्रोलर में लागू करें:

- (void)didClickOnCellAtIndex:(NSInteger)cellIndex withData:(id)data
{
    // Do additional actions as required.
    NSLog(@"Cell at Index: %d clicked.\n Data received : %@", cellIndex, data);
}

यह टेबल व्यू कंट्रोलर में कस्टम सेल बटन क्रियाओं को प्राप्त करने के लिए आदर्श दृष्टिकोण होगा।


2
आपने प्रतिनिधि को सेल की एक मजबूत संपत्ति क्यों बनाया है? यह आपको एक चक्र बनाए रखेगा, जब तक कि आपको पता न हो कि कंट्रोलर केवल सेल को कमजोर रखता है।
जूलियनसम जूल

सेल डिलीट होने के बाद _cellIndex beign के बारे में क्या अपडेट किया गया है?
स्कोर्नोस

2
मैंने अपने एक मित्र से यह कहते हुए सुना कि प्रत्येक कोशिका पर प्रतिनिधि का उपयोग करने से मेमोरी की खपत होती है, इसलिए टैग का उपयोग करें। क्या ये सच है?
बिस्टा

2
इस सवाल का दोस्त जाँच stackoverflow.com/questions/31649220/...
Nischal हाडा

@the_UB एक टैग सेट करने और एक एकल संदर्भ संग्रहीत करने के बीच बहुत कुछ नहीं हो सकता है। संभवतः एक टैग अधिक मेमोरी लेगा।
इयॉन वारबर्टन

66

टैग के साथ खेलने के बजाय, मैंने अलग तरीका अपनाया। UITableViewCell (OptionButtonsCell) के मेरे उपवर्ग के लिए प्रतिनिधि बनाया और एक indexPath संस्करण जोड़ा। स्टोरीबोर्ड में अपने बटन से मैंने @IBAction को OptionButtonsCell से जोड़ा और वहां मैं रुचि रखने वाले किसी व्यक्ति को सही IndexPath के साथ प्रतिनिधि विधि भेजता हूं। सूचकांक पथ के लिए सेल में मैंने वर्तमान indexPath सेट किया है और यह काम करता है :)

कोड को अपने लिए बोलने दें:

स्विफ्ट 3 एक्सकोड 8

OptionButtonsTableViewCell.swift

import UIKit
protocol OptionButtonsDelegate{
    func closeFriendsTapped(at index:IndexPath)
}
class OptionButtonsTableViewCell: UITableViewCell {
    var delegate:OptionButtonsDelegate!
    @IBOutlet weak var closeFriendsBtn: UIButton!
    var indexPath:IndexPath!
    @IBAction func closeFriendsAction(_ sender: UIButton) {
        self.delegate?.closeFriendsTapped(at: indexPath)
    }
}

MyTableViewController.swift

class MyTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, OptionButtonsDelegate {...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "optionCell") as! OptionButtonsTableViewCell
    cell.delegate = self
    cell.indexPath = indexPath
    return cell   
}

func closeFriendsTapped(at index: IndexPath) {
     print("button tapped at index:\(index)")
}

क्या आप मेरी मदद कर सकते हैं, मुझे इस लाइन पर त्रुटि हो रही है: class MyTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, OptionButtonsDelegate // त्रुटि: 'MyTableViewController' प्रोटोकॉल के अतिरेक अनुरूप 'UITableViewDataSource'
उलुगबेक Ro'zimboyev

ऐसा लगता है कि आप कई बार UITableViewDataSource के अनुरूप होने का प्रयास कर रहे हैं। शायद आपको एक एक्सटेंशन मिल गया है जहां आप पहले से ही डेटा स्रोत के अनुरूप हैं ?, बिना कोड के ज्यादा मदद नहीं कर सकते
Maciej Chrzastek

1
और segue करने के लिए डेटा कैसे पास करें और दूसरे व्यू कंट्रोलर के पास जाएं?
मिलाद फरीदिया

2
सबसे अच्छा और सबसे साफ समाधान!
appunited

31

यह मदद करनी चाहिए: -

UITableViewCell* cell = (UITableViewCell*)[sender superview];
NSIndexPath* indexPath = [myTableView indexPathForCell:cell];

यहाँ प्रेषक UIButton उदाहरण है जो ईवेंट भेज रहा है। myTableView वह UITableView उदाहरण है जिससे आप निपट रहे हैं।

बस सेल संदर्भ सही हो और सभी काम किया जाता है।

आपको सेल के contentView से बटन हटाने और उन्हें उप-दृश्य के रूप में सीधे UITableViewCell उदाहरण में जोड़ने की आवश्यकता हो सकती है।

या

आप cell.contentView में विभिन्न UIButtons के लिए एक टैग नामकरण योजना तैयार कर सकते हैं। इस टैग का उपयोग करके, बाद में आप आवश्यकतानुसार पंक्ति और अनुभाग जानकारी जान सकते हैं।


4
यह होना चाहिए [[प्रेषक पर्यवेक्षक] पर्यवेक्षक];
पियर्स

2
यह बहुत ही सरल कोशिकाओं के लिए अच्छा है। हालांकि, अगर आपके सेल में विचारों का एक गहरा पेड़ है, तो मणि का उत्तर सबसे अच्छा है।
सेंटेनिया

3
अब iOS 7 में यह UITableViewCell * cell = (UITableViewCell *) [[[प्रेषक पर्यवेक्षक] पर्यवेक्षक] पर्यवेक्षा]] होना चाहिए; धन्यवाद।
राजन महारजन

इस सवाल का दोस्त जाँच stackoverflow.com/questions/31649220/...
Nischal हाडा

22

निम्नलिखित कोड आपकी मदद कर सकता है।

मैंने UITableViewकस्टम प्रोटोटाइप सेल क्लास को UITableViewCellअंदर ले लिया है UIViewController

तो मेरे पास है ViewController.h, ViewController.mऔर TableViewCell.h,TableViewCell.m

यहाँ उस के लिए कोड है:

ViewController.h

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tblView;

@end

ViewController.m

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return (YourNumberOfRows);
}

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

    static NSString *cellIdentifier = @"cell";

    __weak TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (indexPath.row==0) {
        [cell setDidTapButtonBlock:^(id sender)
         {
             // Your code here

         }];
    }    
    return cell;
}

कस्टम सेल वर्ग:

TableViewCell.h

@interface TableViewCell : UITableViewCell

@property (copy, nonatomic) void (^didTapButtonBlock)(id sender);

@property (strong, nonatomic) IBOutlet UILabel *lblTitle;
@property (strong, nonatomic) IBOutlet UIButton *btnAction;

- (void)setDidTapButtonBlock:(void (^)(id sender))didTapButtonBlock;

@end

तथा

UITableViewCell.m

@implementation TableViewCell

- (void)awakeFromNib {
    // Initialization code
    [self.btnAction addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)didTapButton:(id)sender {
    if (self.didTapButtonBlock)
    {
        self.didTapButtonBlock(sender);
    }
}

नोट : यहां मैंने UIControlsस्टोरीबोर्ड का उपयोग करके सभी लिया है ।

आशा है कि आपकी मदद कर सकते हैं ... !!!


अब तक का सबसे अच्छा तरीका
डैनियल रऊफ

15

कारण मुझे नीचे की तकनीक पसंद है क्योंकि यह मुझे तालिका के अनुभाग की पहचान करने में भी मदद करता है।

सेल में बटन जोड़ें सेलफ़ोरऑटइंडेक्सपैथ:

 UIButton *selectTaskBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [selectTaskBtn setFrame:CGRectMake(15, 5, 30, 30.0)];
        [selectTaskBtn setTag:indexPath.section]; //Not required but may find useful if you need only section or row (indexpath.row) as suggested by MR.Tarun 
    [selectTaskBtn addTarget:self action:@selector(addTask:)   forControlEvents:UIControlEventTouchDown];
[cell addsubview: selectTaskBtn];

इवेंट addTask:

-(void)addTask:(UIButton*)btn
{
    CGPoint buttonPosition = [btn convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil)
    {
     int currentIndex = indexPath.row;
     int tableSection = indexPath.section;
    }
}

इस मदद की उम्मीद है।


इस सवाल का दोस्त जाँच stackoverflow.com/questions/31649220/...
Nischal हाडा

12

स्विफ्ट क्लोजर का उपयोग करें:

class TheCell: UITableViewCell {

    var tapCallback: (() -> Void)?

    @IBAction func didTap(_ sender: Any) {
        tapCallback?()
    }
}

extension TheController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: TheCell.identifier, for: indexPath) as! TheCell {
            cell.tapCallback = {
                //do stuff
            }
            return cell
    }
}

7

तरुण का कोड iOS7 पर काम नहीं करता है, क्योंकि UITableViewCell संरचना बदल गई है और अब उसे इसके बजाय "UITableViewCellScrollView" मिलेगा।

IOS 7 में सुपरवाइज के साथ UITableViewCell हो रही इस पोस्ट में संरचना में भविष्य के किसी भी बदलाव की परवाह किए बिना सही अभिभावक दृश्य खोजने के लिए एक अच्छा समाधान है। यह एक लूप बनाने के लिए उबलता है:

    UIView *superView = [sender superview];
    UIView *foundSuperView = nil;

    while (nil != superView && nil == foundSuperView) {
        if ([superView isKindOfClass:[UITableViewCell class]]) {
            foundSuperView = superView;
        } else {
            superView = superView.superview;
        }
    }

लिंक में अधिक पुन: प्रयोज्य समाधान के लिए कोड है, लेकिन यह काम करना चाहिए।


6

स्विफ्ट 2.2

आपको उस बटन के लिए लक्ष्य जोड़ना होगा।

myButton.addTarget(self, action: #selector(ClassName.FunctionName(_:), forControlEvents: .TouchUpInside)

फंक्शननाम: जुड़ा // उदाहरण के लिए

और निश्चित रूप से आपको उस बटन का टैग सेट करने की आवश्यकता है क्योंकि आप इसका उपयोग कर रहे हैं।

myButton.tag = indexPath.row

UITableViewCell को उप-लिंक करके आप इसे प्राप्त कर सकते हैं। इसे इंटरफ़ेस बिल्डर में उपयोग करें, उस सेल पर एक बटन छोड़ें, इसे आउटलेट के माध्यम से कनेक्ट करें और वहां आप जाएं।

जुड़े समारोह में टैग पाने के लिए:

func connected(sender: UIButton) {
    let buttonTag = sender.tag
    // Do any additional setup
}

6

एक बंद के साथ 3 स्विफ्ट

एक अच्छा समाधान एक कस्टम UITableViewCell में एक कार्रवाई के लिए viewController को कॉलबैक करने के लिए एक बंद का उपयोग कर रहा है।

सेल में:

final class YourCustomCell: UITableViewCell {

    var callbackClosure: (() -> Void)?

    // Configure the cell here
    func configure(object: Object, callbackClosure: (() -> Void)?) {
       self.callbackClosure = callbackClosure
    }


// MARK: - IBAction
extension YourCustomCell {
    @IBAction fileprivate func actionPressed(_ sender: Any) {
        guard let closure = callbackClosure else { return }
        closure()
    }
}

व्यू कंट्रोलर में: टेबलव्यू डेलिगेट

extension YourViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        guard let cell: YourCustomCell = cell as? YourCustomCell else { return }
        cell.configure(object: object, callbackClosure: { [weak self] in
            self?.buttonAction()
        })
     }
 }

fileprivate extension YourViewController {

    func buttonAction() {
        // do your actions here 
    }
}

5

मुझे आपके सेल (स्विफ्ट 3) के अंदर के बटन को दबाना सबसे सरल लगता है:

class MyCellInfoButton: UIButton {
    var indexPath: IndexPath?
}

आपके सेल क्लास में:

class MyCell: UICollectionViewCell {
    @IBOutlet weak var infoButton: MyCellInfoButton!
   ...
}

तालिका दृश्य या संग्रह दृश्य के डेटा स्रोत में, सेल को हटाते समय, बटन को अपनी अनुक्रमणिका पथ दें:

cell.infoButton.indexPath = indexPath

इसलिए आप इन कोड को अपने टेबल व्यू कंट्रोलर में डाल सकते हैं:

@IBAction func handleTapOnCellInfoButton(_ sender: MyCellInfoButton) {
        print(sender.indexPath!) // Do whatever you want with the index path!
}

और अपने इंटरफ़ेस बिल्डर में बटन के वर्ग को सेट करने और handleTapOnCellInfoButtonफ़ंक्शन से लिंक करने के लिए मत भूलना !


संपादित:

निर्भरता इंजेक्शन का उपयोग करना। क्लोजर कॉलिंग सेट करने के लिए:

class MyCell: UICollectionViewCell {
    var someFunction: (() -> Void)?
    ...
    @IBAction func didTapInfoButton() {
        someFunction?()
    }
}

और संग्रह दृश्य के प्रतिनिधि की इच्छा-विधि में बंद को इंजेक्ट करें:

 func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    (cell as? MyCell)?.someFunction = {
        print(indexPath) // Do something with the indexPath.
    }
}

क्लोजर एप्रोच सबसे स्वाफ्टी तरीका है जो मैंने ऐसा करने के लिए देखा है। अच्छा काम!
क्लिफ्टन लैब्रम

5

इसका काम मेरे लिए।

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     UIButton *Btn_Play = (UIButton *)[cell viewWithTag:101];
     [Btn_Play addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)ButtonClicked:(UIButton*)sender {
     CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.Tbl_Name];
     NSIndexPath *indexPath = [self.Tbl_Name indexPathForRowAtPoint:buttonPosition];
}

1
// Add action in cell for row at index path -tableView

cell.buttonName.addTarget(self, action: #selector(ViewController.btnAction(_:)), for: .touchUpInside)

// Button Action

  @objc func btnAction(_ sender: AnyObject) {



        var position: CGPoint = sender.convert(.zero, to: self.tableView)


        let indexPath = self.tableView.indexPathForRow(at: position)
        let cell: UITableViewCell = tableView.cellForRow(at: indexPath!)! as
        UITableViewCell




}


1

यदि आप तब बंद का उपयोग करके UIViewController के लिए सेल से पैरामीटर मान पारित करना चाहते हैं

//Your Cell Class
class TheCell: UITableViewCell {

    var callBackBlockWithParam: ((String) -> ()) = {_ in }

//Your Action on button
    @IBAction func didTap(_ sender: Any) {
        callBackBlockWithParam("Your Required Parameter like you can send button as sender or anything just change parameter type. Here I am passing string")
    }
}

//Your Controller
extension TheController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: TheCell.identifier, for: indexPath) as! TheCell {
            cell.callBackBlockWithParam = { (passedParamter) in 

             //you will get string value from cell class
                print(passedParamter)     
      }
            return cell
    }
}

0

@ मानी उत्तर अच्छा है, हालांकि सेल के contentView के अंदर विचारों के टैग अक्सर अन्य उद्देश्यों के लिए उपयोग किए जाते हैं। आप इसके बजाय सेल के टैग का उपयोग कर सकते हैं (या सेल का contentView टैग):

1) अपने cellForRowAtIndexPath:तरीके में, सेल के टैग को इंडेक्स के रूप में असाइन करें:

cell.tag = indexPath.row; // or cell.contentView.tag...

2) नीचे दिए गए अनुसार अपने बटन के लिए लक्ष्य और क्रिया जोड़ें:

[cell.yourbutton addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

3) विधि बनाएँ जो प्रेषक की पंक्ति लौटाती है (धन्यवाद @ सेन्टेरियो फरेरा):

- (NSInteger)rowOfSender:(id)sender
{
    UIView *superView = sender.superview;
    while (superView) {
        if ([superView isKindOfClass:[UITableViewCell class]])
            break;
        else
            superView = superView.superview;
    }

    return superView.tag;
}

4) सूचकांक के आधार पर कोड क्रियाएँ:

-(void)yourButtonClicked:(UIButton*)sender
{
     NSInteger index = [self rowOfSender:sender];
     // Your code here
}

0

CustomTableCell.h एक UITableViewCell है:

@property (weak, nonatomic) IBOutlet UIButton *action1Button;
@property (weak, nonatomic) IBOutlet UIButton *action2Button;

आयात के बाद MyVC pm:

@interface MYTapGestureRecognizer : UITapGestureRecognizer
@property (nonatomic) NSInteger dataint;
@end

MyVC.m में "cellForRowAtIndexPath" के अंदर:

//CustomTableCell 
CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

//Set title buttons
[cell.action1Button setTitle:[NSString stringWithString:NSLocalizedString(@"action1", nil)] forState:UIControlStateNormal];
[cell.action2Button setTitle:[NSString stringWithString:NSLocalizedString(@"action2", nil)] forState:UIControlStateNormal];

//Set visibility buttons
[cell.action1Button setHidden:FALSE];
[cell.action2Button setHidden:FALSE];

//Do 1 action
[cell.action1Button addTarget:self action:@selector(do1Action :) forControlEvents:UIControlEventTouchUpInside];

//Do 2 action
MYTapGestureRecognizer *action2Tap = [[MYTapGestureRecognizer alloc] initWithTarget:self action:@selector(do2Action :)];
cancelTap.numberOfTapsRequired = 1;
cancelTap.dataint = indexPath.row;
[cell.action2Button setUserInteractionEnabled:YES];
[cell.action2Button addGestureRecognizer:action2Tap];

MyVC.m:

-(void)do1Action :(id)sender{
//do some action that is not necessary fr data
}

-(void)do2Action :(UITapGestureRecognizer *)tapRecognizer{
MYTapGestureRecognizer *tap = (MYTapGestureRecognizer *)tapRecognizer;
numberTag = tap.dataint;
FriendRequest *fr = [_list objectAtIndex:numberTag];

//connect with a WS o do some action with fr data

//actualize list in tableView
 [self.myTableView reloadData];
}

-1
cell.show.tag=indexPath.row;
     [cell.show addTarget:self action:@selector(showdata:) forControlEvents:UIControlEventTouchUpInside];

-(IBAction)showdata:(id)sender
{
    UIButton *button = (UIButton *)sender;

    UIStoryboard *storyBoard;
    storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    SecondViewController *detailView = [storyBoard instantiateViewControllerWithIdentifier:@"SecondViewController"];

    detailView.string=[NSString stringWithFormat:@"%@",[_array objectAtIndex:button.tag]];

    [self presentViewController:detailView animated:YES completion:nil];

}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.