I have custom UITableViewCell (dynamically created) for Billing address and Shipping address .I want to get UITextField value .How I can get its value without using tag property.Following is screen of my UITableViewCell-
cellForRowAtIndexPath code is as-
- (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];
}
Custom Cell class is as-
#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
You can track your value inside TextField's delegate textFieldDidEndEditing method :
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 .
}
}
您可以使用KVO将textfield.text绑定到模型数据。
Create Cell Model class corresponding to each cell -
@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
In textfield Delegate
-(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;
}
Do this in all tableView cell which you have created.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.