Chetan Gharat
Chetan Gharat

Reputation: 85

how to push tableview to UIView dynamically in iOS?

i am displaying list content in table view now i want when i click one menu of list that menu content display in UIView on next screen . how to push UITableView to UIView dynamically in iOS.

      #import "SmsCategoryTitleTableViewController.h"
    #import "CategoryMainWindowViewController.h"
    #import "AppDelegate.h"
    #import "FMDatabase.h"
    #import "FMResultSet.h"
   #import "SmsTitle.h"
   #import "SMSCategory.h"
   #import"smsDisplayViewController.h"

   @interface SmsCategoryTitleTableViewController ()

  @end

     @implementation SmsCategoryTitleTableViewController
     @synthesize theSearchBar,Id;
    @synthesize theTableView;
   @synthesize array;
   @synthesize disableViewOverlay;
   @synthesize Arrayobject;

 - (void)viewDidLoad
  {
  [super viewDidLoad];
  [self SearchBarCode];
  [self GetTableData];

  [self.tableView reloadData];
  filteredContentList = [[NSMutableArray alloc] init];
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];

  [self.view addGestureRecognizer:tap];
  // [self performSelector:@selector(push:) withObject:nil afterDelay:0.2f];
   }


   - (id)initWithStyle:(UITableViewStyle)style
   {
  self = [super initWithStyle:style];
  if (self) {
  }
  return self;
  }


    -(void)GetTableData
  {
  array = [[NSMutableArray alloc] init];

  NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  NSUserDomainMask, YES);
  NSString *documentDir = [docPaths objectAtIndex:0];
  self.databasePath = [documentDir stringByAppendingPathComponent:@"SMS.sqlite"];

 FMDatabase *database = [FMDatabase databaseWithPath:self.databasePath];
[database setLogsErrors:TRUE];
[database open];



    NSString *anQuery = [[NSString alloc]initWithFormat:@"SELECT * FROM SMSnJokes 
    where CategoryId=%@",self.Id];

  FMResultSet *results = [database executeQuery:anQuery];

    while([results next])
     {
    SmsTitle *title=[[SmsTitle alloc]init];
    title.Id = [results stringForColumn:@"Id"];
    title.CategoryId = [results stringForColumn:@"CategoryId"];
    title.Title = [results stringForColumn:@"Title"];
    [array addObject:title];
  //  NSLog(@"SMS LIST %@",title.Title);

   }


    [database close];

 }


    -(void)SearchBarCode
 {
   self.disableViewOverlay = [[UIView  
   alloc]initWithFrame:CGRectMake(0.0f,44.0f,320.0f,0)];
  self.disableViewOverlay.backgroundColor=[UIColor lightGrayColor];
   self.disableViewOverlay.alpha = 0;

  theSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 5, 374.0f, 50)];
   theSearchBar.delegate =self;
  [self.tableView addSubview:theSearchBar];
  self.navigationItem.title=@"SMS LIST";

  [[self tableView] setTableHeaderView:theSearchBar];

 }

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

 // Return the number of sections.
 return 1;
  }

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
   (NSInteger)section {
  // Return the number of rows in the section.
  if (isSearching)
  {
    return [filteredContentList count];
  }
  else
  {
    return [array count];
  }

  }




     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
   (NSIndexPath *)indexPath
     {
      static NSString *CellIdentifier=@"Cell";
     UITableViewCell *cell=[tableView 
      dequeueReusableCellWithIdentifier:CellIdentifier ];

    if(!cell)
    {


      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
  reuseIdentifier:CellIdentifier] ;
   }

   if (isSearching)
   {
    cell.textLabel.text = [filteredContentList objectAtIndex:indexPath.row];

  }
  else
  {
      SmsTitle *title = [array objectAtIndex:indexPath.row];
     [cell.textLabel  setText:[NSString stringWithFormat:@"%@ ",[title    
  valueForKey:@"Title"]]];
  }
  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

     return cell;

  }
  - (void)searchTableList
  {
    NSString *searchString =theSearchBar.text;
   filteredContentList = [[NSMutableArray alloc]init];
  [filteredContentList   removeAllObjects];
   for (SmsTitle *title in array)
   {
    NSString *tempStr = title.Title;
            NSComparisonResult result = [tempStr compare:searchString options:    
     (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, 
    [searchString   length])];
    if (result == NSOrderedSame)
    {
        [filteredContentList addObject:title.Title];
    }
    }


   }




   #pragma mark - Search Implementation


  - (void)viewDidAppear:(BOOL)animated
  {
  [super viewDidAppear:animated];
  [self.theSearchBar resignFirstResponder];
   }


   - (void) dismissKeyboard
   {
    [self.theSearchBar becomeFirstResponder];
    }


    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
  {

    isSearching = YES;

    [theSearchBar setShowsCancelButton:YES animated:YES];


    }

  - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  {
   NSLog(@"Text change - %d",isSearching);

  //Remove all objects first.
  [filteredContentList removeAllObjects];


   if([searchText length] != 0) {
    isSearching = YES;
    [self searchTableList];

   }
    else {
    isSearching = NO;

    }
    [self.tableView reloadData];
   }

 - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
    {
   theSearchBar.text=nil;
  [theSearchBar setShowsCancelButton:NO animated:YES];
  [theSearchBar resignFirstResponder];
    }

  - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
   {
  [theSearchBar setShowsCancelButton: YES animated: YES];





[self searchTableList];
 [searchBar resignFirstResponder];

}

  - (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active
  {
  self.theTableView.allowsSelection = !active;
  self.theTableView.scrollEnabled = !active;

  if (!active)
  {

    [disableViewOverlay removeFromSuperview];
    [searchBar resignFirstResponder];
  }
  else
  {
    self.disableViewOverlay.alpha = 0;
    [self.view addSubview:self.disableViewOverlay];

    [UIView beginAnimations:@"FadeIn" context:nil];
    [UIView setAnimationDuration:0.5];
    self.disableViewOverlay.alpha = 0.6;
    [UIView commitAnimations];
    NSIndexPath *selected = [self.theTableView indexPathForSelectedRow];
    if (selected)
    {
        [self.theTableView deselectRowAtIndexPath:selected
                                         animated:NO];
    }
    }
    [searchBar setShowsCancelButton:active animated:YES];
    }


   -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {

    [self.view endEditing:YES];
    [self.theSearchBar resignFirstResponder];
   }
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
      textField.returnKeyType=UIReturnKeyDefault ;
      return[textField resignFirstResponder];
     }


  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath 
  *)indexPath
   {
  smsDisplayViewController *viewController1 = [[smsDisplayViewController alloc] 
  init];

  [self.navigationController pushViewController:viewController1 animated:YES];

   }

Upvotes: 1

Views: 435

Answers (3)

Sushil Sharma
Sushil Sharma

Reputation: 2361

Ok got your point. I think you want to pass data of selected cell to next UIViewController. Assume, you want to pass cell title label and you have an array of objects. In yoursmsDisplayViewController.h

   #import <UIKit/UIKit.h>
    @interface smsDisplayViewController : UIViewController 
    @property (strong, nonatomic) NSString *cellName;

Now in your SmsCategoryTitleTableViewController.m you have NSMutableArray *nameList; Then in your table view delegate method

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
         {
             [tableView deselectRowAtIndexPath:indexPath animated:YES];

             smsDisplayViewController *viewController1 = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginIdentifier"]; 

             viewController1.cellName=[nameList objectAtIndex:indexPath.row];

               [self.navigationController pushViewController:viewController1 animated:YES];
         }

UPDATE This is the code you posted first in your question.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
               [tableView deselectRowAtIndexPath:indexPath animated:YES];
                 smsDisplayViewController *viewController1 = [[smsDisplayViewController 
                 alloc] initWithNibName:@"viewController1" bundle:nil];
               [self.navigationController pushViewController:viewController1 animated:YES];
}

Now the wrong thing you are doing here isinitWithNibName:@"viewController1". If you are not using any nib file then your code should be

smsDisplayViewController *viewController1 = [[smsDisplayViewController alloc] init];

[self.navigationController pushViewController:viewController1 animated:YES];

Upvotes: 2

Yuvrajsinh
Yuvrajsinh

Reputation: 4584

You have to use prepareForSegue: to pass data to another ViewController. You can learn it from Here which teaches you how to transfer data between ViewControllers while using Segue.

Upvotes: 0

Akshay Bhalotia
Akshay Bhalotia

Reputation: 829

You can use Accessory Buttons in UITableView or learn more about segues.

Upvotes: 0

Related Questions