2017-04-25 49 views
0

我爲定單地址和發貨地址定製了UITableViewCell(動態創建)。我想獲得UITextField的值。如何在不使用標籤屬性的情況下獲取它的值。以下是我的UITableViewCell - 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]; 
} 

定製Cell類是原樣

#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 tableview委託方法更新你的問題 –

+0

爲什麼你不想要標籤Property?這將很容易獲取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細胞執行此操作。

相關問題