iphone - objective-c 从自定义单元格访问方法

标签 iphone ios custom-cell

好的,这可能是一个新手问题,但我需要帮助.. 我有一个 someview.m,其中有一个在 customCell.h 和 .m 中定义的自定义单元格 所以在 someview.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"];
}
return cell;
}

我也有办法

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

现在自定义单元格工作正常,但我需要从

访问方法 printStuff
- (BOOL)textFieldShouldReturn:(UITextField *)textField

在 customCell.m 中 我已经尝试过像 [[self super] printStuff] 这样的东西,但我总是得到一个错误... 我希望我能正确解释问题

最佳答案

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

如果你这样做,你可以简单地用 [self printStuff];<​​

中调用方法

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

或者如果 printStuff 方法在你的 tableView 类中,你可以声明一个协议(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;
}

关于iphone - objective-c 从自定义单元格访问方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16584930/

相关文章:

iphone - iPhone/iPad通用应用程序如何正确添加这三个文件?"Default.png, Default-568h@2x.png, Default@2x.png"?

ios - iOS 10 之前带有 OAEP 填充 sha256 的 objective-c RSA

ios - UITableView 与 SKSTableViewCell 选择问题

ios - 添加图像时静态 TableView 崩溃

ios - 如何在 UITableView 中显示 2 个自定义单元格

ios - 在 Webview 的末尾,显示一条下划线。如何去除 IOS 8 beta 中的下划线?

iphone - 如何在后台运行 iOS4 应用程序?

ios - 在 tableView 单元格上播放视频的最佳做法是什么?

iphone - 如何检查用户是否清除了iPhone后台应用程序的运行状态?

ios - 当文本字段在 uitableview 的单元格中时,不调用 touchesBegan