Editing a tableview in a tabbed application - uitableview

I have a tabbed view application which uses a tab controller as the root view controller. One of the views I have is a tableview with I want to be able to edit by clicking on an add/edit button that will make all the cells editable as well as allowing to add a cell.
In the table view I have added a navigation bar and have tried to use the following code to create a button on the bar that when pressed will make all the cells editable:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithTitle:#"Delete"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(toggleEdit:)];
self.navigationItem.rightBarButtonItem = editButton;
}
-(IBAction)toggleEdit:(id)sender {
[self.mTableView setEditing:!self.mTableView.editing animated:YES];
if (self.mTableView.editing)
[self.navigationItem.rightBarButtonItem setTitle:#"Done"];
else
[self.navigationItem.rightBarButtonItem setTitle:#"Delete"];
}
The code doesn't work, it doesn't produce a button on the navigation bar. I have read that I should use a navigation controller but if I wish to have this one page (in my tabbed view app) use a navigation controller how do I get the tab bar to point to the nav controller as a selection option?
----Update-----
Ok Ive made a nib file that contains the root view controller (a tab bar controller). The tab bar controller contains 2 view controllers and a navigation controller with a view controller in it. I link the View controller in the Navigation controller to a View that contains a tableview. When I run the program and try to click on the tab for the tableview I get the following error:
2012-04-29 17:23:28.116 ash[6778:f803] -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x68bb8d0
2012-04-29 17:23:28.117 ash[6778:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x68bb8d0'
My view controller that contains the tableview is a UIViewController that implements
In its m file it contains the following methods:
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.portfolioArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"CellTableIdentifier";
static BOOL nibsregistered = NO;
if (!nibsregistered) {
UINib *nib = [UINib nibWithNibName:#"ASHInstrumentCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:SimpleTableIdentifier];
nibsregistered = YES;
}
ASHInstrumentCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
NSUInteger row = [indexPath row];
cell.type.text = [portfolioArray objectAtIndex:row];
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSString *rowValue = [portfolioArray objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:
#"You selected %#", rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Row Selected!"
message:message
delegate:nil
cancelButtonTitle:#"Yes I Did"
otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
NSUInteger row = [indexPath row]
[self.portfolioArray removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic] ;
}
What am I doing wrong?

Two points: First of all, a UINavigationController is a subclass of UIViewController, so you could create an instance of UINavigationController whose rootViewController is your tableViewController, and have the TabBarController manage the nav controller as one of its tabs.
Also note that the UIViewController class already has a editButtonItem property that you should use instead of creating your own button:
self.navigationItem.rightBarButtonItem = self.editButtonItem;

Ok I think I figured out what I was doing wrong. I had created a nib file for my tab view controller and to this I added to view controllers and a Navigation controller (which itself contained a tableview controller).
One of the things I wasn't doing was allocating the correct class type to the view controllers in my tabs. If I have created a custom view controller class firstviewcontroller which had IBOutlets and I didn't specify the view controller in my tab controller nib to be of class firstviewcontroller I would get that weird key value error.
The second problem I had was that the tab view controller wouldn't autorotate even though all the view I wanted to show allowed for autorotation using the following code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
The reason for this again was that I hadn't specified the class of each of the view controllers (in my tabbedviewcontroller nib file) to be of my custom made classes that I wished them to show.
Once I specified their class type everything fell into place.

Related

UISearchBar Staying at top when seguing from tableview results

I created a search with 2 UINavigationcontrollers and with 2 UITableViewControllers. One for displaying the initial results, and then another TableViewController for when displaying searched results.
This is where i think the issue occurs. I use this method to show the resultsTableViewController where i make it the top view controller.
Now when i segue from the results view controller, the UISearchBar stays on top. When i code [vc.tableView setDelegate: self];.
it removes the issue of the search bar staying but then screws up the didselectrowatindex path which i need to identify because the results aren't the same from the original and the results Tableviewcontroller.
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
// Set searchString equal to what's typed into the searchbar
NSString *searchString = self.searchController.searchBar.text;
[self updateFilteredContentForAirlineName:searchString];
// If searchResultsController
if (self.searchController.searchResultsController) {
UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController;
// Present SearchResultsTableViewController as the topViewController
SearchResultsTableViewController *vc = (SearchResultsTableViewController *)navController.topViewController;
// Update searchResults
vc.searchResults = self.searchResults;
// And reload the tableView with the new data
[vc.tableView reloadData];
}
}
Here's a project i mocked up that has the issue included.
http://expirebox.com/download/10533c5d386618b95bb39bd1dc886ace.html
try to extend your next viewcontroller to class.so i will create new data class and extend to viewcontroller than paste this code in didSelectRowAtIndexPath: method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
data *next=[self.storyboard instantiateViewControllerWithIdentifier:#"datascreen"];
[self presentViewController:next animated:YES completion:nil];
}
ok bro try this
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
data *next=[self.storyboard instantiateViewControllerWithIdentifier:#"datascreen"];
[self.navigationController next animated:YES];
}
both class use this code.try this if any problem than tell me

Must register a nib or a class for the identifier or connect a prototype cell

I have a default master-detail table view with a menu. The menu works, but for some reason I get the following error when pressing the '+' button to add another cell in my app:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
I searched for a solution for days
If I add to viewDidLoad:
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
I don't get the error, but the cells that are added aren't the ones configured in my storyboard, nor do they point to the detail view controller.
I have checked multiple times if the identifier field for the prototype cell has been filled in and is the same as *cellIdentifier.
From what I know it seems of the same nature as the question asked here.
Any help would be appreciated and if you have the time please explain why it's wrong what I did or why certain code should be added.
Requested code (both are default code added by Xcode 5.0.1):
// Code to add the button
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
// Code called when button is pressed:
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
Also check out the pictures and project files added in the comment
Replace the following line in [MAAppDelegate application:didFinishLaunching]
UINavigationController *masterViewController = [[UINavigationController alloc] initWithRootViewController:[MAMasterViewController new]];
with
UINavigationController *masterViewController = (UINavigationController *)self.window.rootViewController;
When the app reaches didFinishLaunching, the root view controller has already been configured from the storyboard (if you look at your storyboard, the root view controller is the one with the arrow pointing to it). Note that this process relies on the initWithCoder: initializer.
In didFinishLaunching you're discarding this view controller, replacing it with an instance of TWTSideMenuViewController, which you've configured with new, non-storyboard instances of UINavigationController and MAMasterViewController. If you think about it, there's no way for [MAMasterViewController new] to know that it should be configured from a particular storyboard element. There is actually an API for instantiating view controllers from the storyboard: [storyboard instantiateViewControllerWithIdentifier:]. But you don't need to do that since it's already been done for you.
So the solution is simple. Just configure TWTSideMenuViewController with the existing root view controller self.window.rootViewController.

IOS - get a value from a table view controller and send it to another table view controller

I have two Table View Controller in a Navigation Controller. The Cloth table is the main view and when I click on one of the cells go to another view controller that gives me the detail, when I select the option i want and return to the Return of the Navigation button, I lose the information. How do I take the value to the main view?
Here my code: Main View - AddClothViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[list objectAtIndex:indexPath.row] isEqual:#"Categorias"] )
{
CategoriesViewController *DVC = [[CategoriesViewController alloc] initWithNibName:#"CategoriesViewController" bundle:nil];
[self.navigationController pushViewController:DVC animated:YES];
}
}
Here my code: Categories ViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Set the checkmark accessory for the selected row.
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
// This is my main View Controller when i declare a variable category and I try to access from
//here
AddClothViewController *DVC = [[AddClothViewController alloc] initWithNibName:#"AddClothViewController" bundle:nil];
DVC.clothNew.categoria = [list objectAtIndex:indexPath.row ];
}
I resolved my problem by using this tutorial.
(Simple delegate tutorial for ios development)

Different custom View controller for iOS splitViewcontroller

I have a splitview controller containing master and detailview controller. But now i need to display different custom view controller based on the selection in the masterviewcontroller (a tableview). How can i achieve this?
Write below code in master view controller.
Take the decision of creating custom view based on indexpath value.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UISplitViewController *splitController = self.splitViewController;
NSMutableArray *arr = [splitController.viewControllers mutableCopy];
CustomViewController *customVC = [[CustomViewController alloc]init];
[arr replaceObjectAtIndex:1 withObject:customVC];
[customVC release];
splitController.viewControllers = arr;
}

UITabBar disappears when selecting UITableViewCell

I have a UITableView with a cell named 'Sunset'. Upon pressing the UITableViewCell, the view switches to 'infoView' which loads some text from a .txt file. When pressing the 'back' button, the view switches back to the UITableView but there's a problem. After loading the view, the UITabBar is gone! Since it is gone, it is impossible to go back to another view.
I have tried:
self.hidesBottomBarWhenPushed = NO;
Code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cellSelected = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellTitle = cellSelected.textLabel.text;
if (cellTitle == #"Sunrise")
{ //Swap views
informationView *iv = [[informationView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:iv animated:YES];
}
}
Of course the tab bar doesn't show. You are presenting the second view modally. If you want to keep this implementation you are going to have to add a button or something on the second view that will perform
[self.navigationController dismissModalViewControllerAnimated:YES]; //Programatically dismiss the view controller
If you want to keep the tabBar and the navigation bar functionality, you should use
[self.navigationController pushViewController:iv animated:YES];
instead of
[self presentModalViewController:iv animated:YES];

Resources