覆盖方法语法ios

mik*_*kez 3 iphone objective-c ios

我试图像我这样覆盖我的cellForRowAtIndexPath方法中的方法

cell.customSwitch {
    - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
    {
        [super touchesEnded:touches withEvent:event];
        NSLog(@"customSwitch touchesended");
    }

};
Run Code Online (Sandbox Code Playgroud)

但这不起作用(我通常是一个Java人:P).任何帮助将非常感激!

Cal*_*leb 7

Objective-C和Java之间有很多相似之处,但那不是它们之一.;-)

如果要使用自定义-touchesEnded:withEvent:方法创建单元格,则需要单独声明和定义该类.就像是:

@interface MyCell : UITableViewCell
{
//...
}
@end

@implementation MyCell
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
    [super touchesEnded:touches withEvent:event];
    NSLog(@"customSwitch touchesended");
}
@end
Run Code Online (Sandbox Code Playgroud)

完成后,您可以使用MyCell -cellForRowAtIndexPath:.

  • 怎么样:你使用标准的目标 - 动作机制,当交换机切换时,交换机向你的视图控制器发送一条消息,并且控制器在NSUserDefaults中设置适当的布尔值.如果你在标准UI控件类中重写方法,那么无论你实际在做什么,你都可能做错了.处理控件的标准机制是目标操作,所以只要使用它,除非你有令人信服的理由不这样做. (2认同)