如圖所示钞诡,為了省事,我將多個textFiled放到了一個cell里,可是隨之而來的問題出現(xiàn)了荧降,在獲取textFiled.text時竟然為空=芋铩!朵诫!為此困擾了我好長時間辛友,終于在小伙伴和度娘的幫助下渡過難關(guān)。好了剪返,不說廢話废累,直接上代碼:
首先在cell.h中我定義了一個block用來將textFiled.text傳遞出去
#import <UIKit/UIKit.h>
@interface HXHXWantfaqiCell2 : UITableViewCell
@property (weak, nonatomic) IBOutlet UITextField *userNameTextfiled;// 姓名
@property (weak, nonatomic) IBOutlet UITextField *userEmailTextfiled;// 郵箱
@property (weak, nonatomic) IBOutlet UITextField *userPhoneTextfiled;// 電話
@property (weak, nonatomic) IBOutlet UITextField *userCityTextfiled;// 城市
@property (copy,nonatomic) void (^submitApplication)();// 傳遞提交按鈕活動的block
// 重點:這個就是傳遞textFiled.text的block
@property (copy,nonatomic) void (^textFiledblock)(NSString *str,NSUInteger i);
@end
cell.m中
#import "HXHXWantfaqiCell2.h"
@implementation HXHXWantfaqiCell2
// 提交信息
- (IBAction)btnAction:(id)sender {
if (self.submitApplication) {
self.submitApplication();
}
}
- (void)awakeFromNib {
[super awakeFromNib];
// 在此方法中給每個textFiled設(shè)定一個tag
_userNameTextfiled.tag = 101;
// 在此方法中給每個textFiled添加一個方法textFiledAction:
[_userNameTextfiled addTarget:self action:@selector(textFiledAction:) forControlEvents:(UIControlEventEditingChanged)];
_userEmailTextfiled.tag = 102;
[_userEmailTextfiled addTarget:self action:@selector(textFiledAction:) forControlEvents:(UIControlEventEditingChanged)];
_userPhoneTextfiled.tag = 103;
[_userPhoneTextfiled addTarget:self action:@selector(textFiledAction:) forControlEvents:(UIControlEventEditingChanged)];
_userCityTextfiled.tag = 104;
[_userCityTextfiled addTarget:self action:@selector(textFiledAction:) forControlEvents:(UIControlEventEditingChanged)];
}
// 執(zhí)行此方法
- (void)textFiledAction:(UITextField *)textfile{
self.textFiledblock(textfile.text,textfile.tag);
NSLog(@"%ld-%@",textfile.tag,textfile.text
);
}
@end
在HXWantfaqiVC中引入
#import "HXHXWantfaqiCell2.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 在此方法中對應(yīng)的cell里寫如下代碼:
HXHXWantfaqiCell2 *cell = [tableView dequeueReusableCellWithIdentifier:hxCellId2 forIndexPath:indexPath];
// 執(zhí)行傳遞textFiled.text的block方法
cell.textFiledblock = ^(NSString *st,NSUInteger tag){
// 根據(jù)tag值給相應(yīng)的NSString字符串賦值
if (tag == 101) {
_name = st;
}if (tag == 102) {
_email = st;
}if (tag == 103) {
_phone = st;
}if (tag == 104) {
_city = st;
}
};
// 提交信息按鈕執(zhí)行方法的block
cell.submitApplication = ^{
// 打印驗證結(jié)果
NSLog(@"_name = %@\n_email = %@\n_phone = %@\n_city = %@",_name,_email,_phone,_city);
};
return cell;
}