If count of the Array that tableview is using is 1 then how to select the first row in the table i used the following code it is selecting but not navigating to next screen
NSIndexPath * ip =[NSIndexPath indexPathForRow:0 inSection:0];
[npTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
you have to use some delegate methods for uitabelview
you have to add UITableViewDataSource,UITableViewDelegate.
You have to add the delegate method of uitabelview i.e)
Add delegate method as below.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//here you have to write the code for to navigate next screen.
sample *sampleobject=[[sample alloc]init];
[self.navigationController pushViewController:sampleobject animated:YES];
}
that's it enjoy now u can naviagte to another page..
This only highlights the table cell. You would need to call the code which would appear in your tableView:didSelectRowAtIndexPath method or alternatively you could call it yourself like:
NSIndexPath * ip =[NSIndexPath indexPathForRow:0 inSection:0];
[self tableView: npTable didSelectRowAtIndexPath: ip];
Related
What I was trying - I have a UITableView. On selecting any particular row it should show a selectionColor(Blue etc) and then it should disappear. Any property/code to do this? I am actually applying in in MyCalender view.
You can use [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
in your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method.
You should refer the delegates and datasource in tableview for this
The datasource sets the inputs to table
The delegate gives you callbacks of actions on the tableview .
So write the delegate methods ,include it in classdef UITableViewDelegate,connect it in xib
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This delegate method is invoked if you click on a particular row.The particular row is the row at the "indexpath"
finally include this line which deselect the code
[tableView deselectRowAtIndexPath:indexPath animated:YES];
add this to your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
`
UIView *violetColor = [[[UIView alloc] init] autorelease];
//violetColor.backgroundColor = [UIColor colorWithRed:0.824 green:0.749 blue:0.553 alpha:0.70];
violetColor.backgroundColor = [UIColor colorWithRed:0.724 green:0.749 blue:0.953 alpha:0.70];
cell.selectedBackgroundView = violetColor;
`
and then add this to your didselect `
if (indexPath != nil) {
[mTableView deselectRowAtIndexPath:indexPath animated:YES];
}
`.i hope this will do the trick.
In delegate method didSelectRowAtndexPath use this code
[tableView deselectRowAtIndexPath:indexPath animated:YES];
If you are using UITableView and want to remove selection of the UITableViewCell. Then you have to use the below code to deselect tableview cell.
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
UITableView shows last item on the startup. How do I view the first item on the startup?
You can call following method to position your table view to any cell that you want:
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
For example,
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
Is there a way that a UITableView pops up when i select a row in a table?
thanks in advance.
In your UITableView delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[self.view addSubview:myView];
}
Here, myView is the view which you have IBOutlet.
like
in .h file
IBOutlet UIView *myView;
Let me know in case of any difficulty.
Cheers.
Yes there is a way on row selection method you have to fire up a method which creates auiview smaller in size as desired and in that view table view as subview.
Plz specify what type or kind of pop-up you want to appear.
OK, I understand now.
You should use table view's updating methods:
[myTableView beginUpdates];
NSArray *indexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:0 inSection:1],
[NSIndexPath indexPathForRow:1 inSection:1],
[NSIndexPath indexPathForRow:2 inSection:1],
[NSIndexPath indexPathForRow:3 inSection:1],nil];
[myTableView insertRowsAtIndexPaths:indexPaths withRowAnation:UITableViewRowAnimationTop];
[myTableView endUpdates];
I have an application in which I have tableview in which I return 3 rows in the tableview. When I select a particular row in the table view another tableview should open which should display list of items and when I select a particular item the value of that particular item should be set to the textlabel of previous tableview.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView != secondTableview) {
self.secondTableviewArray =[[NSMutableArray alloc]initWithObjects:#"secondfirst",#"secondSecond",nil];
[secondTableview reloadData];
}
}
Pass the second controller (the one that will appear) a weak reference to the first just before showing it. Once the second view controller has appeared and the user has made the selection (you can catch that in (-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;), send a message to the first giving the value selected.
The first view controller, then can store the new value, and even dismiss/pop the second.
This can also be achieved, by writing your own delegate protocol, so the first conforms to it, and is the delegate for the second.
Take NSString in second view and use the property.from the first view to second view set the string and in second view use that String
If you want to open new table view on click of row then you make new xib file and call like as define in below code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
XMLBooksViewController * xmlBooksViewController = [[XMLBooksViewController alloc] initWithNibName:#"DetailView" bundle:nil];
XMLBooksAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
xmlBooksViewController.author = [appDelegate.authors objectAtIndex:indexPath.row];
[self.navigationController pushViewController:xmlBooksViewController animated:YES];
[xmlBooksViewController release];
}
In next file you define another table view, so the list or items comes as table view.
I have a UISplitViewController with a Table View for navigation. It's similar to the Mail app. When you click on a table view in portrait mode, the popup hides itself. When you click on the nav bar to get the popup back, the selected item no longer appears selected. How can make this item appear selected without re-selecting the item? (just like in the mail app)
In your viewDidLoad method, do you call
self.clearsSelectionOnViewWillAppear = NO; ?
This is how Xcode's SplitView template does it.
Do you have by any change a
[tableView deselectRowAtIndexPath:indexPath animated:YES];
in your didSelectRowAtIndexPath in the RootViewController ?
I've got a solution that works, but it's frustratingly hacky. I have to call selectRowAtIndexPath twice. It seems that cellForRowAtIndexPath is invalidating the selection made in viewWillAppear. It still needs to be called in viewDidAppear, however, so the view scrolls to the proper position before cellForRowAtIndexPath is called.
- (void)viewWillDisappear:(BOOL)animated
{
NSIndexPath *selected = [self.tableView indexPathForSelectedRow];
_selectedRow = selected.row;
}
- (void)viewDidAppear:(BOOL)animated
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_selectedRow inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//initialize cell code here...
if (indexPath.row == _selectedRow) {
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
}
For your table view controller, is -viewWillAppear: called before the pop-up is displayed? If so, you could write it as so:
- (void)viewWillAppear:(BOOL)animated
{
[self.tableView selectRowAtIndexPath:<indexPath>
animated:animated
scrollPosition:UITableViewScrollPositionMiddle];
[super viewWillAppear:animated];
}
Obviously, replace <indexPath> with the proper index path and set the scroll position how you want it. You may also want to pass NO instead of animated to make it appear like it was selectd before the view appeared.