将方法从我的自定义单元格 class 传递到主单元格 class

Pass a method from my custom cells class to the main class

我有一个自定义单元格。其中有一个 UITextField 链接到 customCell.m。我正在尝试将文本从 textField 传递到 mainVC.m

我在customCell.m中有一个public方法:

- (NSString *)PassText{
   return self.myTextField.text;
}

如何将该方法传递给 mainVC.m 中的字符串?

如何在 MainVC 中创建 @class CustomCell;@propety (strong, nonatomic) CustomCell *customCell。然后你可以通过 self.customCell.textField.text.

访问它

迈克, 数据传递的正常模式是使用属性和委托。如果要将信息传回视图控制器,请使用委托。当您说自定义单元格时,我不确定您是在谈论 UItableview 单元格还是集合单元格。 uitableview 和 collection view 都有很多方法可以将信息传回视图控制器。

您需要做的是:

  1. 定义一个protocol
  2. 为您的 CustomCell
  3. 添加此协议的 属性
  4. 在您的 MainVC
  5. 中实施协议

1.定义协议

我们通常将协议的定义放在头文件中(在本例中为CustomCell.h)。

 // define the protocol for the delegate
 @protocol CustomCellDelegate 
 // define protocol functions that can be used in any class using this delegate
 -(void)customCell:(CustomCell *)customCell passText:(NSString *)text;
 @end

2。为您的 CustomCell

添加此协议的 属性

并将此添加到 @interface CustomCell@end 之间的 CustomCell

@property (nonatomic, weak) id<CustomCellDelegate> delegate; 

3。在 MainVC

中实施协议

在您的 MainVC 中,按如下方式实现委托函数。

@interface MainCV<CustomCellDelegate>
@end

@implementation MainVC 
-(void)customCell:(CustomCell *)customCell passText:(NSString *)text
{
     // Do whatever you want with text here
}
@end

下面展示了你如何使用上面的协议。

  1. MainCV 中创建 CustomCell 时设置委托。像下面这样的东西,

    CustomCell *cell = ... allocation and initialization
    cell.delegate = self; // self is mainVC
    
  2. 每当您需要在 customCell 中传递数据 NSString 时,请调用以下命令:

    [self.delegate customCell:self passText:self.myTextField.text]; // self is customCell
    

有一种更简单的方法可以做到这一点。

这是假设您全部以编程方式执行此操作,并且您的自定义单元格已正确设置且 textField 正常工作。

首先,您需要使您的 textField 可以从自定义单元格外部访问。您以正常方式将其作为 属性:

放入头文件中

customCell.h

@property (weak, nonatomic) IBOutlet UITextField * customTextField;

确保在自定义单元格 XIB 中分配它。

现在,当我们访问主视图控制器的 cellForRowAtIndex 中的单元格时,我们有一个指向自定义文本字段的指针:

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

    //Adding the cell - remember to add the identifier to the xib file
    BCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:bCustomCellIdentifier];

    // We can now access our cell's textField
    cell.textLabel.text = cell.customTextField.text;

    return cell;
}

希望这对您有所帮助。

根据我的理解,我认为您正在寻找这种方法,如果我在这里错了,请告诉我。

在索引路径的 select 行中,首先您需要找到您的自定义单元格,然后找到您需要的文本字段。像这样:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    customCell* cell=(customCell*)[tableView cellForRowAtIndexPath:indexPath.Row];
    NSString* yourTextFieldText=cell.textField.text;
    mainVC* objmainVC=[mainVC alloc]init];
    objmainVC.yourTextFieldText=yourTextFieldText;
    [self.navigationController pushViewController:objmainVC animated:YES];

}

现在您也可以将您的 TextFieldText 用于 mainVC 控制器。

是的,你可以在代表的帮助下做到这一点,例如,

在自定义单元格中,我使用了一个按钮和文本字段,并定义了如下协议

//in CustomCell.h file
@class CustomCell;
@protocol CellDelegate <NSObject> //protocol defination
- (void)whenDoneButtonClicked:(CustomCell *)cell; //i am passing the cell this would be the good to get whole information of a cell for example apart from properties u can get index path ..
@end


@interface CustomCell : UITableViewCell <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *aButton;
@property (weak, nonatomic) IBOutlet UITextField *aTextField;

@property (nonatomic, assign) id<CellDelegate> delegate; //declare a delegate 

@end

在 CustomCell.m 文件中你可以像下面那样做

// this is the button action method in the custom cell, when the 
// button tapped, this method is called in this method u are 
// calling the delegate method which is defined in the controller.
- (IBAction)buttonAction:(id)sender
 {
    //as in your case user enter the text in textfield and taps button
     if([self.delegate respondsToSelector:@selector(whenDoneButtonClicked:)]) //checking weather it is safe to call the delegate method, it is not need but some times it is necessary to check to avoid crashes
          [self.delegate whenDoneButtonClicked:self]; //pass the cell or if u want text then u can also pass text also by adding 2 or more parameters
 }  

// in the above method u are calling the delegate method by passing
// the cell (hear the self means -> current object -> nothing but cell)  
// u are calling the delegate method defined in the controller by
// passing the  "self" nothing but cell ...  

在 maninVc 中执行以下操作

在 viewcontroller.h 文件中

#import "CustomCell.h"
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,CellDelegate> //confirms to delegate 

@property (nonatomic, retain) IBOutlet UITableView *tableView;
//.....  rest of the properties and method 

@end

并在 viewcontroller.m 文件中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
     if(cell == nil)
     {
        cell = [CustomCell cell]; //initilization if it is nil 

     } 

    cell.delegate = self; //set the delegate to self 
    //...other code 
    return cell;
 } 


 //define the delegate method

 // when u are calling this method from customCell class by passing 
 // the cell->(self) hear u get the cell        
 - (void)whenDoneButtonClicked:(CustomCell *)cell
 {
    //hear u will get a cell
     NSLog(@"text is:%@",cell.aTextField.text); 
 }

希望这对你有帮助.. :)