4

ok, this is maybe a newbie question but i need help with it.. I have a someview.m and in it a custom cell which is defined in customCell.h and .m So in someview.m i have

- (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");
}

Now the custom cells are working fine, but i need to access the method printStuff from

- (BOOL)textFieldShouldReturn:(UITextField *)textField

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

  • 1
    Suggestion: Please follow camelCasing while writing code. `customCell *cell` should be `CustomCell *cell` Looks professional :) – Abhishek Bedi May 16 '13 at 11:00

4 Answers4

2

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

if you do so, you can call the methode 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 methode is in you 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;
}
geo
  • 1,781
  • 1
  • 18
  • 30
  • The printStuff method have to be in someview.m (obviously its not a simeple print-out method) – user2165933 May 16 '13 at 10:49
  • btw, the protocol solution is a parent independent one, so you just know, that your parent responds to this methode and you can call it without any warnings. practicaly if you will use the cell in more than only this class ;) – geo May 16 '13 at 11:23
  • Thanks, the protocol solution did it. – user2165933 May 16 '13 at 11:50
  • You should declare a parent property with assign attribute (or weak with ARC) or you will have a memory cycle between tableViewController and your customCell. – Dmitry Zhukov May 16 '13 at 11:58
1

Take 1 variable in customCell.h like

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

now in following 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 query.

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
0

You need to use delegate. Custom cell init method will take delegate of someview. To textFeild in CustomCell set this delegate. Something link this In CustomCell.h have 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;
}

// You can now use this call [target printStuff];

In someView.m cellForRow method call this init method to initialise cell.

cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier withtarget:self ];
Durgaprasad
  • 1,910
  • 2
  • 25
  • 44
  • When i try [target printStuff] i get "Use of undeclared identifier 'target'; did you mean _target?" if i put [_target printStuff] i get "Instance variable _target is private"... – user2165933 May 16 '13 at 10:48
  • Now i get " Receiver type 'UIViewController' for instance message does not declare a method with selector 'printStuff' when i try [target printStuff] – user2165933 May 16 '13 at 11:31
  • Change UIViewController to SomeView and import SomView.h – Durgaprasad May 16 '13 at 11:34
0

Make your UITableView global

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

add something like:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:TheIndexPath];
[cell printStuff];
Joshua
  • 2,432
  • 1
  • 20
  • 29