4

わかりました、これはおそらく初心者の質問ですが、私はそれについて助けが必要です.. someview.m があり、その中に customCell.h と .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]にあります

4

4 に答える 4

2

textField がカスタム セルにある場合は、textField... イベントも処理できcustomCell.mます。

そうすれば、単に[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;
}
于 2013-05-16T10:32:08.913 に答える
1

customCell.hlikeで1つの変数を取る

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

現在、次の方法で

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

これが役に立てば幸いです。質問がある場合はお知らせください。

于 2013-05-16T10:48:26.830 に答える
0

デリゲートを使用する必要があります。カスタム セルの init メソッドは someview のデリゲートを受け取ります。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;
}

// この呼び出しを使用できるようになりました [target printStuff];

someView.m の cellForRow メソッドで、この init メソッドを呼び出してセルを初期化します。

cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier withtarget:self ];
于 2013-05-16T10:33:19.860 に答える
0

あなたのUITableViewグローバルを作る

そして、-(BOOL)textFieldShouldReturn:(UITextField *)textField

次のようなものを追加します。

UITableViewCell *cell = [tableView cellForRowAtIndexPath:TheIndexPath];
[cell printStuff];
于 2013-05-16T10:42:05.847 に答える