Getting the row of a NSButtonCell - objective-c

I have a NSTableView that contains a NSButtonCell in one of the columns. I can set up the action that is called when the button is clicked in Interface Builder fine, but I can't find anyway to determine which row in the table that the button exists in. Is there any way to do this?
Thanks. :)

The cell for a particular column is reused throughout the whole table so there isn't one cell per row by default. You can get the the row that was clicked on though in your action method by sending the clickedRow message to the cell's table view.
NSInteger clickedRowIndex = [tableView clickedRow];

Related

how to remove accessory from cellForRowAtIndexPath

i have the indexPath number... i need to remove the cell accessory from that cell based on the number in the variable i have?
NSInteger *removeAccessoryFrom = 3;
[studentTableView cellForRowAtIndexPath:removeAccessoryFrom].accessoryType = UITableViewCellAccessoryNone;
It doesn't work like that. You need to reload the table data (either for the whole table or for just this row) and, inside cellForRowAtIndexPath, set the accessoryType to UITableViewCellAccessoryNone conditionally. In other words, you keep a list, or have some other way of knowing, at every moment, and for the entire table, exactly what cells get exactly what accessory. That way, any time cellForRowAtIndexPath is called, you can configure this cell for this index path correctly.

Getting Selected value from NSTableView

I'm trying to build this application where the Column 'Package Name' is populated from Core data. The second column has NSButtonCell added in IB and the idea is each time a checkbox is selected I need to get the name of the corresponding zip file in the 'Package Name' column.
I tried something like this
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
NSTableView * tableView = [notification object];
NSInteger rowIndex = [tableView selectedRow];
NSTableColumn *column = [tableView tableColumnWithIdentifier:#"package-name"];
NSCell *cell = [column dataCellForRow:rowIndex];
NSLog(#"Found Value %#",cell.stringValue);
}
But the Value I get each time is different and inconsistent. For example if I select the 6ht checkbox (as shown in the screenshot), I expect to see 4.zip getting printed, but I get something else. How do I fix this ?
Both my Package Name and Installation Status columns are sortable.
Thanks
Don't consult the cells of the table. Instead, go straight to the data source or, if you're using bindings, the array controller.
First, in NSCell-based table views, there's typically a single cell object for the whole column. It is reused. It is configured for the first row and asked to draw in the appropriate location for that row. Then, it's configured for the second row and asked to draw in the appropriate location for the second row. Etc. So, retrieving the cell does not give you data for any particular row. The cell will have whatever properties it had the last time it was configured for a row, which may have been a completely different row than you're interested in.
(You could work around this by asking for the prepared cell for a row, using -[NSTableView preparedCellAtColumn:row:]. But that's really the roundabout way of doing things. The table view will just have to consult the data source in order to prepare the cell.)
If you're using an array controller, then your window/view controller should have an outlet to the array controller. Then, you can get the appropriate model object for a given row by calling:
object = [self.arrayController.arrangedObjects objectAtIndex:row];
If you're interested in the selected objects and your bindings are set up properly, you can just get the selected model object(s) directly from the array controller:
object = self.arrayController.selectedObjects[0];
Or:
for (object in self.arrayController.selectedObjects)
{
// ...
}
Note, though, that checking the checkbox in the second column is a distinct thing from selecting a row. Depending on how the table is configured, the user may be able to do either without the other. For example, they could select a row by clicking in the first column, which would not check the checkbox in the other column. They could also change the selection with the keyboard (e.g. down arrow).

Excess UITableView cells

I have a custom UITableViewCell that seems to appear multiple times on my table before I even start scrolling. It seems dequeueReusableCellWithIdentifier isn't working properly. Here's my cell:
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
... setup ...
}
When I call reloadRowsAtIndexPaths: on my tableView, for some reason it doesn't dequeue that cell and it recreates the new one. Then when I scroll back up and then down again, sometimes my first cell dequeues, sometimes the second cell dequeues. MyCustomCell is basically a textfield so depending on which one dequeues, the text has different data each time.
Is there something more I should be doing in my custom cell? Thanks in advance.
You mention reloadRowsAtIndexPaths: in your question, but the method is actually reloadRowsAtIndexPaths:withRowAnimation:. The fact that the reload can be animated matters. For example, to animate UITableViewRowAnimationLeft, the table view needs to slide out an old cell while sliding in a new cell. Both the old and new cells are on screen simultaneously, so the table view can't just use the same UITableViewCell object for both. That's why “it doesn't dequeue that cell and it recreates the new one”. It needs a new one because the old one is still visible.
You need to make sure that when you receive a tableView:cellForRowAtIndexPath: message, you fully initialize the cell (whether dequeued or newly created) to show the data for the requested row, and only the data for the requested row.
Also, you can't keep your own mapping from the row's indexPath to the cell, because (during animations) there may be two cells for the same row. Instead, if at any time you need to get the cell that is currently showing the data for a row, you need to ask the table view for the cell by sending it a cellForRowAtIndexPath: message.
From the code you show, it looks as if you only set up your cell's content when a new one is created. Since there's no way of knowing which cell you'll be given from the cache, you need to configure the cell whether it's new or one that's being re-used.

Get state of UISwitch in custom UITableViewCell's

I am using a custom tableviewcell in one of my view's. I have decided to add a UISwitch to this cell to enable the user to delete multiple row's at once.
Normally when the user selects a row and taps my delete button I have a UIAlert pop up for confirmation and after that the alert clickedButtonAtIndex method handles the outcome. In that method I get the indexpath ( [self.myTableView indexPathForSelectedRow] ) and delete (or not) accordingly.
So basically as the title states instead of using the indexPath to fall into my delete statement, I need to check to see if self.myTableView.myCell.mySwitch.on is TRUE. Can anyone point me in the right direction for doing this? I will need to iterate through all rows in the tableview and for each row where the state is on it needs to be removed.
Thanks.
you should iterate through your cells using a nested loop ( or a single loop if you have only one section) and cellForRowAtIndexPath:.if you see a cell's switch.on = TRUE save it's indexPath in an array by calling indexPathForCell:. after you're done iterating through your cells, call deleteRowsAtIndexPaths:withRowAnimation: with the array previously created.

Can I programmatically modify which UITableViewCell is selected?

Is there a way to move the highlighted tableviewcell programatically. If I wanted to highlight cell number 3 and then highlight cell number 4 instead, based on something the user did in the detail view of my split view controller, can I do this?
Is there a way I can get the indexpath for the row two below one that I have?
Thanks!
You can do this via the UITableView selectRowAtIndexPath:animated:scrollPosition: method.
In terms of getting the "next" row from the indexPath, you can simply use the indexPath.row property (as supplied from your UITableViewDelegate's tableView:didDeselectRowAtIndexPath: method) as the basis for the selected row.