Adding rows to a UITable from an array - objective-c

I have a UITable, and class who is it's datasource and delegate. The class has a property called logItemsArray which contains a number of objects which are used as data for a row (customer name, date added etc...) (the cells have a special class to which I pass these objects and it returns a cell).
I also have a method called loadNextLogEvents which loads some data from a web server and adds items into the logItemsAraay (it automatically loads the data for the next 100 rows).
This method is called once from the viewWillApear method and also from a button to load the next events.
The table view's dataSource numberOfRowsInTable returns the count of the logItemsArray.
The thing is: How do I "update" the table from the loadNextLogEvents to use the new data to load cells (the array is updated so both the numberOfRowsInTable and the cellForRowAtIndexPath method will return the desired result of new row, I just need to trigger them!

Use this:
[tableView reloadData];

Related

Update main view with change in tableView subview

I have a tableView sub view on my Main view controller. Here's a screenshot: https://www.dropbox.com/s/mp5r9eiv8tg3fmj/Screenshot%202015-02-03%2018.58.24.png?dl=0
I want to update the "remaining" labels when a row is either inserted or deleted. The problem comes when I call my "setLabels" method in controller didChangeObject: delete and insertRowAtIndexPaths. With this (https://www.dropbox.com/s/fcj8h3a03pfud6x/Screenshot%202015-02-04%2019.11.46.png?dl=0 ) implementation the labels only update when the next cycle comes. (If i add one, nothing will happen until I add another or delete one. I'm left with the label values that correspond to the tableView before the most recent change. Always one step behind)
I added an NSLog to verify the method is being called (it is) so I have no idea why my labels won't update till controllerWillChangeContent is called for a second time.
PS: my set labels method is simple. It uses my "getSum" method which gets the sum of an entities attribute and sets the label

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).

NSTableView binding with CoreData and use objectValueForTableColumn (DataSource delegate)

I have an NSTableView with two columns, First of them(With text cells) get it data through an NSArrayController.arrangedObjects.someKey bound to a Core Data entity. All is OK.
But second column is NSLevelIndicatorCell (Level Indocator Cell in IB's Object Library).
I set maximum value through an NSArrayController (someKey.#count).
And now want set the current value when some function run.
upd:
Also I know how set current value in "tableView:willDisplayCell:forTableColumn:row:" delegate method. But this is not enough. How update current value of second column in some function?
Send tableView's pointer(or rowPointer) to this Function, finding the required cell's pointer and use SetValue: method?
What is the alternative for tableView:objectValueForTableColumn:row:?
I do not want to change Entity and create any new numerical attributes like "the maximum number of subsections" or "total downloaded and processed at the moment."
The question: How do I make the tableview use its existing binding and set current value of NSLevelIndicatorCell?

How to implement numberOfRowsInSection to add a Button in UITableView?

I have a UITableView with gets populated from a backing array every time cellForRowAtIndexPath is called. The user can either go Back (via the nav bar) or they must submit the tableview's dataset to a server. Our GUI team wants the interface designed with a 'Done' button as the last cell in the table row in order to submit the dataset. Right now, numberOfRowsInSection is returning [myArray count] and I could conceivably alter the backing array with a "ghost" record for the done button, or simply return [myArray count] + 1 and catch the mis-matched array count in cellForRowAtIndexPath. Both ways, however, seem like trouble down the road. What's the best way to implement this?
Either put the button in the footer for the tableview, or mess with the row count. I've used both methods, just depends how much that last cell needs to look like the rest of the rows (or not look like them, in the case of using the footer)

How to populate the table view cell with values entered in another view controller?

I have a custom table view cell, which consists of four labels and an image view. I have another UIViewController which consists of two text fields and one text view.
When the values are entered in the text fields and text view of this controller, and the save button is pressed, the values must be saved on to my custom table view cells' text labels. How is this possible?
Update your data source object with the values and call reloadData function on tableview instance
- (void)reloadData
Call as below
[myTableView reloadData];
EDITED:
First : you could use the delegate concept of Objective-C,
Second: you could store the text value in user default using NSUserDefault and access that in other view controller,
See the very good blog post on NSUserDefault .
iPhone Programming Tutorial – Saving/Retrieving Data Using NSUserDefaults
Capture the Values entered in the Text Inputs and save them in different NSString variables. Set those values for the Next ViewControllers Input.
For example, declare a NSString in second View Controller Interface and #synthesize that. Before moving from first controller to second controller, set the value for that respective variable.