I want to navigate from a tableviewcell to another view by the cell's path.
I am using the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath == 0) {
[self performSegueWithIdentifier:#"segueToKnutene" sender:self];
}
As you might understand, this did not work.
Keep in mind that i am fairly new to xcode and objective-c.
In my head what this does is performing a segue to another view if the top cell in my tableview is pressed/released. And this is also what i want to do.
An NSIndexPath consists of a row and a section.
So you probably want to use
if (indexPath.section == 0 && indexPath.row == 0)
Related
I need to keep last but one indexPath cell of table view and compare it to the current one on
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method.
I don't know how can I do it. I found answers about last cell selected, but it doesn't help me. If anybody has some solution, please let me know about it.
To keep the reference of last indexPath in tableview add this code in your cellForRowAtIndexPath:
if(indexPath == _indexPathReference)
{
//add your logic
}
...
...
if(indexPath.row == array.count - 1)
_indexPathReference = indexPath;
return cell;
Hope this helps!
You try this out:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:<Pass_Last_But_One_Row> inSection:<Pass_Correct_Section>]
Here,
Pass_Last_But_One_Row = cellDataModel.count - 1;
I have a table view controller in which I need to display a date&time picker when a cell is tapped and hide it when the cell is tapped again. Basically the same effect that the iphone has when you choose your start and end date and time to create a new event in the calendar.
I'm guessing the display and hiding goes in the following method but I'm unsure of what goes inside:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}
Some example code or link would be great. Thank you!!
Create your cell with whatever you want to show and the picker:
-----------------------------------
cell visible part
-----------------------------------
cell invisible part (with picker)
-----------------------------------
Define a property that let you know if you have to show the entire cell:
#property (nonatomic) BOOL shouldShowPicker;
Initialise this property (on viewDidLoad for example);
self.shouldShowPicker = NO;
A couple of methods to touch:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 4) { //where your picker row is
self.shouldShowPicker = YES;
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 4 && self.shouldShowPicker) { //where your picker row is
return CELL_VISIBLE_PLUS_INVISIBLE_PART;
} else if(indexPath.row == 4 && !self.shouldShowPicker) {
return return CELL_VISIBLE_PART;
} else {
return OTHER_CELLS_HEIGHT;
}
}
You might find my answer here useful which describes what you need to do.
Inline UIPicker Implementation
Essentially you create a custom cell containing a date picker, with optional buttons. You then add this cell below the cell when you edit it and remove it when finished. All explained in the link.
Here are tableView data source and delegate implementation, and I set the maximum selection is 5.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
if ([multiOptions count] > 5) {
[self tableView:tableView didDeselectRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected = NO;
//show alert
}
...
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[multiOptions removeObject:selectedOption];
...
}
but here comes a question, if the options exceed the limit, the first click on cell will work just fine. but second time click the cell will call
didDeselectRowAtIndexPath
again, that's not what i expected, and I tried
[tableView deselectRowAtIndexPath:indexPath animated:YES];
in didSelectRowAtIndexPath, it didn't work, can someone give me a hint, how to correct it? thanks.
You have to check this in tableView:willSelectRowAtIndexPath:
Have a look at the UITableView class reference
especially at the last sentence:
Return Value An index-path object that confirms or alters the selected
row. Return an NSIndexPath object other than indexPath if you want
another cell to be selected. Return nil if you don't want the row
selected.
So something like this will work within tableView:willSelectRowAtIndexPath::
if ([multiOptions count] > 5) return nil;
I have a UITableView with static cells created in a storyboard. When the user touches a cell, another cell should hide/show. This is identical to how it looks in the built-in calendar app in iOS 7. It's basically the same question as this: How does one implement a view that slides out like the date picker in calendar? but I have a static table view and not a dynamic, otherwise the solution would have worked. If i try it, en exception is thrown.
As it is now, I can show and hide the cell at the row, but it is without animation, which isn't acceptable. This is the code I use:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 2 && indexPath.row == 0)
{
NSIndexPath *dateIndexPath = [NSIndexPath indexPathForRow:1
inSection:2];
UITableViewCell *dateCell = [self.tableView cellForRowAtIndexPath:dateIndexPath];
if (dateCell.hidden)
{
dateCell.hidden = NO;
}
else
{
dateCell.hidden = YES;
}
}
}
hye, i think you should refer this link, it will help you regarding hiding cells,
UITableView set to static cells. Is it possible to hide some of the cells programmatically?
I have a main window which has two UITableViewControllers. On selecting the cell in first tableViewController (left pane), the tableViewContainer on the rightside gets populated. In the Voice over mode, I want that when the cell in the first tableViewController is selected, the first cell in the secondViewController gets focus so that the flick order starts from there. Please note that I have already tried the following two approaches:
1) Making the first cell of the second view controller, the firstResponder.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *outCell =(UITableCellView*) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (indexPath.row == 0)
{
[outCell becomeFirstResponder];
}
}
2) I also tried posting the layout changed notification given the cell as the first accessibility item.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *outCell =(UITableCellView*) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (indexPath.row == 0)
{
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, outCell);
}
}
None of the above approaches worked. Any idea what else can I try or what is wrong with the above approaches?
Please note that: I have read the View controller’s documentation (https://developer.apple.com/LIBRARY/IOS/#featuredarticles/ViewControllerPGforiPhoneOS/Accessibility/AccessibilityfromtheViewControllersPerspective.html - Moving the VoiceOver Cursor to a Specific Element). Tried posting LayoutChanged notifications as well as ScreenChangedNotifications in my sample as well as tried with the sample code (ListAdder - http://developer.apple.com/library/ios/#samplecode/ListAdder/Introduction/Intro.html, which also uses the NavigationController as we does), but the above did not work even with the sample.
Any idea why?
I solved this issue by posting the screen changed notification. The second parameter to that call is the table view's view that you want to select the first cell of.
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, [[self nextTableViewController] view];