好吧,这可能是一个新手问题,但我需要帮助。
我有一个 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 在您的自定义单元格中,您可以处理 textField... 中的事件customCell.m
too.
如果这样做,您可以简单地调用该方法[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;
}
或者如果 printStuff 方法在你的 tableView 类中,你可以声明一个协议
// 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)