Nstableview Get Indexpath Having the Cell

NSTableView get indexPath having the cell

In macOS use a view based table view, create an standard IBAction for the button and get the row with row(for view:

func row(for view: NSView) -> Int

Returns the index of the row for the specified view.

A view based table view is much more versatile and much easier to handle.

NSTableView get what cell is being edited with keyboard

When a text field in a cell is focused then the first responder is the field editor of the text field. Use row(for:) and column(for:) of NSTableView to get the row and column.

if let view = window.firstResponder as? NSView {
let column = tableView.column(for: view)
let row = tableView.row(for: view)
}

NSTableView start cells from bottom

From what I think you want, you don't need to use CGAffineTransform .

You just need to organise your data set as you wish and populate the cells accordingly.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath) as? CustomTableViewCell else {
return UITableViewCell()
}
cell.updateTitle(customCellTitles[indexPath.row])
return cell
}

instead of having your data set as:

customCellTitles = ["a","b","c","d"]

make it:

customCellTitles = ["d","c","b","a"]

In addition to this, I would go for the following setup of your layout:

Sample Image

Notice there is no top constraint.

Next, create an outlet from your table view height constraint and update it when you have your data set ready.

In the simple scenario where your cell size is the same (or independent from the content you display) update your height constraint based on the cell size:

tableViewHeightConstraint.constant = cellSize * numberOfCells
view.setNeedsLayout()

which method is called when selecting a cell in the NSTableView in Cocoa OS X?

What is tableViewController object? Only NSTableView instances respond to selectedRow. You can get current table view (the one that sent the notification) from notification's object property:

Objective-C:

-(void)tableViewSelectionDidChange:(NSNotification *)notification{
NSLog(@"%d",[[notification object] selectedRow]);
}

Swift:

func tableViewSelectionDidChange(notification: NSNotification) {
let table = notification.object as! NSTableView
print(table.selectedRow);
}

How to get the indexpath.row when an element is activated?

giorashc almost had it with his answer, but he overlooked the fact that cell's have an extra contentView layer. Thus, we have to go one layer deeper:

guard let cell = sender.superview?.superview as? YourCellClassHere else {
return // or fatalError() or whatever
}

let indexPath = itemTable.indexPath(for: cell)

This is because within the view hierarchy a tableView has cells as subviews which subsequently have their own 'content views' this is why you must get the superview of this content view to get the cell itself. As a result of this, if your button is contained in a subview rather than directly into the cell's content view, you'll have to go however many layers deeper to access it.

The above is one such approach, but not necessarily the best approach. Whilst it is functional, it assumes details about a UITableViewCell that Apple have never necessarily documented, such as it's view hierarchy. This could be changed in the future, and the above code may well behave unpredictably as a result.

As a result of the above, for longevity and reliability reasons, I recommend adopting another approach. There are many alternatives listed in this thread, and I encourage you to read down, but my personal favourite is as follows:

Hold a property of a closure on your cell class, have the button's action method invoke this.

class MyCell: UITableViewCell {
var button: UIButton!

var buttonAction: ((Any) -> Void)?

@objc func buttonPressed(sender: Any) {
self.buttonAction?(sender)
}
}

Then, when you create your cell in cellForRowAtIndexPath, you can assign a value to your closure.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! MyCell
cell.buttonAction = { sender in
// Do whatever you want from your button here.
}
// OR
cell.buttonAction = buttonPressed(closure: buttonAction, indexPath: indexPath) // <- Method on the view controller to handle button presses.
}

By moving your handler code here, you can take advantage of the already present indexPath argument. This is a much safer approach that the one listed above as it doesn't rely on undocumented traits.

NSTableView get selected row

You won't be using UIKit in a Mac application, instead you will use AppKit's NSTableView.

Create a column:

NSTableColumn * column = [[NSTableColumn alloc] initWithIdentifier:@"SomeColumnId"];
column.headerCell.stringValue = NSLocalizedString(@"Column Name", nil);
[_table addTableColumn:column];

Set up the table:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return _someArray.count;
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
if ( [tableColumn.identifier isEqualToString:@"SomeColumnId"] )
{
return @"Some cell string"; // or _someArray[row][@"some_string_key"]
}

return nil;
}

-(void)tableViewSelectionDidChange:(NSNotification *)notification
{
if ( [[notification object] selectedRow] == 0 )
{
/// first row was selected.
}
}

Xcode Swift. How to programmatically select Cell in view-based NSTableView

I'd googled this for two days solid before asking the question here. But now almost immediately after posting the question I managed to get it working.
In fact it's so simple I can't believe it took so long to figure it out.

// Assuming we want to programmatically select Column 4, Row 5
myTableView?.editColumn(4, row: 5, withEvent: nil, select: true)

On my travels I found dozens of people asking this question but no simple answers. Or answers that actually worked with NSTableView rather than UITableView and when tableView is 'view based' rather than cell based. So I hope me posting this here will help some of those people.



Related Topics



Leave a reply



Submit