Objective c UITextField和UIImageView重叠表视图
我有以下问题,UITextField和UIImageView与tableView重叠。如果我先选择位置,然后返回选择活动,则位置的Objective c UITextField和UIImageView重叠表视图,objective-c,uitableview,uitextfield,Objective C,Uitableview,Uitextfield,我有以下问题,UITextField和UIImageView与tableView重叠。如果我先选择位置,然后返回选择活动,则位置的UItextfield将与活动重叠tableView 代码 更改cofigureAutoComTableView方法中的Tableview框架,方法如下:autocompleteTableView=[[UITableView alloc]initWithFrame:CGRectMake(self.txtActivity.frame.origin.x,CGRectG
UItextfield
将与活动重叠tableView
代码
更改cofigureAutoComTableView方法中的Tableview框架,方法如下:autocompleteTableView=[[UITableView alloc]initWithFrame:CGRectMake(self.txtActivity.frame.origin.x,CGRectGetHeight(self.txtActivity.frame)+40,txtActivity.frame.size.width,200)style:UITableViewStylePlain];不起作用,它仍然是一样的。尝试这种方式autocompleteTableView=[[UITableView alloc]initWithFrame:CGRectMake(self.txtActivity.frame.origin.x,CGRectGetHeight(\u txtPlaceSearch.frame)+40,\u txtActivity.frame.size.width,200)style:UITableViewStylePlain]@RajeshDharani,尝试了你的最新建议,tableView出现在文本字段的后面;
#import "Search.h"
#import <QuartzCore/QuartzCore.h>
#import "AppDelegate.h"
#import <GoogleMaps/GoogleMaps.h>
#import <GooglePlaces/GooglePlaces.h>
#import "Classes.h"
#import "MyTabBarController.h"
@interface Search(){
AppDelegate *appDelegate;
NSString *sURL, *strResult, *sRemaining;
NSString *sLanguage;
}
@end
@implementation Search
@synthesize categoryMArray;
@synthesize autocompleteTableView;
@synthesize autocompleteMArray;
@synthesize sSelectedAdd;
- (void)viewDidLoad {
[super viewDidLoad];
//=== Call the stored txtMemCode, something like session ---
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
sLanguage = [defaults objectForKey:@"txtLanguage"];
//=== Pass the string to web and get the return Category result
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:@"/apps/getcat.asp?"];
self.responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:sURL]];
(void) [[NSURLConnection alloc] initWithRequest:request delegate:self];
//--- Configure the TableView
self.txtActivity.delegate = self;
UIImageView *imgforLeftActivity=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)]; // Set frame as per space required around icon
imgforLeftActivity=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)]; // Set frame as per space required around icon
[imgforLeftActivity setImage:[UIImage imageNamed:@"search.png"]];
[imgforLeftActivity setContentMode:UIViewContentModeCenter];// Set content mode centre or fit
self.txtActivity.leftView=imgforLeftActivity;
self.txtActivity.leftViewMode=UITextFieldViewModeAlways;
//==== Google Place autocomplete
_txtPlaceSearch.placeSearchDelegate = self;
_txtPlaceSearch.strApiKey = @"AIzaSyBJvZbCA-BiAE3HBgvrm6TTjAiVkYTU9Kk";
_txtPlaceSearch.superViewOfList = self.view; // View, on which Autocompletion list should be appeared.
_txtPlaceSearch.autoCompleteShouldHideOnSelection = YES;
_txtPlaceSearch.maximumNumberOfAutoCompleteRows = 5;
UIImageView *imgforLeft=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)]; // Set frame as per space required around icon
[imgforLeft setImage:[UIImage imageNamed:@"location.png"]];
[imgforLeft setContentMode:UIViewContentModeCenter];// Set content mode centre or fit
_txtPlaceSearch.leftView=imgforLeft;
_txtPlaceSearch.leftViewMode=UITextFieldViewModeAlways;
//--- Set the Navigation Bar to Transparent---
UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
navigationBarAppearance.backgroundColor = [UIColor clearColor];
[navigationBarAppearance setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
navigationBarAppearance.shadowImage = [[UIImage alloc] init];
-(void)viewWillAppear:(BOOL)animated{
//--- Call the stored txtMemCode, something like session ---
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
sLanguage = [defaults objectForKey:@"txtLanguage"];
if ([sLanguage isEqualToString:@"CN"]){
[_txtActivity setPlaceholder:@"按活动搜索"];
[_txtPlaceSearch setPlaceholder:@"搜索你所在城市的地方"];
}else{
[_txtActivity setPlaceholder:@"Search activities"];
[_txtPlaceSearch setPlaceholder:@"Location"];
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//Optional Properties for Google Place AutoComplete
_txtPlaceSearch.autoCompleteRegularFontName = @"HelveticaNeue-Bold";
_txtPlaceSearch.autoCompleteBoldFontName = @"HelveticaNeue";
_txtPlaceSearch.autoCompleteTableCornerRadius=0.0;
_txtPlaceSearch.autoCompleteRowHeight=35;
_txtPlaceSearch.autoCompleteTableCellTextColor=[UIColor colorWithWhite:0.131 alpha:1.000];
_txtPlaceSearch.autoCompleteFontSize=14;
_txtPlaceSearch.autoCompleteTableBorderWidth=1.0;
_txtPlaceSearch.showTextFieldDropShadowWhenAutoCompleteTableIsOpen=YES;
_txtPlaceSearch.autoCompleteShouldHideOnSelection=YES;
_txtPlaceSearch.autoCompleteShouldHideClosingKeyboard=YES;
_txtPlaceSearch.autoCompleteShouldSelectOnExactMatchAutomatically = YES;
_txtPlaceSearch.autoCompleteTableFrame = CGRectMake(self.txtPlaceSearch.frame.origin.x,self.txtPlaceSearch.frame.origin.y+32, _txtPlaceSearch.frame.size.width, 200.0);
[self cofigureAutoComTableView];
}
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange: (NSRange)range
replacementString:(NSString *)string{
if ([textField isEqual:_txtActivity]) {
autocompleteTableView.hidden = NO;
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring
stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
return YES;
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring{
autocompleteMArray = [[NSMutableArray alloc] init];
[autocompleteMArray removeAllObjects];
if ([substring length] > 0)
{
for(NSString *curString in categoryMArray)
{
NSRange substringRange = [curString rangeOfString:substring];
if (substringRange.length > 0)
{
[autocompleteMArray addObject:curString];
}
}
}
else
{
autocompleteTableView.hidden = YES;
}
NSLog(@"*******The autocompleteMArray : %@ ", autocompleteMArray);
[autocompleteTableView reloadData];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//NSLog(@"didReceiveResponse");
[self.responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//NSLog(@"didFailWithError");
//NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
//[spinner stopAnimating];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//NSLog(@"connectionDidFinishLoading");
//NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);
strResult = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
NSLog(@"This returned response.write stuff %@", strResult);
NSString *sSeparator= @"|";
NSRange range = [strResult rangeOfString:sSeparator];
NSInteger position = range.location + range.length;
NSString *sLoop = [strResult substringToIndex:position-1];
sRemaining = [strResult substringFromIndex:position];
categoryMArray = [[NSMutableArray alloc] init];
for (int i = 0; i< [sLoop intValue]; i++) {
range = [sRemaining rangeOfString:sSeparator];
position = range.location + range.length;
NSString *sCatName = [sRemaining substringToIndex:position-1];
sRemaining = [sRemaining substringFromIndex:position];
[categoryMArray addObject:sCatName];
}
NSLog(@"The Array : %@ ", categoryMArray);
}
-(void)cofigureAutoComTableView {
autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.txtActivity.frame.origin.x,self.txtActivity.frame.origin.y+32,_txtActivity.frame.size.width, 200) style:UITableViewStylePlain];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
autocompleteTableView.hidden = YES;
[self.view addSubview:autocompleteTableView];
CALayer *layer = autocompleteTableView.layer;
[layer setMasksToBounds:YES];
[layer setCornerRadius: 0.0];
[layer setBorderWidth:1.0];
[layer setBorderColor:[[UIColor blackColor] CGColor]];
}
- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath {
UIFont *myFont = [ UIFont fontWithName: @"HelveticaNeue" size: 14.0 ];
cell.textLabel.font = myFont;
cell.backgroundColor = [UIColor whiteColor];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 35.0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return autocompleteMArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [self.autocompleteTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [autocompleteMArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSLog(@"Selected Cell %@", [autocompleteMArray objectAtIndex:indexPath.row]);
autocompleteTableView.hidden = YES;
_txtActivity.text = [autocompleteMArray objectAtIndex:indexPath.row];
}
#pragma mark - Place search Textfield Delegates
-(void)placeSearch:(MVPlaceSearchTextField*)textField ResponseForSelectedPlace:(GMSPlace*)responseDict{
[self.view endEditing:YES];
sSelectedLat = [NSString stringWithFormat: @"%f", responseDict.coordinate.latitude];
sSelectedLong = [NSString stringWithFormat: @"%f", responseDict.coordinate.longitude];
//NSLog(@"SELECTED ADDRESS :%@",responseDict);
}
-(void)placeSearch:(MVPlaceSearchTextField*)textField ResultCell:(UITableViewCell*)cell withPlaceObject:(PlaceObject*)placeObject atIndex:(NSInteger)index{
cell.contentView.backgroundColor = [UIColor whiteColor];
}
-(BOOL)textFieldShouldClear:(UITextField *)textField {
if ([textField isEqual:_txtActivity]) {
autocompleteTableView.hidden = YES;
return YES;
}
return YES;
}
- (IBAction)dissmissKeyboardOnTap:(id)sender{
autocompleteTableView.hidden = YES;
[[self view]endEditing:YES];
}
-(void)viewDidLayoutSubviews{
//=== Customize the UITextField
[self SetTextFieldBorder:_txtPlaceSearch];
[self SetTextFieldBorder:_txtActivity];
}
-(void)SetTextFieldBorder :(UITextField *)textField{
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor whiteColor].CGColor;
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, textField.frame.size.height);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.masksToBounds = YES;
}
@end
-(void)cofigureAutoComTableView {
autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.txtActivity.frame.origin.x,CGRectGetHeight(_txtPlaceSearch .frame)+40,_txtActivity.frame.size.width, 200) style:UITableViewStylePlain];
[autocompleteTableView bringSubviewToFront:_txtPlaceSearch];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
autocompleteTableView.hidden = YES;
[self.view addSubview:autocompleteTableView];`