Implementing copy method - copy whole row - objective-c

I am trying to copy the row of the NSTableView on clipboard. Here is my code:
- (void) copy:(id)sender
{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
// I get warning in the line bellow, unused variable changeCount
NSInteger changeCount = [pasteboard clearContents];
NSInteger row = [self.customersViewController.customersTableView selectedRow];
NSTableColumn *columnFirstName = [self.customersViewController.customersTableView tableColumnWithIdentifier:#"firstName"];
NSCell *cellFirstName = [columnFirstName dataCellForRow:row];
NSArray *objectsToCopy = #[[cellFirstName stringValue]];
// I get warning in the line bellow unused variable OK
BOOL OK = [pasteboard writeObjects:objectsToCopy];
}
This code works, and if I select the row in the NSTableView, the content of the firstName column of the selected row is indeed on the pasteboard (I can paste the value in text editor).
However this code have couple of issues:
1. I get 2 warnings as you can see from my comments.I rewrite the code to get rid of the warnings like this. Is anything wrong with the way how I re-write the code?
// warning one
NSInteger changeCount = 0;
changeCount = [pasteboard clearContents];
// warning 2
BOOL OK = NO;
OK = [pasteboard writeObjects:objectsToCopy];
In the code above I name specific which NSTableView I use
...self.customersViewController.customersTableViev....
However If the user switch the view, it may use some other NSTableView...how can I find out from which NSTableView the copy method should copy the row?
If I comment the line where I use specific NSTableView and try to use sender, my app crashes.
//NSInteger row = [self.customersViewController.customersTableView selectedRow];
NSInteger row = [sender selectedRow];
3.How could I write a loop to get all column names instead of specifically write them by hand one by one? I will not know which NSTableView is used anyway....
NSTableColumn *columnFirstName = [self.customersViewController.customersTableView tableColumnWithIdentifier:#"firstName"];

If you don't want the return value you can omit it.
To make you code table view independent you can use firstResponder of the window. Alternatively you can implement copy: in a cubclass of NSTableView. sender is the menu item.
NSTableView's property tableColumns is an array of NSTableColumn.
Here's what I did:
- (void)copy:(id)sender {
NSResponder *firstResponder = self.window.firstResponder;
if (firstResponder && [firstResponder isKindOfClass:[NSTableView class]]) {
NSTableView *tableView = (NSTableView *)firstResponder;
NSArrayController *arrayController = [[tableView infoForBinding:NSContentBinding] objectForKey:NSObservedObjectKey];
// create an array of the keys and formatters of the columns
NSMutableArray *keys = [NSMutableArray array];
for (NSTableColumn *column in [tableView tableColumns]) {
NSString *key = [[column infoForBinding:NSValueBinding] objectForKey:NSObservedKeyPathKey]; // "arrangedObjects.name"
if (key) {
NSRange range = [key rangeOfString:#"."];
if (range.location != NSNotFound)
key = [key substringFromIndex:range.location + 1];
NSFormatter *formatter = [[column dataCell] formatter];
if (formatter)
[keys addObject:#{#"key":key, #"formatter":formatter}];
else
[keys addObject:#{#"key":key}];
}
}
// create a tab separated string
NSMutableString *string = [NSMutableString string];
for (id object in [arrayController selectedObjects]) {
for (NSDictionary *dictionary in keys) {
id value = [object valueForKeyPath:dictionary[#"key"]];
if (value) {
NSFormatter *formatter = [dictionary objectForKey:#"formatter"];
if (formatter)
[string appendFormat:#"%#\t", [formatter stringForObjectValue:value]];
else
[string appendFormat:#"%#\t", value];
}
else
[string appendFormat:#"\t"];
}
[string replaceCharactersInRange:NSMakeRange([string length] - 1, 1) withString:#"\n"];
}
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard setString:string forType:NSPasteboardTypeString];
}
}

Related

TableView Unable to get DATA which are scrolled up or down

Whenever I try to get data from a cell it returns empty value, however I found that I'm unable to read data from cells that are Scrolled Up/Down of TableView
here is image clearing my situation
Here is the code I'm using for saving data:
NSMutableArray *arrTmp = [[NSMutableArray alloc] init];
for(int i = 0; i<[tblView numberOfRowsInSection:0]; i++){
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
FUCellView *cell = [tblView cellForRowAtIndexPath:indexPath];
UITextField *tfNo = (UITextField *)[cell viewWithTag:i + 100];
UIButton *btnTime = (UIButton *)[cell viewWithTag:i + 200];
UITextField *tfVisit = (UITextField *)[cell viewWithTag:i + 300];
UIButton *btnProvider = (UIButton *)[cell viewWithTag:i + 400];
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
if([tfNo.text length] > 0 && tfNo.text != nil){
NSString* str = [tfNo.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[theDictionary setObject:str forKey:#"number"];
}
else{
[theDictionary setObject:#"" forKey:#"number"];
}
if([btnTime.titleLabel.text length] > 0 && btnTime.titleLabel.text != nil){
NSString* str = [btnTime.titleLabel.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[theDictionary setObject:str forKey:#"time"];
}
else{
[theDictionary setObject:#"" forKey:#"time"];
}
if([tfVisit.text length] > 0 && tfVisit.text != nil){
NSString* str = [tfVisit.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[theDictionary setObject:str forKey:#"visit_type"];
}
else{
[theDictionary setObject:#"" forKey:#"visit_type"];
}
if([btnProvider.titleLabel.text length] > 0 && btnProvider.titleLabel.text != nil){
NSString* str = [btnProvider.titleLabel.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[theDictionary setObject:str forKey:#"provider"];
}
else{
[theDictionary setObject:#"" forKey:#"provider"];
NSLog(#"ProviderELse");
}
[arrTmp addObject:theDictionary];
UITableview is reusing the cells. So you can't get the values in for loop. This will return nil.
A better approach is to use a data model for storing the values.
For example: Create a data Model . Add some properties to it. And load cells with these properties. Now only you need to update the model object properties.
Store these model objects in an array.
Finally you can have the data ready at any time.
As i understand you, you should keep ur data somewhere else(u might have a model with data right?) and then appropriate to model set (in cellForRowAtIndexPath) UITableView. There is no need to set or get data direct from UITableView. Please read official documentation here
Here is nice lesson about Table View. Good luck.

function to check if there is any empty cell in a multicolumn nstableview not working

Hi i have written a function to check if there is any empty cell in a multicolumn nstableview like below.However this function is not working and always checks for the columns in the first row only.. any suggestions.and what is the right way to check for it
-(BOOL)isTableRowsContainingEmptyCells
{
for(int row=0;row< [[self.myarray arrangedObjects] count];row++)
{
NSTableColumn *column1 = [self.formFieldValues
tableColumnWithIdentifier:#"A"];
NSTableColumn *column2 = [self.formFieldValues
tableColumnWithIdentifier:#"B"];
NSCell *cell1 = [column1 dataCellForRow:row];
NSCell *cell2 = [column2 dataCellForRow:row];
NSLog(#"cell1 %# cell2 %#",cell1,cell2);
if([[cell1 stringValue] isEqualToString:#""]||[cell1 stringValue]==nil||[[cell2 stringValue] isEqualToString:#""]||[cell2 stringValue]==nil)
{
return YES;
}
}
return NO;
}
Try like this
NSCell *cll=[tableView preparedCellAtColumn:0 row:0];
NSString *str=[cll stringValue];
NSInteger len=[str length];
if (len ==0)
{
NSLog(#"NO data")
}

Add cell to UITableView

Can't handle simple problem - adding cells to UITableView.
I have single-view application, with added from Objects - Table View and simple NSArray (deseriliazed json from internet-grabbed data).
- (void) didLoadMusicList:(APIDownload *)request
{
NSLog(#"Music list loaded");
CJSONDeserializer *deserializer = [CJSONDeserializer new];
NSDictionary *dict = [deserializer deserializeAsDictionary:request.downloadData error:nil];
NSArray *response = [dict objectForKey:#"response"];
NSArray *audios = [response subarrayWithRange:NSMakeRange(1, response.count-1)];
for(int i = 0; i < audios.count; i++)
{
NSDictionary *audio = [audios objectAtIndex:i];
// add a cell?
}
}
So, how do I add cell for each element?
You need to implement the UITableViewDatasource on your view controller. You can use the same array to provide the data to the cells, and return them using the cellForRowAtIndexPath datasource method. You also need to provide with the count of cells on your tableView.
Check this documentation: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html

How can I remove all NSTableColumns from an NSTableView?

I am trying to implement a method to clear the NSTableView of all items AND columns. But I get a crash when I try to implement the following:
- (void)clearResultData
{
[resultArray removeAllObjects];
NSArray *tableCols = [resultTableView tableColumns];
if ([tableCols count] > 0)
{
id object;
NSEnumerator *e = [tableCols objectEnumerator];
while (object = [e nextObject])
{
NSTableColumn *col = (NSTableColumn*)object;
[resultTableView removeTableColumn:col];
}
}
[resultTableView reloadData];
}
Well, if it's any help you can remove all the columns like this:
- (void)removeAllColumns
{
while([[tableView tableColumns] count] > 0) {
[tableView removeTableColumn:[[tableView tableColumns] lastObject]];
}
}
The NSArray returned by tableColumns is changed by removeTableColumn. Do not assume it is unchanged.
Although it is returned as a non-mutable NSArray, the underlying implementation is being modified and it is not safe to use NSEnumerator with collections that are modified. In the while loop, you are sending a nextObject message to an enumerator whose current object was just deleted -- so bad things can happen!
Here's a more efficient implementation:
NSTableColumn* col;
while ((col = [[tableView tableColumns] lastObject])) {
[tableView removeTableColumn:col];
}
When there are no columns in the table view: tableColumns returns an empty array, lastObject on an empty array returns nil, col is assigned the value of nil, the condition is false and the while loop finishes.
[[[_tableView tableColumns] copy] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[_tableView removeTableColumn:obj];
}];
Here is a Swift implementation:
tableView.tableColumns.forEach({tableView.removeTableColumn($0)})

cancel button on search bar does not cancel correctly

I have a search bar, i can search now, but when I enter a text to search, and click the cancel button. It does not give me back my first stage, meaning full of the items in the table.
For example: I search the item with word: a, it gives me all the a items, yes, it is right now, but when i hit the cancel button, i want the programme gives me all the items exist, not just a items.
Here is the code: please help me out. Thank you so much.
- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar
{
searchBar.text = #"";
[searchBar resignFirstResponder];
letUserSelectRow = YES;
searching = NO;
self.tableView.scrollEnabled = YES;
NSLog(#"what text after cancel now: %#", searchBar.text);
[self.tableView reloadData];
}
- (NSMutableArray *) searchTableView {
NSString *searchText = searchBar.text;
NSLog(#"search text: %#", searchText);
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
NSMutableArray *tempArr = [[NSMutableArray alloc] init];
for (NSDictionary *dTemp in arrayData)
{
NSString *tempStr = [dTemp objectForKey:#"url"];
NSLog(#"sTemp string: %#",[ NSString stringWithFormat:#"%#", tempStr]);
NSRange titleResultsRange = [tempStr rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
{
NSLog(#"1 count :%d", [resultArray count]);
[resultArray addObject:dTemp];
NSLog(#"2 count :%d", [resultArray count]);
[tempArr addObject:resultArray];
[resultArray release];
resultArray = [NSMutableArray new];
}
}
if (resultArray != nil) {
[resultArray release];
}
return tempArr;
}
- (void)searchBar:(UISearchBar *)aSearchBar textDidChange:(NSString *)searchText
{
NSLog(#"what text after cancel now: %#", searchBar.text);
if([searchText length] > 0) {
[sortedArray removeAllObjects];
searching = YES;
letUserSelectRow = YES;
self.tableView.scrollEnabled = YES;
NSMutableArray *searchArray = [self searchTableView];
sortedArray = [[NSMutableArray alloc] initWithArray:searchArray copyItems:YES];
for (int i = 0; i<[sortedArray count]; i++) {
NSLog(#"this is the search array: %#", [[sortedArray objectAtIndex:i] class]);
}
NSLog(#"sorted array: %d", [sortedArray count]);
}
else {
searching = NO;
letUserSelectRow = NO;
self.tableView.scrollEnabled = NO;
}
[self.tableView reloadData];
}
You don't need to override any of UISearchBar methods to accomplish this. The new way of doing this relies on the UISearchDisplay controller instead (specifically on shouldReloadTableForSearchString).
Declare your view controller to conform to UISearchDisplayDelegate protocol, and keep two instance variables: your model as NSArray (all data) and a filtered array as NSMutableArray (a subset of your data). The code you presently have in "searchTableView" would filter the content of the model and place it into the filtered NSMutableArray. Then you would override the following UITableView methods: -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section and -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. In each, before returning, make a comparison to determine whether your tableView argument is equal to self.searchDisplayController.searchResultsTableView. If it is, the user is looking at the filtered list and your should use the content of the filtered NSMutableArray to create the view, otherwise, the user is looking at the whole data set and you should use the content of the NSArray that holds your model. Take a look at the following Apple code for a simple example of what I described:
http://developer.apple.com/library/ios/#samplecode/TableSearch/Introduction/Intro.html