Skip to main content

Drop down using Tableview and Button. #Objective C


I have created a drop-down list in iPhone.
Steps to create:
1) Open Xcode, create new project
2) Add new ViewController.h , ViewController.m and tick on xib file also in project after creating the AppDelegate files.
3) In xib file drag an UIButton and an UITableView.
4) Place the UITableView below the button as if it will appear after clicking on the button.



First of all drag one button and table view in viewController.xib.

Then,drag the delegates and data source of tableview.

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource> {
    BOOL flag;
    NSArray *arryData;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *selectOutlet;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    arryData = [[NSArray alloc] initWithObjects:@"iPhone",@"iPod",@"MacBook",@"MacBook Pro",nil];
    //tblSimpleTable.frame =CGRectMake(10, 10, 300, 100);
    flag=1;
    _tableView.hidden=YES;
    _selectOutlet.layer.cornerRadius=8;
    _tableView.layer.cornerRadius=8;
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
        
    }
    return self;
}

#pragma mark Table view methods

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


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [arryData count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }
    
    // Set up the cell...
    cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_selectOutlet setTitle:[arryData objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    flag = 1;
     _tableView.hidden=YES;
    
}


- (IBAction)SelectButton:(id)sender {
    if (flag==1) {
        flag=0;
        _tableView.hidden=NO;
    }
    else{
        flag=1;
        _tableView.hidden=YES;
    }
}


@end

Comments

Popular posts from this blog

Draggable Marker and Drawing the routes between them using Google Maps | IOS | Swift

This blog is about how we can make the marker draggable and get the current location from it and to draw the routes between them. For that we have to install the pods of Google maps from developer.google.com. Then we have to add the following    pod 'GoogleMaps'   pod 'GooglePlaces'   pod 'GooglePlacesAPI'   pod 'SwiftyJSON' And we have to save the pods into the pod file. Then, we have to install all the pods through terminal. Again we have to make the outlet for textfield and button as discussed below. 2 TextField and 3 Buttons. Hope you will enjoy the coding. Happy coding.! import UIKit import GoogleMaps import GooglePlaces import SwiftyJSON import Alamofire enum Location { case startLocation case destinationLocation } class ViewController: UIViewController , GMSMapViewDelegate ,  CLLocationManagerDelegate { @IBOutlet weak var googleMaps: GMSMapView ! @IBOutlet weak

Route between two places using Google Maps iOS SDK.

Its very interesting to have the Google maps  Firstly. We have to install the pods of Google maps. So, follow the steps for installing the pods of Google maps on developers.google.com 1. Create the pod file as on the site. 2. In Podfile add the pod of Alamofire and SwiftyJSON. 3. Save the pod file.      pod 'GoogleMaps'     pod 'GooglePlaces'     pod 'Alamofire' , '~> 4.2.0'     pod 'SwiftyJSON' 4. Install the pods. Here is the code for route. import UIKit import GoogleMaps import GooglePlaces import Alamofire import SwiftyJSON class GoogleViewController: UIViewController , GMSMapViewDelegate {     @IBOutlet weak var googleView: GMSMapView !          // AIzaSyC907BQBnrZK0UjA2zARtE6Mq7L__yqw5Q     override func viewDidLoad() {         super . viewDidLoad ()         GMSServices . provideAPIKey ( "AIzaSyC907BQBnrZK0UjA2zARtE6Mq7L__yqw5Q" )         GMSPlacesClient . pr

Cancellable Task with Dispatch Work Item | iOS | Swift

In some scenarios, we all want that the task we want to perform should be cancellable later on. For this kind of behaviour, we already have OperationQueue but the use of It seems difficult for beginners. Most of us didn't use OperationQueue so frequently and were only familiar with Grand Central Dispatch (GCD) . by performing UI operations in the main thread, Dispatch Group, etc. By the use of DispatachWorkItem , we can create some tasks that can be cancelled later on. Here is the example, let workItem = DispatchWorkItem {     // Here is our task     debugPrint ( "Task is to print some text..." ) } This is DispatchWorkItem containing a task to print something. To perform this task, we need to make use of DispatchQueue like this by adding delay if required. DispatchQueue . main . asyncAfter (deadline: . now () + . seconds ( 1 ), execute: workItem ) Here is the basic example to add a task and perform it using DispatchQueue. Now to cancel that we need to keep the referen