2017-04-25 18 views
0

私はUITableFieldの値を取得したいと思います。 - Custom UITableViewCellカスタムUItableViewCellからUITextfield値を取得する方法。 UITableViewCellに複数のUITextFieldがある場合、タグのプロパティを使用せずに

cellForRowAtIndexPathコードは

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    if(isShippingShow){ 
     switch (indexPath.section){ 
      case 0: 
      { 
       CardListCell *cell = (CardListCell*)[tableView dequeueReusableCellWithIdentifier:@"CardListCell" forIndexPath:indexPath]; 
       return cell; 
      } 
       break; 
      case 1: 
      { 
       NewCardCell *cell = (NewCardCell*)[tableView dequeueReusableCellWithIdentifier:@"NewCardCell" forIndexPath:indexPath]; 
       return cell; 
      } 
       break; 
      case 2: 
      case 3: 
      { 
       PayAddressCell *cell = (PayAddressCell*)[tableView dequeueReusableCellWithIdentifier:@"PayAddressCell" forIndexPath:indexPath]; 
       cell.txtCountry.delegate = self; 
       cell.txtCountry.inputView = _pickerView; 
       [self setTextFieldAccessoryView:cell.txtCountry]; 

       return cell; 
      } 
       break; 
      case 4: 
      { 
       SettingCell *cell = (SettingCell*)[tableView dequeueReusableCellWithIdentifier:@"SettingCell" forIndexPath:indexPath]; 
       [cell.swSetting addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventValueChanged]; 

       return cell; 
      } 

       break; 
      default: 
       break; 
     } 

    } 
    else{ 
     switch (indexPath.section){ 
      case 0:{ 
       CardListCell *cell = (CardListCell*)[tableView dequeueReusableCellWithIdentifier:@"CardListCell" forIndexPath:indexPath]; 
       return cell; 
      } 
       break; 
      case 1:{ 
       NewCardCell *cell = (NewCardCell*)[tableView dequeueReusableCellWithIdentifier:@"NewCardCell" forIndexPath:indexPath]; 
       return cell; 
      } 
       break; 
      case 2: 
      { 
       PayAddressCell *cell = (PayAddressCell*)[tableView dequeueReusableCellWithIdentifier:@"PayAddressCell" forIndexPath:indexPath]; 
       cell.txtCountry.delegate = self; 
       cell.txtCountry.inputView = _pickerView; 
       [self setTextFieldAccessoryView:cell.txtCountry]; 

       return cell; 
      } 
       break; 
      case 3: 
      { 
       SettingCell *cell = (SettingCell*)[tableView dequeueReusableCellWithIdentifier:@"SettingCell" forIndexPath:indexPath]; 
       [cell.swSetting addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventValueChanged]; 

       return cell; 
      } 

       break; 
      default: 
       break; 
     } 

    } 

    return [UITableViewCell new]; 
} 

カスタムセルクラスがれたままであるれたままである

#import <UIKit/UIKit.h> 

@interface NewCardCell : UITableViewCell 


@property(nonatomic,weak) IBOutlet UITextField *txtCardHolderName; 
@property(nonatomic,weak) IBOutlet UITextField *txtCardNumber; 
@property(nonatomic,weak) IBOutlet UITextField *txtExpDate; 
@property(nonatomic,weak) IBOutlet UITextField *txtCVV; 

@end 


@interface CardListCell : UITableViewCell 
@property(nonatomic,weak) IBOutlet UILabel *lblCount; 
@property(nonatomic,weak) IBOutlet UILabel *lblCardNumber; 
@property(nonatomic,weak) IBOutlet UIImageView *imgCardIcon; 
@property(nonatomic,weak) IBOutlet UIButton *btnSelectCard; 
@end 


@interface SettingCell : UITableViewCell 
@property(nonatomic,weak) IBOutlet UILabel *lblCaption; 
@property(nonatomic,weak) IBOutlet UISwitch *swSetting; 
@end 


@interface PayAddressCell : UITableViewCell 
@property(nonatomic,weak) IBOutlet UITextField *txtAddress; 
@property(nonatomic,weak) IBOutlet UITextField *txtCity; 
@property(nonatomic,weak) IBOutlet UITextField *txtState; 
@property(nonatomic,weak) IBOutlet UITextField *txtZipcode; 
@property(nonatomic,weak) IBOutlet UITextField *txtCountry; 
@end 
+0

cellrowatindexテーブルビューデリゲートメソッド –

+0

で質問を更新してください。なぜ、タグプロパティを使用したくないのですか? uitextfieldを簡単に取得できるようになります – ajjjjjjjj

+0

Scrollviewのテキストフィールドを使用してみませんか? もっと簡単になります –

答えて

0

することができますTextFieldのデリゲート内で値を追跡するtextFieldDidEndEditingメソッド:

NSString * strAddress; // in interface

-(void)textFieldDidEndEditing:(UITextField *)textField{ 

    UITableViewCell *cell = (UITableViewCell *)textField.superview.superview; //track cell's view hierarchy 
    if (cell isKindOfClass:[PayAddressCell class]) { 
     PayAddressCell *settingCell = cell; 
     if (textField == cell.txtAddress) { 
      strAddress = textField.text; // your address textfield's value 
     } 
     // you can check all textfield as above . 
    } 
    } 
0

KVOを使用すると、textfield.textをモデルデータにバインドできます。

0

各セルに対応する細胞モデルクラスを作成します -

@interface CellModel : NSObject <NSCoding, NSCopying> 

@property (nonatomic, strong) NSString *address; 
@property (nonatomic, strong) NSString *city; 
@property (nonatomic, strong) NSString *state; 
@property (nonatomic, strong) NSString *zipcode; 
@property (nonatomic, strong) NSString *country; 

@end 

テキストフィールドでの委任

-(void)textFieldDidEndEditing:(UITextField *)textField{ 
cellModel.address = textField.text; 
// or cellModel.city = textField.text; 
// or cellModel.state = textField.text; 
// or cellModel.zipcode = textField.text; 
// or cellModel.country = textField.text; 
    } 

は、あなたが作成したすべてのtableView細胞でこれを行います。

関連する問題