Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Showing table view on another view programatically

imosek8imosek8 Posts: 3New Users Noob
Hello everybody! I found on a Github code in which was written a combo-box for IOs that used UIPickerView. I'd like to change it that it would use UITableView instead of Picker. I wrote some code, some changes, but it doesnt work( table isnt showed, but with picker it worked). I'm asking for your help to improve it and understand what is wrong.

Here is code:
@interface TablePicker : UIControl<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
{
    UITableView* pickerView;
    IBOutlet UITextField* textField;
    NSMutableArray* dataArray;
    NSString* placeholder;
    NSString* placeholderWhileSelecting;
	NSString* toolbarDoneButtonText;
	UIBarStyle toolbarStyle;
}

@property (nonatomic, readonly) NSString* text;

-(id)initWithTextField:(UITextField *)tf;
-(id)initWithTextField:(UITextField *)tf withData:(NSMutableArray*) data;
-(void) setArrowImage:(UIImage*)image;
-(void) setData:(NSMutableArray*) data;
-(void) setPlaceholder:(NSString*)str;
-(void) setPlaceholderWhileSelecting:(NSString*)str;
-(void) setToolbarDoneButtonText:(NSString*)str;
-(void) setToolbarStyle:(UIBarStyle)style;
-(void) showArrowImage:(BOOL)b;
-(UITableView*) getTableView;
-(UITextField*) getTextField;
-(void) setValueAtIndex:(NSInteger)index;
@end
#import "TablePicker.h"


@implementation TablePicker
// @synthesize text;

-(id)initWithTextField:(UITextField *)tf
{
    return [self initWithTextField:tf withData:nil];
}

-(id)initWithTextField:(UITextField *)tf withData:(NSMutableArray*) data
{
    self = [super init];
    if (self) {
        self->textField = tf;
        self->textField.delegate = self;
        
        // set UI defaults
        self->toolbarStyle = UIBarStyleDefault;
		
        // set language defaults
        self->placeholder = @Tap to choose...;
        self->placeholderWhileSelecting = @Pick an option...;
		self->toolbarDoneButtonText = @Done;
        
        // hide the caret and its blinking
        [[textField valueForKey:@textInputTraits]
         setValue:[UIColor clearColor]
         forKey:@insertionPointColor];
        
        // set the placeholder
        self->textField.placeholder = self->placeholder;
        
        // show the arrow image
        self->textField.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@downArrow.png]];
        self->textField.rightView.contentMode = UIViewContentModeScaleAspectFit;
        self->textField.rightView.clipsToBounds = YES;
        [self setArrowImage:[UIImage imageNamed:@downArrow.png]];
        [self showArrowImage:YES];

        // set the data array (if present)
        if (data != nil) {
            [self setData: data];
        }
    }
    return self;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)numberOfComponentsInTableView:(UITableView *)tableView;
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self->textField.text = [dataArray objectAtIndex:row];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInComponent:(NSInteger)component;
{
    return [dataArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    return [dataArray objectAtIndex:row];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @SimpleTableItem;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
   
    return cell;
}

-(void)doneClicked:(id) sender
{
    [textField resignFirstResponder]; //hides the tableView
    if (self->textField.text.length == 0) {
        self->textField.text = [dataArray objectAtIndex:0];
    }
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

- (IBAction)showPicker:(id)sender
{
    tableView = [[UITableView alloc] init];
    tableView.showsSelectionIndicator = YES;
    tableView.dataSource = self;
    tableView.delegate = self;
    
    //If the text field is empty show the place holder otherwise show the last selected option
    if (self->textField.text.length == 0)
    {
      self->textField.placeholder = self->placeholderWhileSelecting;
    }
    else
    {
      [self->tableView selectRow:[self->dataArray indexOfObject:self->textField.text] inComponent:0 animated:YES];
    }

    UIToolbar* toolbar = [[UIToolbar alloc] init];
    toolbar.barStyle = self->toolbarStyle;
    [toolbar sizeToFit];
    
    //to make the done button aligned to the right
    UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    
    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc]
                                   initWithTitle:self->toolbarDoneButtonText
                                   style:UIBarButtonItemStyleDone
                                   target:self
                                   action:@selector(doneClicked:)];
    
    
    [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];

    //custom input view
    textField.inputView = tableView;
    textField.inputAccessoryView = toolbar;  
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)aTextField
{
    if ([self->dataArray count] > 0) {
        [self showPicker:aTextField];
        return YES;
    }
    return NO;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return NO;
}

// Utility Methods
-(void) setData:(NSMutableArray*) data
{
    dataArray = data;
}

-(void) showArrowImage:(BOOL)b
{
    if (b == YES) {
      // set the DownPicker arrow to the right (you can replace it with any 32x24 px transparent image: changing size might give different results)
        self->textField.rightViewMode = UITextFieldViewModeAlways;
    }
    else {
        self->textField.rightViewMode = UITextFieldViewModeNever;
    }
}

-(void) setArrowImage:(UIImage*)image
{
    [(UIImageView*)self->textField.rightView setImage:image];
}

-(void) setPlaceholder:(NSString*)str
{
    self->placeholder = str;
    self->textField.placeholder = self->placeholder;
}

-(void) setPlaceholderWhileSelecting:(NSString*)str
{
    self->placeholderWhileSelecting = str;
}

-(void) setToolbarDoneButtonText:(NSString*)str
{
    self->toolbarDoneButtonText = str;
}

-(void) setToolbarStyle:(UIBarStyle)style;
{
    self->toolbarStyle = style;
}

-(UITableView*) getTableView
{
    return self->tableView;
}

-(UITextField*) getTextField
{
    return self->textField;
}

-(void) setValueAtIndex:(NSInteger)index
{
    [self tableView:nil didSelectRow:index inComponent:0];
}


//Getter method for self.text
- (NSString*) text {
    return self->textField.text;
}

@end

I would appreciate if I got some help.

Replies

Sign In or Register to comment.