好的,这可能是一个新手问题,但我需要帮助.. 我有一个 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/