2013-05-16 42 views
4

好的,這可能是一個新手問題,但我需要幫助它.. 我有一個someview.m,並在其中定製的自定義單元格customCell.h和.m 所以在someview.mi有objective-c從自定義單元格訪問方法

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

這是customCell.m 我已經嘗試的東西一樣[[self super] printStuff]但我總是得到一個錯誤...... 我希望我正確地說明了問題

+1

建議:在編寫代碼時請遵循camelCasing。 'customCell * cell'應該是'CustomCell * cell' 看起來很專業:) –

回答

2

如果文本框是你的自定義單元格,可以處理在中的textField ...事件也是如此。

,如果你這樣做,你可以

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

調用梅索德只需用[self printStuff];或者如果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; 
} 
+0

printStuff方法必須在someview.m中(顯然它不是一個simeple print-out方法) – user2165933

+0

btw,協議解決方案是一個父獨立一個,所以你只知道,你的父母迴應這種方法,你可以在沒有任何警告的情況下調用它。實際上,如果你將使用的單元不僅僅是這個類;) – geo

+0

謝謝,協議解決方案做到了。 – user2165933

0

您需要使用委託。自定義單元格init方法將採用某個視圖的委託。在CustomCell中爲textFeild設置此代理。一些鏈接此 在CustomCell.h有一類變量

{ 
UIViewController *target; 
} 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withtarget:(UIViewController *)_target; 

在CustomCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withtarget:(UIViewController *)_target 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
     target = _target; 
} 

//現在你可以使用這個調用[目標printStuff]

在someView.m中,cellForRow方法調用此init方法初始化單元格。

cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier withtarget:self ]; 
+0

當我嘗試[目標printStuff]我得到「使用未聲明的標識符'目標';你的意思是_target?」如果我把[_target printStuff]我得到「實例變量_target是私人的」... – user2165933

+0

現在我得到「接收器類型'UIViewController'例如消息不會聲明選擇器'printStuff'的方法當我嘗試[目標printStuff] – user2165933

+0

將UIViewController更改爲SomeView並導入SomView.h – Durgaprasad

0

讓您UITableView全球

而且在-(BOOL)textFieldShouldReturn:(UITextField *)textField

添加類似:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:TheIndexPath]; 
[cell printStuff]; 
1

以1個變量customCell.h

@property (nonatomic,strong) UIView *parent; //Assuming someview is UIView, if it is UIViewController than change UIView to id 
01現在

在下面的方法

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

現在

-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [_parent printStuff]; //Call like this. 
    return YES; 
} 

希望這會有所幫助,讓我知道的任何查詢的情況。