Objective-c access to methods from a custom cell

OK, this may be a newbie question, but I need help. I have someview.m, and it creates a custom cell, which is defined in customCell.h and .m So in someview.m I

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { customCell *cell=[tableView dequeueReusableCellWithIdentifier:@"charCell"]; if (cell == nil || (![cell isKindOfClass: customCell.class])) { cell=[[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"charCell"]; } return cell; } 

I have a method too

 -(void) printStuff { NSLog(@"stuff"); } 

Custom cells are working fine now, but I need to access the printStuff method from

 - (BOOL)textFieldShouldReturn:(UITextField *)textField 

which is in customCell.m I tried things like [[self super] printStuff] , but I always get an error message ... I hope I correctly explained the problem.

+4
source share
4 answers

if textField is in your custom cell, you can handle textField ... events in customCell.m too.

if you do, you can call the method simply with [self printStuff]; in

- (BOOL)textFieldShouldReturn:(UITextField *)textField

 //CustomCell.h // ... @interface CustomCell : UITableViewCell <UITextFieldDelegate> { //... } -(void)printStuff; @end //CustomCell.m //... -(void)printStuff { //... } -(BOOL)textFieldShouldReturn:(UITextField *)textField { //... [textField resignFirstResponder]; [self printStuff]; return YES; } 

or if the printStuff method is in your tableView class, you can declare a protocol

 // CustomCell.h @protocol CustomCellProtocol <NSObject> -(void)printStuff:(NSString *)stuff; @end @interface CustomCell UITableViewCell <UITextFieldDelegate> @property (nonatomic, assign)UIViewController<CustomCellProtocol> *parent; // CustomCell.m -(void)printStuff:(NSString *)stuff { [parent printStuff:stuff]; } // TableViewClass.h ... @interface TableViewClass : UITableViewController<CustomCellProtocol> // TableViewClass.m - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { customCell *cell=[tableView dequeueReusableCellWithIdentifier:@"charCell"]; if (cell == nil || (![cell isKindOfClass: customCell.class])) { cell=[[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"charCell"]; cell.parent = self; // or with a custom setter methode } return cell; } 
+2
source

Take 1 variable in customCell.h as

 @property (nonatomic,strong) UIView *parent; //Assuming someview is UIView, if it is UIViewController than change UIView to id 

now in the next method

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { customCell *cell=[tableView dequeueReusableCellWithIdentifier:@"charCell"]; if (cell == nil || (![cell isKindOfClass: customCell.class])) { cell=[[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"charCell"]; } cell.parent = self; return cell; } 

now in

 -(BOOL)textFieldShouldReturn:(UITextField *)textField { [_parent printStuff]; //Call like this. return YES; } 

Hope this helps, let me know in case of any queries.

+1
source

You need to use a delegate. A custom cell initialization method will be accepted by a delegate of some kind. For textFeild in CustomCell, set this delegate. Something to bind this In CustomCell.h there is a class variable

 { UIViewController *target; } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withtarget:(UIViewController *)_target; 

In CustomCell.m

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withtarget:(UIViewController *)_target { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code target = _target; } 

// Now you can use this call [target printStuff];

The someView.m method cellForRow calls this init method to initialize the cell.

 cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier withtarget:self ]; 
0
source

Make Your UITableView Global

And in -(BOOL)textFieldShouldReturn:(UITextField *)textField

add something like:

 UITableViewCell *cell = [tableView cellForRowAtIndexPath:TheIndexPath]; [cell printStuff]; 
0
source

Source: https://habr.com/ru/post/1481146/


All Articles