查找好多了资料,对这个如果将自己定义的cell中的textfield实现,失去焦点触发键盘隐藏的功能,我这里简单的贴出自己的代码

第一部分是我的自定义的cell

nameCell.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//
// nameCell.h
// xunYi6
//
// Created by david on 13-5-20.
// Copyright (c) 2013年 david. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface nameCell : UITableViewCell<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *markPic;
@property (strong, nonatomic) IBOutlet UILabel *teleplayName;
@property (strong, nonatomic) IBOutlet UITextField *teleplayInput;

@property (copy, nonatomic) UIImage *teleplayImage;
@property (copy, nonatomic) NSString *teleplayTitle;
@property (copy, nonatomic) NSString *name;

@end
nameCell.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// nameCell.m
// xunYi6
//
// Created by david on 13-5-20.
// Copyright (c) 2013年 david. All rights reserved.
//

#import "nameCell.h"

@implementation nameCell

@synthesize teleplayImage = _teleplayImage;
@synthesize teleplayTitle = _teleplayTitle;
@synthesize name = _name;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.teleplayInput.delegate = self;
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

-(void) setTeleplayImage:(UIImage *)value
{
if(![value isEqual:_teleplayImage])
{
_teleplayImage = [value copy];
self.markPic.image = _teleplayImage;
}
}

-(void) setTeleplayTitle:(NSString *)value
{
if(![value isEqualToString:_teleplayTitle])
{
_teleplayTitle = [value copy];
self.teleplayName.text = _teleplayTitle;
}
}

-(void) setName:(NSString *)value
{
if(![value isEqualToString:_name])
{

_name = [value copy];
self.teleplayInput.text = _name;
}
}

@end

实现的方法我实在-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section这个方法中实现的,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataDic count];
}

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


NSUInteger row = [indexPath row];

if(row == 0)
{
static NSString *nameCellIdentifier = @"nameCell";
UINib *nib = [UINib nibWithNibName:@"nameCell" bundle:nil];
[self.publicChanceTable registerNib:nib forCellReuseIdentifier:nameCellIdentifier];

nameCell *cell = [self.publicChanceTable dequeueReusableCellWithIdentifier:nameCellIdentifier];

if(cell == nil)
{
cell = [[nameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nameCellIdentifier];
}

cell.teleplayInput.delegate = self;

cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

static NSString *cellIdentifier = @"cellIdentifier";

UITableViewCell *cell = [self.publicChanceTable dequeueReusableCellWithIdentifier:cellIdentifier];

if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

NSDictionary *dic = [self.dataDic objectForKey:[NSString stringWithFormat:@"%d",row]];
cell.textLabel.text = [dic valueForKey:@"detail"];

return cell;
}

重要的一点是我加入了这条语句

1
cell.teleplayInput.delegate = self;

同时在你的.h文件中要做的是:

1
@interface chancePublicViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>

没错要引入UITextFieldDelegate协议