में के बाद से आप कस्टम सेल का उपयोग कर रहे हैं, मुझे लगता है कि आपको चयन को संभालने की भी आवश्यकता है, क्योंकि आप कस्टम सेल के अंदर बटन को छू रहे हैं, इसलिए सेल स्वयं ही टेबलव्यू प्रतिनिधि विधियों को निकाल नहीं दिया गया है, जैसा कि मैंने आपके कस्टम सेल में कहा है, के लिए एक प्रतिनिधि विधि आपके कस्टम कोशिकाओं में उदाहरण
अपने CellTypeOne.h
में जोड़ने के इस
//@class CellTypeOne; //if u want t pass cell to controller
@protocol TouchDelegateForCell1 <NSObject> //this delegate is fired each time you clicked the cell
- (void)touchedTheCell:(UIButton *)button;
//- (void) touchedTheCell:(CellTypeOne *)cell; //if u want t send entire cell this may give error add `@class CellTypeOne;` at the beginning
@end
@interface CellTypeOne : UITableViewCell
{
}
@property(nonatomic, assign)id<TouchDelegateForCell1> delegate; //defining the delegate
- (IBAction)actionForFirstButton:(id)sender;
- (IBAction)actionForSecondButton:(id)sender;
- (IBAction)actionForThirdButton:(id)sender;
- (IBAction)actionForFourthButton:(id)sender;
अपने
CellTypeOne.m
फ़ाइल
@synthesize delegate; //synthesize the delegate
- (IBAction)actionForFirstButton:(UIButton *)sender
{
//add this condition to all the actions becz u need to get the index path of tapped cell contains the button
if([self.delegate respondsToSelector:@selector(touchedTheCell:)])
{
[self.delegate touchedTheCell:sender];
//or u can send the whole cell itself
//for example for passing the cell itself
//[self.delegate touchedTheCell:self]; //while at the defining the delegate u must change the sender type to - (void)touchedTheCell:(CellTypeOne *)myCell; if it shows any error in the defining of the delegate add "@class CellTypeOne;" above the defying the delegate
}
}
और अपने ViewContainingMyTableView.h
@interface ViewContainingMyTableView : UIViewController <UITableViewDelegate, UITableViewDataSource ,TouchDelegateForCell1> //confirms to custom delegate like table delegates
{
UITableView *myTBV;
}
@property (retain, nonatomic) IBOutlet UITableView *myTBV;
@end
में और ViewContainingMyTableView.m
फ़ाइल में में
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//during the creating the custom cell
CellTypeOne *cell1 = [self.aTableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell1 == nil)
{
cell1 = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.delegate = self; //should set the delegate to self otherwise delegate methods does not called this step is important
}
//now implement the delegate method , in this method u can get the indexpath of selected cell
- (void)touchedTheCell:(UIButton *)button
{
NSIndexPath *indexPath = [self.aTableView indexPathForCell:(UITableViewCell *)button.superview];
NSLog(@"%@",indexPath.description);
}
/* if u pass the cell itself then the delegate method would be like below
- (void)touchedTheCell:(CellTypeOne *)myCell
{
NSIndexPath *indexPath = [self.aTableView indexPathForCell:myCell];//directly get the cell's index path
//now by using the tag or properties, whatever u can access the contents of the cell
UIButton *myButton = [myCell.contentView viewWithTag:1000]; //get the button
//... u can access all the contents in cell
}
*/
अपने मामले में
, इस सेल में पहला बटन के लिए है, सेल में एक और बटन के लिए, विभिन्न कार्यों होने प्रत्येक बटन के लिए प्रतिनिधि विधियों को जोड़ने की प्रक्रिया से ऊपर दोहराने
आशा यू इस मिल :) hapy कोडिंग
आप बटन पर क्लिक करने पर ही सेल के indexpath की ज़रूरत है? –
हां, मुझे उस सेल के इंडेक्सपाथ की आवश्यकता है जिसमें क्लिक किए गए बटन हों। –
@ Erzékiel बस कस्टम सेल में एक प्रतिनिधि डालते हैं और जब भी आप कस्टम सेल में बटन पर क्लिक करते हैं तो वहां नियंत्रक को देखने के लिए प्रतिनिधि विधि को ट्रिगर करते हैं, आप पर्यवेक्षक को अपने कस्टम सेल के रूप में प्राप्त कर सकते हैं और आपको चयनित सेल –