目前,我正在项目https://github.com/EddyBorja/MLPAutoCompleteTextField中使用此第三方库。该库用于根据用户输入显示建议列表。
我确实设置了像这样的文本字段
self.searchTextField = [[MLPAutoCompleteTextField alloc] initWithFrame:CGRectMake(0, 0, 250, 30)];
[self.searchTextField setBorderStyle:UITextBorderStyleRoundedRect];
self.searchTextField.backgroundColor = [UIColor whiteColor];
self.searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
self.searchTextField.textColor = [UIColor blackColor];
self.searchTextField.returnKeyType = UIReturnKeyDone;
self.searchTextField.placeholder = @"Enter name to search";
self.searchTextField.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchTextField.delegate = self;
self.searchTextField.autoCompleteDelegate = self;
self.searchTextField.autoCompleteDataSource = self;
实施数据源协议,如下所示
- (NSArray *)autoCompleteTextField:(MLPAutoCompleteTextField *)textField possibleCompletionsForString:(NSString *)string {
return @[@"AAA", @"BBB", @"CCC", @"DDD"];
当我在文本字段中输入内容时,显示了下拉列表,但是当我点击下拉列表中的单元格时,列表消失了而没有在文本字段中填写任何单词。
任何遇到此问题的人请向我解释我错在哪里。提前谢谢。
答案 0 :(得分:1)
我曾经有过同样的问题。我碰巧在UITableView中使用MLPAutocomleteTextField,因此所有触摸事件都可能被tableview拦截。
我通过将MLPAutocomleteTextField实例的autoCompleteTableAppearsAsKeyboardAccessory设置为TRUE来解决此问题,以启用启用自动完成作为键盘附件。这允许我选择我的自动完成选项。希望这也解决了你的问题。 :)
答案 1 :(得分:1)
我通过继承我的UITableViewCell来解决它,有点棘手,但它为我工作:
#import "AutoCompleteTableViewCell.h"
@interface AutoCompleteTableViewCell()
@property (nonatomic, assign) BOOL selectedCell;
@end
@implementation AutoCompleteTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
self.selectedCell = NO;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
UIView* hitView = [super hitTest:point withEvent:event];
if (hitView != nil)
{
[self.superview bringSubviewToFront:self];
}
return hitView;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
CGRect rect = self.bounds;
BOOL isInside = CGRectContainsPoint(rect, point);
if(!isInside)
{
for (UIView *view in self.subviews)
{
isInside = CGRectContainsPoint(view.frame, point);
if(isInside)
break;
}
}
if (!self.selectedCell) {
self.selectedCell = YES;
id view = [self superview];
while (view && [view isKindOfClass:[UITableView class]] == NO) {
view = [view superview];
}
UITableView *tableView = (UITableView *)view;
NSIndexPath *indexPath = [tableView indexPathForCell:self];
[tableView.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
}
return isInside;
}
然后调用委托方法:
- (void)autoCompleteTextField:(MLPAutoCompleteTextField *)textField
didSelectAutoCompleteString:(NSString *)selectedString
withAutoCompleteObject:(id<MLPAutoCompletionObject>)selectedObject
forRowAtIndexPath:(NSIndexPath *)indexPath;