IOS/Xcode/Storyboard: How can I segue from table row to modal view controller - xcode

This is a storyboard question, I think.
How do you create a segue from a table row to a new view controller?
Here is a picture of what it is supposed to look like.
I tried control dragging from the table to the view controller on the bottom but it does not take. If I embed the view controller in a navigator I can create a push segue but it does not work when you click on it.
This is the code I am using for that but first I would like to be able to replicate the storyboard in the picture. In any case I want the new view controller to pop up when you click on the table row.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Store Selection
[self setSelection:indexPath];
}
...
else if ([segue.identifier isEqualToString:#"updateToDoViewController"]) {
NSLog(#"segue to update");
// Obtain Reference to View Controller
UpdateTodoVC *vc = (UpdateTodoVC *)[segue destinationViewController];
// Configure View Controller
[vc setManagedObjectContext:self.managedObjectContext];
if (self.selection) {
// Fetch Record
NSManagedObject *record = [self.fetchedResultsController objectAtIndexPath:self.selection];
if (record) {
[vc setRecord:record];
}
// Reset Selection
[self setSelection:nil];
}
}
}
Thanks for any suggestions.

You can't ctrl+drag from a "Prototype" cell! You can only do that with static cells. So you have to ctrl+drag from the UITableViewController to the next view controller, give it an identifier of updateToDoViewController, and perform the segue programatically:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Store Selection
[self setSelection:indexPath];
// Perform the segue with identifier: updateToDoViewController
[self performSegueWithIdentifier:#"updateToDoViewController" sender:self];
}

Related

Customise table view cell in Mac app

Am trying to customise a table view cell. I have created a separate nib file for the cell. Have added a Table view Cell in it with the customised layout ( 2 textfields and an image). All of them have an outlet.
In,
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
MyCell *myCell = [tableView makeViewWithIdentifier:#"cellId" owner:self];
myCell.text1.stringValue = #"Foo";
myCell.text2.stringValue = #"Bar";
return myCell;
}
I know this is wrong implementation, but how can I load a new nib for a cell in a tableView ?
Are you using
- (void)registerNib:(NSNib *)nib forIdentifier:(NSString *)identifier
to register your cell xibs?

Adding elements with content not working in NStable view in cocoa

I created a tableview and added image & text table view cell designed it now i thought to add another label in the row and assigned some values to the label based on the rows but it is not working and one more problem i cant hide label when the label in cell while in view i can able to hide
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSImage *profile=[NSImage imageNamed:#"header_menubg.png"];
NSString *identifer=[tableColumn identifier];
if([identifer isEqualToString:#"Mainidentifier"])
{
cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
[cellView.textField setStringValue:[_homearray objectAtIndex:row]];
[cellView.imageView setImage:profile];
return cellView;
}
if(row==0)
{
_countLabel.stringValue=#"40";
[cellView.textField setHidden:YES];
}
return nil;
}
And even i tried to hide text field in some rows that too not working what may be the problem

NSViewController view's not released when used in a view based NSTableView

I have a view based NSTableView that uses the views of NSViewController subclasses (InspectorViewController) :
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// Get inspector view controller
InspectorViewController *inspectorViewController = [_inspectorViewControllers objectAtIndex:row];
// Return its view
return inspectorViewController.view;
}
All is ok except that the view of NSViewController is never released when I remove the view controller (which is well released).
If I use this code instead :
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// Get inspector view controller
InspectorViewController *inspectorViewController = [_inspectorViewControllers objectAtIndex:row];
// Return its view
NSTableCellView *view = [[NSTableCellView alloc] init] autorelease];
[view addSubview:inspectorViewController.view];
return view;
}
The view is correctly released when I remove the view controller.
Here is the code for removing a view controller :
- (void)inspectorViewControllerClosed:(InspectorViewController *)inspectorViewController {
// Get index of inspector view controller
NSUInteger index = [_inspectorViewControllers indexOfObject:inspectorViewController];
// Remove inspector view controller in array
[_inspectorViewControllers removeObject:inspectorViewController];
// Create index set
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
// Begin updates
[_tableView beginUpdates];
// Move row in table view
[_tableView removeRowsAtIndexes:indexSet withAnimation:NSTableViewAnimationEffectFade];
// End updates
[_tableView endUpdates];
}
I tried to reproduce the problem with a small sample code and I have the same problem, the view of view controller is only released when I don't return directly the view of view controller.
Anybody can help me please ?
Found, NSTableView retains view for reusing, it is correctly released with this code :
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// Get inspector view controller
InspectorViewController *inspectorViewController = [_inspectorViewControllers objectAtIndex:row];
// Disable reusing ?
[_inspectorViewController.view setIdentifier:nil];
// Return its view
return inspectorViewController.view;
}
You could use:
[inspectorViewController.view removeFromSuperview]
before removing the view controller.

How to present specific view controller when a cell is selected on iOS 6?

I am using Xcode 4.5.2, and iOS6.
I have 3 View Controllers, FirstViewController contains dynamic table view.
What I want is, when the first row is selected it takes the user to SecondViewController, and when the second row is selected to the ThirdViewController.
Below is the didSelectRowAtIndexPath method of my code
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *path = indexPath;
NSInteger theInteger = path.row;
switch (theInteger)
{
case 0:
//here i wanna present FirstViewController
break;
case 1:
//here i wanna present SecondViewController
break;
default:
break;
}
}
I am not sure if this is the best practice but this is all what I know. I hope I made it clear.

Using a different view in editing mode in a view based NSTableView

I have a NSTableView with a single NSTableCellView column that let's say, has an icon, name and an optional date.
When you edit a row, I want to replace the whole view with a simple NSTextField, and I will do some parsing to that text and extract that optional date, if present.
My question is, how would you implement this editing mechanism?
I tried returning a different view in the tableView:viewForTableColumn:row, something like:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
BOOL isSelected = [tableView isRowSelected:row];
if (isSelected)
{
NSView *view = [tableView makeViewWithIdentifier:#"editor" owner:self];
....snip....
return view;
}
else
{
TaskView *view = [tableView makeViewWithIdentifier:#"view" owner:self];
....snip....
return view;
}
}
and then whenever the selected row changes, trying to request a refresh on that row.
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
NSTableView *table = [aNotification object];
NSUInteger rowIndex = [table selectedRow];
[table reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:rowIndex]
columnIndexes:[NSIndexSet indexSetWithIndex:0]];
}
It doesn't quite work, and the code feels a bit dirty.
It must be a better way of doing this, and I can't seem to find in the docs or online.

Resources