objective-c从自定义单元格访问方法 [英] objective-c accessing methods from custom cell

查看:109
本文介绍了objective-c从自定义单元格访问方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这可能是一个新手问题,但我需要帮助..
我有一个someview.m,并在其中定制单元格,在customCell.h和.m
中定义所以在someview.mi有

   - (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] ;
}
返回单元格;
}

我也有一个方法

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

现在自定义单元格工作正常,但我需要访问方法printStuff来自

   - (BOOL)textFieldShouldReturn:(UITextField *)textField 

这是在customCell.m
i尝试了类似 [[self super] printStuff] 之类的东西,但是我总是收到错误...
我希望我能正确解释问题

解决方案

如果textField在你的自定义单元格,您也可以在 customCell.m 中处理textField ...事件。



如果你这样做所以,您只需使用 [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];

返回YES;
}

或者如果printStuff方法在你的tableView类中,你可以声明一个协议

  // CustomCell.h 
@protocol CustomCellProtocol< NSObject>

- (void)printStuff:(NSString *)stuff;

@end

@interface CustomCell UITableViewCell< UITextFieldDelegate>

@property(非原子,赋值)UIViewController< CustomCellProtocol> *父母;

// 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; //或使用自定义setter方法
}
返回单元格;
}


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

解决方案

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;
}

这篇关于objective-c从自定义单元格访问方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆