我为结算地址和送货地址定制了UITableViewCell(动态创建)。我想获得UITextField值。如何在不使用tag属性的情况下获取其值。以下是我的UITableViewCell的屏幕 -
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 :(得分:0)
您可以在TextField的委托 textFieldDidEndEditing 方法中跟踪您的值:
NSString * strAddress; //在界面
-(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 .
}
}
答案 1 :(得分:0)
您可以使用KVO将textfield.text绑定到模型数据。
答案 2 :(得分: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
在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;
}
在您创建的所有tableView单元格中执行此操作。