Return to specific UITableView Cell - xcode

I have 2 views.
On the second I have a UITableview where I can choose the content to view in the first window.
When I return to the second view containing the UITableView, is there an easy way to return to the cell representing the content on the main page, rather than scrolling down to it?
I'm assuming if I know the record number of the array that created the UITableview, which is the same array that I'm populating the main page with, then I should just be able to jump to that index number on the UITableView. I can't figure out how to get the index number of the current record.
Sorry this is such a convoluted question.

if I understood you question correctly, to update the currently selected row in the previous tableView, you can invoke a method via delegate and perform something like
-(void) updateSelectedRow:(NSInteger)row {
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:0];
[self.tableView selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}

Related

Cannot get NSTableView to populate

In a Swift OSX Cocoa program, I cannot for the life of me get any text to display from a simple two-dimensional array into a two-column NSTableView. The Table View is inside a window in the xib. The dataSource outlet and delegate outlet, and tableView referencing outlet, are all connected to File's Owner. In the controller's .swift code file, I have declared
#IBOutlet weak var tableView: NSTableView!
and that is connected to the xib, so my problem doesn't seem to be the same as [this one]. I want to populate tableView with an array, but like this person I simplified it to populating with a string literal just to try and get any text at all. (The previous poster never seems to have gotten an answer, but he didn't post a code snippet so I will do that.) numberOfRowsInTableView returns 2, as it's currently supposed to. And here's tableView, excluding currently commented-out code:
func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject!
{
var newString = ""
newString = "Help!"
return newString
}
As far as I can tell, I should get a 2x2 table with each cell containing the word "Help!" But I don't. The table remains empty. Why?
EDIT in response to comment below: The commenter recommends I drag to Application under Objects, which I think is referring to something like this?
For whatever reason, I don't have that. My document outline for the .xib looks like this:
Maybe that's a clue as to what's wrong?

How to expand table view cell and show content beneath it?

I want to have the table view cell expand and show the buttons that I have laid out below the visible view when the cell isn't selected. So far I have managed to expand the cell so that the entire view shows with the buttons, but there is one major problem with this....
The buttons that are supposed to be revealed only when the cell is selected always appear in the table, and the table view looks really weird becuase for each cell there are buttons overlapping the next cell which were supposed to be hidden!
I have tired making a subclass of the cell, but I am stuck because when I override the setSelected method to show the button, all the buttons from all the cells show up, not just the one I clicked, Ill provide my code below.
I there an easier way to show the buttons without using a subclass? And if not how could I use the subclass in a way that wouldn't show all the buttons for all the cells?
Cell Subclass (.m file)
- (void)awakeFromNib {
// Initialization code
editHidden.hidden = YES;
removeHidden.hidden = YES;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
editHidden.hidden = NO;
removeHidden.hidden = NO;
// Configure the view for the selected state
}
Your table view delegate needs to implement tableView:heightForRowAtIndexPath:. Implement this method to return the correct height for your cell given the state that it is in (collapsed or expanded). When it comes time to expand your cell you should update your state and call [tableView beginUpdates]; [tableView endUpdates]; to have it recalculate and relayout the tableview.

Tell an Array Controller to refresh the table view after the array is edited?

Thanks for reading:
I've gotten stuck on this problem before and I know there is a solution, but after a a few hours of searching I haven't been able to find it again.
I'm using an NSForm and an "Add" button to create a new instance of a student and add that student to an array (sessionRoster). No problems here.
- (IBAction)addNewStudentButtonPressed:(id)sender {
//CREATE NEW STUDENT
Student *newStudent = [[Student alloc] init];
//ASSIGN VALUES FROM FORM
newStudent.firstName = [[studentForm cellAtIndex:0] stringValue];
newStudent.lastName = [[studentForm cellAtIndex:1] stringValue];
//ADD STUDENT TO ROSTER
[sessionRoster addObject:newStudent];
//CLEAR FORM
[[studentForm cellAtIndex:0] setStringValue:#""];
[[studentForm cellAtIndex:1] setStringValue:#""];
}
I'm using an Array controller to display the array in a tableview. No problems here. I have the bindings correct, because I've included some "dummy" students in the init, and they appear when the program is run (I wanted to post the screenshot, but I don't have enough reputation).
My question is, how can I make the table update each time the "add" button is pressed?
I do have the table as a property, and calling [tableView reloadData] doesn't work. I believe the solution before was some kind of "contentWillUpdate:YES" and "contentDidUpdate:YES" pairing, but I can't find that information again.
Thanks for your time!
J
Add the student object to the arrayController not to the array. The array controller "does the lifting."

NSTableView rowViewAtRow: always returning nil

I'm trying to display an NSPopover with additional information to a selected row of an NSTableView. For that I need to get a reference to the view representation of the selected row so I can "attach" my popover to it:
NSInteger row = [[self membersTableView] selectedRow];
NSTableRowView *aView = [[self membersTableView] rowViewAtRow: row makeIfNecessary: YES];
[self setQuickLookPopoverController: [QuickLookPopoverController showPopoverFor: anObject at: aView]];
In the above, the result of aView is always nil. According to Apple documentation, this is the method to obtain a view object, given a selected row. Especially the last sentence of the discussion is a bit weird:
Discussion This method will first attempt to return a currently
displayed view in the visible area. If there is no visible view, and
makeIfNecessary is YES, a prepared temporary view is returned. If
makeIfNecessary is NO, and the view is not visible, nil will be
returned.
In general, makeIfNecessary should be YES if you require a resulting
view, and NO if you only want to update properties on a view only if
it is available (generally this means it is visible).
An exception will be thrown if row is not within the numberOfRows. The
returned result should generally not be held onto for longer than the
current run loop cycle. It is better to call
rowViewAtRow:makeIfNecessary: whenever a view is required..
Why is this method always returning nil?
Solved it. I used NSTableView's method (NSRect) rectOfRow: (NSInteger) rowIndex which will give the frame of the required row.
Thanks for showing me the right direction, I had the same problem! I ended up doing the following, but note that I disabled selection of empty rows and that the following code is inside an IBAction:
[popOver showRelativeToRect:[sender bounds]
ofView:[sender rowViewAtRow:[sender selectedRow]
makeIfNecessary:YES]
preferredEdge:NSMaxXEdge];

How to add row-span in view-based NSTableView?

I'm trying to get a table layout as shown below working with view-based NSTableViews that have been introduced in Lion.
There were multiple approaches described for cell-based NSTableViews, e.g. Mimic the artwork column, but these don't really apply for view based table views.
The idea is that the table gets populated by an array of objects, in one (or more) column spanning rows indicating that objects share some data in common. numberOfRowsInTableView: returns to total number of items (19 in the case of the attached image).
Has anyone tried something like this?
Layout
I was able to do this by using two separate NSTableViews that have their NSScrollView's scrolling synchronized. To learn how to synchronize multiple scroll view's (with the exact subclass code) read Scroll View Programming Guide for Mac - Synchronizing Scroll Views
I have groupTableView that has a single column and shows the views that represent the group. I also have itemTableView that has columns representing the items of the group. I use the same delegate/datasource methods with if-else-statements to check which NSTableView is being used and respond accordingly with the proper # of rows, cell view, etc.. Additionally, I implement the following delegate method to adjust the group table view's row height to be equal the sum of the item row heights of the rows in the group:
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
if (tableView == self.groupTableView) {
NSUInteger firstRowIndexForGroup = ...;
NSUInteger lastRowIndexForGroup = ...;
CGFloat groupHeight = 0.0;
for (NSUInteger currentRowIndex = firstRowIndexForGroup; currentRowIndex <= lastRowIndexForGroup; currentRowIndex++) {
groupHeight += [self.itemTableView rectOfRow:lastRowIndexForGroup].size.height;
}
return groupHeight - [self.itemTableView intercellSpacing].height;
} else {
return self.itemTableView.rowHeight;
}
}
You must also call -noteHeightOfRowsWithIndexesChanged: every time the table view needs a group view because the height changes according to the number of rows in the group.
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
if (tableView == self.groupTableView) {
GroupRowView *view = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
// configure view
[tableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndex:row]];
return view;
} else {
ItemRowView *view = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
// configure view
return view;
}
}
I disabled row selection and horizontal and vertical scroll bars on the group table view. I also made the horizontal grid solid for both table views. Finally, I position the group table view directly next to the item table view with no gap in between so it appears as if it's simply another column in a single table view. The result is a flawless implementation that has zero lag between the table views. To the user, it appears as if it's a single table view.
Could you not use an NSOutlineView instead? It's specifically designed to have grouping support.

Resources