TableView is not showing when creating it programmatically - ios

In my main view, I want to create a tableView with some data.
For this, when you select a row in the tableView from the second view, it should be creating the tableView in the main.
OpenTableView.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *vc = [[ViewController alloc] init];
[vc makeView];
}
ViewController.m
- (void)makeView
{
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 700, 704) style:UITableViewStylePlain];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.tableView];
[self.tableView reloadData];
[self viewDidLoad];
}
Why is this not working?
If you need more details or pictures, just ask.
Thanks in advance to all of you.

You need to move makeView method into your viewDidLoad method. This will let the life cycle process of the application take over initializing your view frame and when it is ready you can set up your views.
OpenTableView.m
ViewController *vc = [[ViewController alloc] init];
Within your new view controller in the viewDidLoad method is when you should programmatically setup any views and add them to the main view.
ViewController.m
- (void) viewDidLoad(...){
[super viewDidLoad:...];
[self makeView];
}

You must present view controller
makeView is necessary to call the method after loading View

Related

Tableview is not visible on UIViewController

UITableView is not visible on the UIViewController Class.
On Storyboard added a new UIViewController set to related SlideViewcontroller class to it, Add a new UITableView (tbl_Slide), set the datasource and delegate to UIViewController in storyboard for the added UITableView.
And also included the related datasource and delegate methods to the UIViewController class.
- (void)viewDidLoad {
[super viewDidLoad];
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
self.tbl_SlideMenu.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tbl_SlideMenu.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.tbl_SlideMenu.separatorColor = [UIColor lightGrayColor];
self.tbl_SlideMenu.dataSource=self;
self.tbl_SlideMenu.delegate=self;
[self.view addSubview:self.tbl_SlideMenu];
}
Since added the UITableView to the UIViewController for UIView, need not to induced above two lines of code
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStylePlain];
[self.view addSubview:self.tbl_SlideMenu];
Once I included the allocation and added to subview its showing the data with tableview.
Once I removed allocation and frame added to subview is not visible.
So what is need of storyboard where I added UITableView to UIViewController for UIView (view default.) once Component added to storyboard and framed need not to allocate and frame in code.
Advance Thanks
Hope for best posible answers
If u added the tableview in storyboard there is no need to initialise the view components in the code in this case u don't need below lines as u mentioned
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStylePlain];
however u need to create a outlet for your tableview to access the tableview in code that means as soon as view is loaded all your view components are initialised in your case tableview is initialised and u can ready to use it and also u don't need below lines of code if u set datasource and delegate in storyboard
self.tbl_SlideMenu.dataSource=self;
self.tbl_SlideMenu.delegate=self;
since u configured your tableview datasource and delegate and also added tableview to view controller's view there is no need of below line also
[self.view addSubview:self.tbl_SlideMenu];
and second way is as u mentioned creating the view in pure code, in this case u have to do all things by your self from initialising to adding the view component to controller's view as u mentioned like below
self.tbl_SlideMenu = [[UITableView alloc]
initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain]; //creating the tableview
self.tbl_SlideMenu.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tbl_SlideMenu.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; //configuring the view
self.tbl_SlideMenu.separatorColor = [UIColor lightGrayColor];
self.tbl_SlideMenu.dataSource=self; //setting the datasource and delegate
self.tbl_SlideMenu.delegate=self;
[self.view addSubview:self.tbl_SlideMenu]; //adding it to tableview
hope u get this
Edit
it is working fine for me, i crossed checked it with some dummy values, for example as i mentioned , dragged and dropped a tableview, set its datasource and delegate to View Controller in storyboard and connected a outlet in ViewController.h file, like below,
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tbl_SlideMenu;
#end
and in ViewController.m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tbl_SlideMenu.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.tbl_SlideMenu.separatorColor = [UIColor lightGrayColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CELL"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CELL"];
}
cell.textLabel.text = #"Hello";
return cell;
}
#end
and i got output showing the tableview with 5 "Hello" on each row

iOS multiple viewcontrollers

I am new to iOS programming.
I have to implement a location search based on the third party map API, but I think I have messed up multiple view controllers.
Scenario:
I have a mapviewController for displaying the mapview. And I have a searchController for searchbar. There is a resultviewController property in searchController for display the search suggestions.
I have tried to add search VC as child VC to map VC, but once I click to the search bar it returns "Application tried to present modally an active controller".
So then I remove search VC from map VC & the search bar is not activated.
Therefore I want to ask how to handle these VCs, and avoid the case that map view covers the search bar and suggestion lists.
Thank you so much.
UPDATE:
I'm building a search bar in code. My aim is to build the following style in iOS.
like this http://i1.tietuku.com/cd4efa23d97af75ft.jpg
I pushed the map viewController to the navigation controller from menu.
[self.navigationController pushViewController:mapviewController animated:YES];
And trying to add a searchController to it.
- (void)viewDidLoad {
[super viewDidLoad];
_resultsTableController = [[UITableViewController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
_searchController.searchResultsUpdater = self;
[_searchController.searchBar sizeToFit];
_resultsTableController.tableView.tableHeaderView = _searchController.searchBar;
self.searchController.searchBar.delegate = self;
_resultsTableController.tableView.delegate = self;
_searchController.delegate = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.searchBar.delegate = self;
mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, kHeight(100), kScreen_Width , kScreen_Height-kHeight(100))];
[self addChildViewController:_searchController];
[self.view addSubview: _searchController.view];
[self.view addSubview:mapView];
}
for UISearchController you no need to add it as child view controller and and instant of using UITableViewController uses UITableView it make easy for you and
here below is code that is .m file
free to run that code and without uses storyboard.
#interface ViewController ()
{
UITableView *tableview;
UISearchController *searchcontroller;
NSMutableArray *array;}
#end
#implementation ViewController
(void)viewDidLoad
{
[super viewDidLoad];
array = [[NSMutableArray alloc]initWithObjects:#"one",#"two",#"three",#"four", nil];
tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)/2)];
[self.view addSubview:tableview];
tableview.dataSource = self;
tableview.delegate =self;
searchcontroller = [[UISearchController alloc]initWithSearchResultsController:nil]; // Create Reference of searchController
searchcontroller.searchResultsUpdater = self;
searchcontroller.searchBar.backgroundColor = [UIColor redColor];
searchcontroller.searchBar.barStyle = UIBarStyleBlack;
searchcontroller.searchBar.barTintColor = [UIColor whiteColor];
searchcontroller.dimsBackgroundDuringPresentation = false;
[searchcontroller.searchBar sizeToFit];
tableview.tableHeaderView = searchcontroller.searchBar;
MKMapView *mapview = [[MKMapView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(tableview.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)/2)];
[self.view addSubview:mapview];
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;
{
NSLog(#"update ");
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
// Default is 1 if not implemented
{
return 1;
}
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
}
cell.textLabel.text = array[indexPath.row];
return cell;
}
According to me you should use only one view controller which has a map on it. Over the map you can simply add custom search view and on typing/ taping search you can show search results using UITableview with some dropping animation keeping some max frame fixed, And finally on taping any result remove this UITableView and show location of map. If time permits I will create a sample for you, cant give assurance but will try. Mean while you can also go through Maps reference may be helpful

UITableView bottom area keeps out of screen if navigationBar.translucent is NO;

I use UITableView instance as the subview of my ViewController's view, the table's content is higher than the screen so it is scrollable. Here is the code in AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)opts {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *nav = [[UINavigationController alloc] init];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
UIViewController *controller = [[ViewController alloc] init];
[nav pushViewController:controller animated:YES];
// set to No cause the problem
nav.navigationBar.translucent = NO;
return YES;
}
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.textLabel.text = [NSString stringWithFormat:#"test %u", indexPath.row];
return cell;
}
#end
Now the problem is
if nav.navigationBar.translucent = YES, the content is scrolling right, the bottom line is visible if I scroll to bottom.
if nav.navigationBar.translucent = NO, the content isn't scrolling right, the bottom line automatically move out of the screen if I end the swip.
Seems like the auto hidden area is of the height of navigationBar, any one who knows how to fix it? I want it work the same no matter translucent = YES or translucent = NO.
Try this & let me know if it works:
self.tableView.contentInset = UIEdgeInsetsMake(44,0,0,0);
I figure out the solution, I set the tableView as the mainview of the controller, not a subview of the controller's mainview
[self setView:_tableView];
instead of
[self.view addSubview:_tableView];

Difference between [UINavigationController setView] and [UINavigationController setViewControllers]

I have a method, which is supposed to init a navigation controller and load a view controller for a side menu. This all happens within a view controller that's not attached to any other navigation controllers.
- (void)showLeftNC
{
if (leftNavCon == nil)
{
leftNavCon = [[UINavigationController alloc] init];
}
[leftNavCon setViewControllers:#[lmvc] animated:NO];
//[leftNavCon setView:lmvc.view];
[leftNavCon.view setFrame:lmvc.view.frame];
[self.view addSubview:leftNavCon.view];
[self showCenterViewWithShadow:YES withOffset:-2];
[self.view sendSubviewToBack:leftNavCon.view];
}
leftNavCon is the navigation controller
lmvc is the primary view controller
It doesn't work in this way, neither when I call initWithRootViewController:lmvc
It only has worked when I used the commented [leftNavCon setView:lmvc.view]. But even then I couldn't get the navigation controller to push any another view controllers yet.
Help please.
never mind, i worked out something, I used lmvc as a containing view, initialised the navigation controller and all the view controllers i needed inside it
in the lmvc initWithNibName
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
if (testViewController == nil)
{
testViewController = [[UIViewController alloc] init];
}
if (navCon == nil)
{
navCon = [[UINavigationController alloc] initWithRootViewController: testViewController];
}
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 748)];
[self setView:navCon.view];
[testViewController.view addSubview:table];
[testViewController.view addSubview:button];
[testViewController.view addSubview:anyControlYouNeed];
}
and then later on
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableTwo == nil)
{
tabl = [[UITableView alloc] initWithFrame:table.frame];
}
if (testViewControllerTwo == nil)
{
testViewControllerTwo = [[UIViewController alloc] init];
}
tableTwo.delegate = self;
tableTwo.dataSource = self;
[testViewControllerTwo setView:tableTwo];
[navCon pushViewController: testViewControllerTwo animated:YES];
}
Works like a charm

Selections not occurring when UISearchDisplayController is being used

I am using UISeachDisplayController with a UITableView. It is set up programatically in the viewDidLoad method for the UITableViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadProjectsAndTitles];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 600, 44)];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsTableView.allowsSelection = TRUE;
self.tableView.tableHeaderView = searchBar;
}
Everything is working absolutely fine except that when I click on a row when the UISearchBar is being used, the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method is not being called although it is being called perfectly well when the UISearch is not being used. My question is is this normal behaviour or should the selection method work? If it should work are there any suggestions why it might not be working?
I think you forgot to set
searchDisplayController.searchResultsDelegate = self;
searchResultsDelegate is the delegate for the table view in which the search results are displayed, and didSelectRowAtIndexPath is one of the table view delegate methods.

Resources