I have a UISplitViewController. When the app launches initially, the detail view is showing the detail data for the first row. However, the cell in the table is not highlighted, since it hasn't been selected yet. How can I set it to selected by default when the app loads initially?
I added this to cellForRowAtIndexPath but its not highlighting. It highlights fine when I actually select a cell.
if (indexPath.row == 0)
{
cell.selected = YES;
}
-(void)selectFirstRow {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
// optional:
// [self tableView:myTable willSelectRowAtIndexPath:indexPath];
[myTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
// optional:
// [self tableView:myTable didSelectRowAtIndexPath:indexPath];
}
Then call [self selectFirstRow]; wherever you need to.
Related
I'm writing an iPhone app with a UITableView as the primary user interface. Each section consists of two rows, a header and the body. When the user clicks on the header, I remove the second row by changing the numberOfRowsInSection value:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
cbwComponent *comp = [_componentController objectInListAtIndex:section];
if([comp hasContentsView] && !comp.contentsHidden){
return 2;
}else
return 1;
}
When the user selects the header, I'm using the following code:
comp.contentsHidden = YES;
[self.tableView beginUpdates];
NSArray *deleteIndexPaths = [[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:1 inSection:indexPath.section], nil];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
It's working great, with a nice smooth fade effect. The problem is, I'm trying to add an indicator in the header cell (row 0) that changes when it's clicked on. To change that image I have to refresh the top row as well as the second row, which makes the transition look bad (well, not nearly as smooth). Is there a way to change the image in a UITableViewCell without refreshing the cell?
Thanks
EDIT: I figured it out! You can maintain the smooth transition as long as you reload that first row before you make the change to the second row. It has to be called inside of [tableView beginUpdates];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:0 inSection:indexPath.section], nil] withRowAnimation:UITableViewRowAnimationNone];
...
[self.tableView endUpdates];
Did the trick.
You could also subclass a tableview cell and implement a view transition in it that can be called from your view controller. You could then call that without having to reload the cell.
[(YourCustomCell*)[tableView cellForRowAtIndexPath:indexPathOfYourCell] fadeInIndicator];
I'm calling a method that selects the first row of the table view when the view loads. But for some reason, after the selectFirstRow is being called, it goes back to self.couldNotLoadData = NO and keeps going back and forth. Any ideas why? When the initial if/else loop goes to else, that method isn't called so it doesn't keep looping.
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
if (self.ichronoAppointments.count > 0)
{
self.couldNotLoadData = NO;
[self selectFirstRow];
return self.ichronoAppointments.count;
}
else
{
self.couldNotLoadData = YES;
return 1;
}
}
-(void)selectFirstRow
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
}
This is unconfirmed, but I bet that when you call selectRowAtIndexPath:animated:scrollPosition: from selectFirstRow it calls UITableView's delegate's -tableView:numberOfRowsInSection:.
You've got infinite recursion going on, basically. tableView:numberOfRowsInSection calls selectFirstRow which calls selectRowAtIndexPath:animated:scrollPosition: which calls tableView:numberOfRowsInSection ad infinitum.
You need to move your selectFirstRow call to viewDidAppear or viewWillAppear. tableView:numberOfRowsInSection: is no place to be doing anything complex... it's called VERY VERY often.
And while you're at it, move the logic that checks for number of items into selectFirstRow. i.e.
if (self.ichronoAppointments.count) {
//select the first row
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
} else {
//don't
NSLog(#"Couldn't select first row. Maybe the data is not yet loaded?");
}
It's more DRY/modular/cleaner that way.
Actually i am using next and previous button for moving one to another cell and each cell has a textfield so when i am clicking on next button it moves me to the next cell and by getting this cell reference i can make the text field become first responder but when i am clicking on previous button it returns me no reference.
The code which i am using for next and previous is given below
- (IBAction)nextPrevious:(id)sender
{
NSIndexPath *indexPath ;
BOOL check = FALSE;
if([(UISegmentedControl *)sender selectedSegmentIndex] == 1){
if(sectionCount>=0 && sectionCount<8){
//for next button
check = TRUE;
sectionCount = sectionCount+1;
indexPath = [NSIndexPath indexPathForRow:0 inSection:sectionCount];
}
}else{
//for previous button
if(sectionCount>0 && sectionCount<=9){
check = TRUE;
sectionCount = sectionCount-1;
indexPath = [NSIndexPath indexPathForRow:0 inSection:sectionCount];
}
}
if(check == TRUE){
//[registrationTbl reloadData];
UITableViewCell *cell = [registrationTbl cellForRowAtIndexPath:indexPath];
for(UIView *view in cell.contentView.subviews){
if([view isKindOfClass:[UITextField class]]){
[(UITextField *)view becomeFirstResponder];
break;
}
}
[registrationTbl scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
// UITextField *field = (UITextField *) [cell.contentView viewWithTag:indexPath.section];
// [field becomeFirstResponder];
}
Any small suggestion will be much appreciated. Thanks in advance
The problem lies in the scrolling. When you scroll to the top of the next row, the previous row gets removed and reused for the last visible row, meaning that the method cellForRowAtIndexPath: will probably return null, as the cell is not currently available.
The quick&dirty fix would involve scrolling to Middle or a little displaced so the cell is still visible. The not-so-quick-nor-dirty would involve making a procedure that scrolls the table to make sure the cell is visible, and then when the scrolling stops, set the textfield it as the first responder.
(Edit) To explain a little more this last approach. Let's say that you add a new variable NSIndexPath *indexPathEditing. The delegate method tableView:cellForRowAtIndexPath: would have:
if (indexPathEditing && indexPathEditing.row == indexPath.row && indexPathEditing.section == && indexPath.section)
{
// Retrieve the textfield with its tag.
[(UITextField*)[cell viewWithTag:<#Whatever#>] becomeFirstResponder];
indexPathEditing = nil;
}
This means that if indexPathEditing is set, and the current row that is being loaded is visible, it will automatically set itself as the firstResponder.
Then, for example (in your nextPrevious: method), all you need to do is:
indexPathEditing = [NSIndexPath indexPathForRow:0 inSection:sectionCount];
[registrationTbl scrollToRowAtIndexPath:indexPathEditing
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
[registrationTbl reloadData];
The row will appear, the tableView:cellForRowAtIndexPath: called, and it will get automatically set as the firstResponder.
Also, notice that instead of doing a for with isKindOfClass, it's easier to set a tag number, and then retrieve the object with viewWithTag:, I incorporated this in the example.
I have a view controller that manages a table view. My understanding is that a table cell will be deselected automatically if I push another viewcontroller and then pop back to the table view.
However, in the same class (that I use a few times), there is one instance of the class when the cell is deselected but not animated (it'll just turn blue and then back to normal without animating). Why did this happen? I have a few instances of this class but it only happens to one of them. What might be causing this?
From my experience cells are not automatically deselected if you push/pop a view controller (at least not when using a navigationcontroller), unless you add some code te deselect it !
It may also be automatically deselected if you are doing a [tableView reloadData] in viewWill/DidAppear (or in a process started in these methods).
Did you try to add something like that in viewDidAppear ?
NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
if (indexPath != nil) {
[tableView deselectRowAtIndexPath:indexPath animated:YES]
}
NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
if (indexPath != nil) {
[tableView deselectRowAtIndexPath:indexPath animated:YES]
}
this you have to write in didselectRowAtIndexPath method
You can reload the Tableview again.
In your viewWillAppear
[yourTableView reloadData];
Or if you dont want to disturb your Datasource try this
NSArray *array = [yourTableView visibleCells];
for(UITableViewCell *cell in array)
{
cell.selected = NO;
}
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.