I have a Tab Bar Controller with UITableView inside. When an item is selected from the table, it pushes another View with a UITableView. What I want to achieve is when the second View has 0 rows (all rows deleted) to simulate the backBarButton behavior - to go to the previous view (the one with the Tab Bar Controller).
In -viewDidAppear:
if ([yourContentArray count] == 0) { // Or other way to check if there are 0 rows
[self.navigationController popViewControllerAnimated:YES];
}
If you want to go back once several rows have been deleted, you could put it into the Delegate, too:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
if ([yourContentArray count] == 0) {
[self.navigationController popViewControllerAnimated:YES];
}
// ...
}
}
Is your view Hierarchy like
UITabBarViewController
|--UINavigationController
|--UITableViewController1
|--UITableViewController2
If yes, then you should be able to popViewControllerAnimated: on the navigation controller.
Now I really, I mean really, don't advice to do that since it will create a very strange feeling for your user. If there is nothing to show, just don't let the user believe there is. Hide the mark for that row in the first tableView.
Related
I've got an app with a UITabBarController that controls several UIViewControllers. Within that controller there is a UITableViewController. I'm trying to figure out how I can select a row on my tableview and push the view controller of one of my other tabs while actually navigating to that tab.
I've used performSegueWithIdentifier: but it pushes the new view onto the same tab that my tableview is in. I'd like to navigate to the tab that the view I'm pushing to is on.
In case I haven't been clear: My tableview is in Tab 1. I click on a row. I want to get to Tab 2. So far I've been able to push the view controller for Tab 2 onto Tab 1. No Good.
Can anyone shed some light?
Thanks
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger theIndex;
[self.tabBarController setSelectedIndex:theIndex];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *tab2ViewController = [self.tabBarController.viewControllers objectAtIndex:1];
NewViewControllerToPush *mvc = [[NewViewControllerToPush alloc] init];
[tab2ViewController.navigationController pushViewController:mvc animated:YES];
[mvc release];
}
When you enter the viewControllers in your tabBar, make sure to wrap them inside a UINavigationControllers, otherwise you would not be able to use push and pop functionalities.
I am working on UITableView and I am doing a deletion. As far as I know there are 2 ways to delete a row
swiping from the left to right and Delete button will appears so that you can do a deletion
add EDIT button to right top right or left. By clicking on it, EDIT will change to DONE and each row of a table will have a red circle
with (-) sign in front of each. Click on it , delete button will
appear and you can process
I noticed that if you do have an EDIT button and use the method 1 to delete, the EDIT button also change to DONE...However, after swiping it, mine is still EDIT. does any one encounter this problem before, please help. All comments are welcomed here. Thanks
Swipe to delete happens when the view is not in edit mode. It's supposed to be a quick way to delete items when browsing the view. Check out how it works in the Mail app for example - you can either tap to edit all rows, or just swipe to delete one of them.
So basically it's working correctly.
However, if you want to override the standard behaviour, you can call setEditing: on the viewController to toggle the mode programmatically, which will also update the edit button.
Add the Edit button using the ViewdidLoad method
-(void)viewDidLoad;
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [self editButtonItem];
}
And for the delete method
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
[array removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPath:[NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationFade];
[self.tableView reloadData];
}
To get this behavior you need to implement these two delegate methods:
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
self.editing = YES;
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
self.editing = NO;
}
hii,
i need delete a cell data in a table view and rearrange after clicking the delete button in another view controller i will go to another view controller on selecting index at row in atable view controller....please do need ful help
Thanks&Regards
Devi.
call
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
this method with from another class... with editing style set to UITableViewCellEditingStyleDelete
provided you have implement the same method in your previous class...
Pass a reference to your initial view controller with the table to the 2nd controller with you initialize and push it. Then you will be able to call any public methods on that view controller. This will allow you to define a method like -(void)deleteIt; and call that directly from the other controller.
Also, if you need to track which cell element was clicked, pass the index along also.
EDIT
To pass data to the new view controller, you can do:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NextViewController *nvCon = [[NextViewController alloc] initWithNibName:#"NextViewController" bundle:nil];
nvCon.rootViewCon = self;
nvCon.selectedIndex = indexPath;
[self.navigationController pushViewController:nvCon animated:YES];
[nvCon release];
}
Then call a public method on rootViewCon to initiate the delete. You can pretty much just copy the boilerplate code used for deleting table view cells here. There are many references on SO and in the Apple docs.
I am building an app with several levels.
First level are the Tabs on the bottom of the screen. (MainWindow)
Second Level is the content displayed with each tab in a TableView (Locations)
Third Level is the detailed overview of the selected item in the Second level content. (LocationDetails)
First and Second levels work fine. Now I want to create the content of the detailled overview. I have created a new NIB called LocationDetails.xib. It only contains some static text (an inserted Text View). So I can view the loaded window.
In the Locations.m file I have the following function:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * locationCity = [[locations objectAtIndex:indexPath.row] objectForKey:#"city"];
NSLog(#"Your choice is: %#", locationCity);
Within this function I want to go to an other view (LocationDetails). What is the best way to go. If I haven't given you enough information, tell me! I can provide all the information you need!
Hope to hear from you soon!
With kind regards,
Douwe
I would use a navigation controller.
In your main xib file you would end up with something like this :
UITabBarViewController // Main Window
tab 1:
UINavigationController
UITableView // (Locations)
tab 2:
UINavigationController
UITableView // (Locations)
Then, the code to slide on your LocatonDetails is simple :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
LocationDetailsViewController = [[LocationDetailsViewController alloc] initWithNibName:#"LocationDetails" bundle:nil];
controller setLocation:[locations objectAtIndex:indexPath.row]];
[[self navigationController] pushViewController:controller animated:YES];
[controller release];
}
How can I pass the first tableview row data to second detail view row
I use
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
or
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
to show up disclosure button in each cells
it can ,I also add actions in
- (void)tableView:(UITableView *)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(#"disclosure button %# touched",indexPath.row);
}
But when I touch the disclosure button I got nothing shows up in console
Than I add actions in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// carry Alarm Name text into sub-table-view to look for detail time and text info
NSLog(#"Alarm Name = %#", [alarmName objectAtIndex:indexPath.row]);
NSLog(#"Index Path Raw# = %i", indexPath.row);
// Navigation logic may go here. Create and push another view controller.
AlarmDetailTableViewController *detailViewController = [[AlarmDetailTableViewController alloc] initWithNibName:#"AlarmTableViewController" bundle:[NSBundle mainBundle]];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
detailViewController = nil;
}
It can notified on console mode
But I think it always appear the same detail view when I touch any rows in First Level TableView
How to show a new detail view with data from Level one ?
for example
To make the tableView1.row0.text = detail view.title
and show other data under the title
if you need the code I can upload it
my boss said it's not big secret inside...
Thanks a lot
Perhaps the problem is with your:
- (void)tableView:(UITableView *)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
which should be reading:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
Ok,if you tapped on accessory button than show another detail view
we need to add a detail view
First create a "new navigation based application",ex FirstView
right click on the class folder and choice "add new File" choice a new view you wanna display
ex I selected a new tableview named "DetailView"
than first go to the DetailView.m go give a section and row numbers
or declare the layout
than back to FirstView.h
add #import DetailView
go to FirstView.m
find this method - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
and use DetailView sub class to create a new view
add the codes like this
DetailView *detailView =[[DetailView alloc]init];
detailView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:detailView animated:YES];
you can add any view you want to display