objective-c accessing methods from custom cell

后端 未结 4 1301
广开言路
广开言路 2021-01-19 23:56

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

4条回答
  •  滥情空心
    2021-01-20 00:36

    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 
    {
        //...
    }
    
    -(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 
    
    -(void)printStuff:(NSString *)stuff;
    
    @end
    
    @interface CustomCell UITableViewCell 
    
    @property (nonatomic, assign)UIViewController *parent;
    
    // CustomCell.m
    -(void)printStuff:(NSString *)stuff
    {
        [parent printStuff:stuff];
    }
    
    
    // TableViewClass.h
    ...
    @interface TableViewClass : UITableViewController
    
    
    // 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;
    }
    

提交回复
热议问题