NSTableview and SearchField - objective-c

I have an action for searching in NSMutableArray with name searcharray, which is equal to array with which NSTableView are connected.And I wrote a method for adding items to TableView just by sending NSMutableArray to my method.
The problem is that after searching if I delete what I have typed in SearchField and SearchField is empty, the compiler doesn't feel that it's empty and my TableView getting empty too, but due my code it's have to be with data from searcharray.
Here is my code:
#import "myTableViewAppDelegate.h"
#implementation myTableViewAppDelegate
#synthesize window;
#synthesize searcharray;
-(void)addItems:(NSMutableArray *)ar{
array = ar;
[array retain];
[tableView reloadData];
}
- (int)numberOfRowsInTableView:(NSTableView *)tableView{
return (int)[array count];
}
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row
{
return [array objectAtIndex:row];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
NSMutableArray *animals = [[NSMutableArray arrayWithObjects:#"Cat", #"Dog", #"Fish", #"Squirrel", #"Bear", #"Turtle", nil] retain];
NSLog(#"%#",animals);
searcharray = animals;
[self addItems:animals];
}
- (IBAction)search:(id)sender {
//NSLog(#"%#",searcharray);
NSString *filter = [[NSString alloc] initWithString:[search stringValue]];
NSMutableArray *result = [[NSMutableArray alloc] init];
if (filter != nil) {
for (NSString *item in searcharray) {
if ([item rangeOfString:[search stringValue]].location !=NSNotFound ) {
NSLog(#"Item %# contains %#",item,[search stringValue]);
[result addObject:item];
}
}
}
else{
result = searcharray;
}
NSLog(#"%#",result);
[self addItems:result];
[result release];
[filter release];
}
#end

Fixed it.
if (filter != nil || filter.length != 0)
I don't know why, but just checking variable equal to NULL wasn't enough....

Related

prepareForSegue not passing correct PFFile

When passing a PFFile into new view I am either getting no image or the wrong image.
Here is prepareForSegue method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ShowWine"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PFObject *object = [self.objects objectAtIndex:indexPath.row];
PFFile *file = [object objectForKey:#"image"];
[[segue destinationViewController] setFile:file];
}
}
And the rest of my relevant implementation file, I just think that I am not getting to the right row in the right section?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.sections.allKeys.count;
}
- (NSString *)wineTypeForSection:(NSInteger)section {
return [self.sectionToWineTypeMap objectForKey:[NSNumber numberWithInt:section]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *wineType = [self wineTypeForSection:section];
NSArray *rowIndecesInSection = [self.sections objectForKey:wineType]; return rowIndecesInSection.count;
}
- (void)objectsDidLoad:(NSError *)error {
[super objectsDidLoad:error];
[self.sections removeAllObjects];
[self.sectionToWineTypeMap removeAllObjects];
NSInteger section = 0;
NSInteger rowIndex = 0;
for (PFObject *object in self.objects) {
NSString *wineType = [object objectForKey:#"wineType"];
NSMutableArray *objectsInSection = [self.sections objectForKey:wineType];
if (!objectsInSection) {
objectsInSection = [NSMutableArray array];
[self.sectionToWineTypeMap setObject:wineType forKey:[NSNumber numberWithInt:section++]];
}
[objectsInSection addObject:[NSNumber numberWithInt:rowIndex++]];
[self.sections setObject:objectsInSection forKey:wineType];
}
}
- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath {
NSString *wineType = [self wineTypeForSection:indexPath.section];
NSArray *rowIndecesInSection = [self.sections objectForKey:wineType];
NSNumber *rowIndex = [rowIndecesInSection objectAtIndex:indexPath.row];
return [self.objects objectAtIndex:[rowIndex intValue]];
}
What you should try doing is the following :
Create an instance variable (PFFile *aFile)
In didSelectRowAtIndexPath:, call your objectAtIndexPath: method, and set aFile with the returning value
Still in didSelectRowAtIndexPath:, call your performSegueWithIdentifier: and in prepareForSegue:, just set the destination file to your var : [[segue destinationViewController] setFile: aFile];.
Try this, and see if it works better. If not, please let me know !

need to save contents of table view as txt file and the reload it later

i need to be able to save the contents of my table view which is given data by an NSMutableArray to a txt file and then need to reopen that file automatically when the window containing the table view. i am making an application for mac
thanks
this is the code for the data source:
#import "tableViewData.h"
#import "Customer.h"
#implementation tableViewData
-(id) init
{
self = [super init];
if (self) {
list = nil;
filepath = #"/Users/Gautambir/Desktop/CustomerNames.txt";
if ([[NSFileManager defaultManager]fileExistsAtPath:filepath]) {
list = [[NSMutableArray alloc] initWithContentsOfFile:filepath];
}
else
list = [[NSMutableArray alloc]initWithObjects:name,memberNumber ,nil];
[list writeToFile:filepath atomically:YES];
}
return self;
}
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
return [list count];
}
-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
Customer *Customer = [list objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
return [Customer valueForKey:identifier];
}
-(void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
Customer *Customer = [list objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
[Customer setValue:object forKey:identifier];
}
-(void)save{
[list writeToFile:filepath atomically:YES];
}
-(IBAction)add:(id)sender{
[list addObject:[[Customer alloc]init]];
[tableView reloadData];
NSLog (#"array:%#",list);
}
-(IBAction)remove:(id)sender{
NSInteger row = [tableView selectedRow];
if (row != -1) {
[list removeObjectAtIndex:row];
}
[tableView reloadData];
}
-(void)dealloc
{
[super dealloc];
}
#end
and this is the .m file for the customer object class:
#import "Customer.h"
#implementation Customer
#synthesize name;
#synthesize memberNumber;
-(id) init
{
self = [super init];
if(self) {
name = #"Test";
int i = arc4random()%1000000000000000000;
if (i<0) {
memberNumber = i*-1;
}
else
memberNumber = i;
}
return self;
}
-(void)dealloc
{
[name release];
[super dealloc];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self)
{
name = [[aDecoder decodeObjectForKey:#"name"]retain];
memberNumber = [aDecoder decodeIntForKey:#"memberNumeber"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:name forKey:#"name"];
[aCoder encodeInt:memberNumber forKey:#"memberNumber"];
}
#end
strong text
im sorry to post another answer - i misread the tags, and my first answer was relying to iOS.
this is the way to do it in OSX:
saving
NSMutableArray *array = ... //your array
[array addObject:...];
[array addObject:...];
[array addObject:...];
...
// then use an NSData to store the array
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSString *path = #"/Users/User/path...";
[data writeToFile:path options:NSDataWritingAtomic error:nil];
retrieving
NSMutableArray *archive = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
sebastian

Implementing drag and drop in NSTableView

Can anyone help me to implement drag and drop in an NSTableView? I used this code below, but these methods are not getting called during execution.
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
{
// Copy the row numbers to the pasteboard.
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArray arrayWithObject:#".gif"] owner:self];
[pboard setData:data forType:#".gif"];
return YES;
}
- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)op
{
// Add code here to validate the drop
if (row > [ m_imageArray count])
return NSDragOperationNone;
if (nil == [info draggingSource]) // From other application
{
return NSDragOperationNone;
}
else if (self == [info draggingSource]) // From self
{
return NSDragOperationNone;
}
else // From other documents
{
[tv setDropRow: row dropOperation: NSTableViewDropAbove];
return NSDragOperationCopy;
}
NSLog(#"validate Drop");
return NSDragOperationCopy;
}
- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
NSPasteboard* pboard = [info draggingPasteboard];
NSData* rowData = [pboard dataForType:#".gif"];
NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
NSInteger dragRow = [rowIndexes firstIndex];
// Move the specified row to its new location...
}
Here is an example
#import "TableViewController.h"
#import "Person.h"
#define MyDataType #"MyDataType"
#implementation TableViewController {
NSMutableArray *list;
NSInteger sourceIndex;
}
#synthesize tableView;
-(void)awakeFromNib {
[tableView registerForDraggedTypes:[NSArray arrayWithObjects:MyDataType, nil]];
list = [[NSMutableArray alloc] init];
Person *person = [[Person alloc] initWithName:#"Newton" age:64];
[list addObject:person];
person = [[Person alloc] initWithName:#"Archimedes" age:74];
[list addObject:person];
person = [[Person alloc] initWithName:#"Euler" age:44];
[list addObject:person];
person = [[Person alloc] initWithName:#"Poincare" age:24];
[list addObject:person];
person = [[Person alloc] initWithName:#"Gauss" age:34];
[list addObject:person];
}
-(void)reArrange:(NSMutableArray *)array sourceNum:(NSInteger)sourceNum destNum:(NSInteger)destNum {
Person *person = list[sourceNum];
[list insertObject:person atIndex:destNum];
if (sourceNum < destNum) {
[list removeObjectAtIndex:sourceNum];
} else {
[list removeObjectAtIndex:sourceNum+1];
}
[tableView reloadData];
}
#pragma mark - Table
// how many rows are there in the table?
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tv {
return list.count;
}
// What object should I show in a particular cell?
-(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
Person *person = list[row];
NSString *identifier = [tableColumn identifier];
return [person valueForKey:identifier];
}
// Should I accept the drag with the rows specified by rowIndexes? If YES then place the data on the provided paste board.
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArray arrayWithObject:MyDataType] owner:self];
[pboard setData:data forType:MyDataType];
sourceIndex = [rowIndexes firstIndex];
return YES;
}
// What kind of drag operation should I perform?
- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id )info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)op {
return op == NSTableViewDropAbove; // Specifies that the drop should occur above the specified row.
}
// The mouse button was released over a row in the table view, should I accept the drop?
- (BOOL)tableView:(NSTableView*)tv acceptDrop:(id )info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)op {
[self reArrange:list sourceNum:sourceIndex destNum:row]; // let the source array reflect the change
return YES;
}
#end
You need to declare a custom drag type for your table view and then call registerForDraggedTypes: with your custom type. Otherwise, as you have noticed, none of these methods will get called.
Drag and Drop NSTableview using core data
Register table-view object for drag and drop:-
[tblCategory registerForDraggedTypes:[NSArray arrayWithObject:#"public.text"]];
Drag and drop Delegate methods:-
- (id <NSPasteboardWriting>)tableView:(NSTableView *)tableView pasteboardWriterForRow:(NSInteger)row
{
Category *category = (Category *)[self.arrCategoryList objectAtIndex:row];
NSString *identifier = category.categoryname;
NSPasteboardItem *pboardItem = [[NSPasteboardItem alloc] init];
[pboardItem setString:identifier forType: #"public.text"];
return pboardItem;
}
- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation
{
if(dropOperation == NSTableViewDropOn)
{
NSPasteboard *p = [info draggingPasteboard];
NSString *title = [p stringForType:#"public.text"];
Category* category ;
NSInteger srcIndex;
for (srcIndex = 0; srcIndex < [_arrCategoryList count]; srcIndex++)
{
category = [_arrCategoryList objectAtIndex:srcIndex];
if ([category.categoryname isEqualToString:title])
{
break;
}
category = nil;
}
if(!category)
{
// Not found
return NO;
}
[_arrCategoryList removeObjectAtIndex:srcIndex];
[_arrCategoryList insertObject:category atIndex:row];
[tblCategory reloadData];
return NSDragOperationMove;
}
return NSDragOperationNone;
}
- (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id <NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation
{
return YES;
}
I usually observe this kind of error when I forgot to hook up the dataSource to the NSTabeView in IB, i.e. the class implementing the tableView:writeRowsWithIndexes:toPasteboard: method of the NSTableViewDataSource.
You can register for draggedTypes for an NSMutableArray or NSMutableDictionary or any other object.Following Code snippet is for a NSMutableArray.
[tableView registerForDraggedTypes:[NSArray arrayWithObject:#"NSMutableArray"] ];
You need to declare a registerForDraggedTypes method in awakFromnib
like this.
[table_view registerForDraggedTypes:[NSArray arrayWithObjects:BasicTableViewDragAndDropDataType, nil]];
I have to recommend both this excellent Red Sweater blog post
http://www.red-sweater.com/blog/274/a-moveable-beast
It provides an NSArrayController sub-class which will enable drag and drop re-ordering of table items, and if you want to support dragging outside of the tableview with your objects, we've just written up a neat and simple addition to that class:
http://www.rwe-uk.com/blog/comments/nstableview_drag_and_drop_with_bindings_and_nsarraycontroller
It builds on the original post

Invalid argument type to unary expression

I have this program with a tableView as my first view. I have also implemented (or at least tried to) a search bar on top of the view. Have used several hours to search for a solution, but without positive results.
#import "FirstViewController.h"
#import "NSDictionary-MutableDeepCopy.h"
#implementation FirstViewController
#synthesize listData, table, search, allNames, names, keys;
#pragma mark -
#pragma mark Custom Methods
- (void)resetSearch {
NSMutableDictionary *allNamesCopy = [self.allNames mutableDeepCopy];
self.names = allNamesCopy;
[allNamesCopy release];
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
[keyArray addObjectsFromArray:[[self.allNames allKeys]
sortedArrayUsingSelector:#selector(compare:)]];
self.keys = keyArray;
[keyArray release];
}
-(void)handleSearchForTerm:(NSString *)searchTerm {
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];
for (NSString *key in self.keys) {
NSMutableArray *array = [names valueForKey:key];
NSMutableArray *toRemove = [[NSMutableArray alloc] init];
for (NSString *name in listData) {
if ([name rangeOfString:searchTerm
options:NSCaseInsensitiveSearch].location == NSNotFound)
[toRemove addObject:name];
}
if ([array count] == [toRemove count])
[sectionsToRemove addObject:key];
[array removeObjectsInArray:toRemove];
[toRemove release];
}
[self.keys removeObjectsInArray:sectionsToRemove];
[sectionsToRemove release];
[table reloadData];
}
- (void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:#"sortednames" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.names = dict;
self.allNames = dict;
[dict release];
[self resetSearch];
[table reloadData];
[table setContentOffset:CGPointMake(0.0, 44.0)animated:NO];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
NSArray *array = [[NSArray alloc] initWithObjects:
// A larger amount of objects here.
self.listData = array;
[array release];
[super viewDidLoad];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.listData = nil;
self.table = nil;
self.search = nil;
self.allNames = nil;
self.names = nil;
self.keys = nil;
}
- (void)dealloc {
[listData release];
[search release];
[table release];
[allNames release];
[keys release];
[names release];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ([keys count] > 0) ? [keys count] : 1;
}
- (NSInteger)tableView:(UITableView *)aTableView
numberOfRowsInSection: (NSInteger)section {
return [self.listData count];
if ([keys count] == 0)
return 0;
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
return [nameSection count];
}
- (UITableViewCell *) extracted_method: (UITableViewCell *) cell {
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *sectionsTableIdentifier = #"sectionsTableIdentifier";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:
sectionsTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: sectionsTableIdentifier] autorelease];
}
cell.backgroundColor = [UIColor clearColor];
cell.textColor = [UIColor whiteColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.text = [nameSection objectAtIndex:row];
[self extracted_method: cell].text = [listData objectAtIndex:row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
if ([keys count] == 0)
return nil;
NSString *key = [keys objectAtIndex:section];
return key;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return keys;
}
#pragma mark -
#pragma mark Table View Delegate Methods
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[search resignFirstResponder];
return indexPath;
}
#pragma mark -
#pragma mark Search Bar Delegate Methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSString *searchTerm = [searchBar text];
[self handleSearchForTerm:searchTerm];
}
- (void)searchBar:(UISearchBar *)searchBar
textDidChange:(NSString *)searchTerm {
if ([searchTerm length] == 0) {
[self resetSearch];
[table reloadData];
return;
}
[self handleSearchForTerm:searchTerm];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
search.text = #"";
[self resetSearch];
[table reloadData];
[searchBar resignFirstResponder];
}
#end
Ok guys. My problem is that this doesnt get the search function to work. In addition I receive siginal SIGABRT at this line:
NSString *key = [keys objectAtIndex:section];
So I need help with two things:
1: I need to get that SIGABRT away.
Error log message: * Terminating app due to uncaught exception
'NSRangeException', reason: '* -[NSMutableArray objectAtIndex:]:
index 0 beyond bounds for empty array'
That is I don't store any data in keys. how would I do that? and what would I store?
2: Want the search function to search in my listData array!
Thanks in advance - hope u can help!
You have not finished your sectionIndexTitlesForTableView: method. Right now it is:
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return keys;
There is no closing }, so the compiler thinks everything after that is still part of that method. When you try to define the next method, use use - (NSIndexPath *) to indicate that it is an instance method which returns NSIndexPath*, but the compiler thinks you are trying to subtract something.
The solution is simple: Add the } to the end of sectionIndexTitlesForTableView:.
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return keys;
}

How do I access the data source in my NSTableView to allow editing of a row in the table?

I'm trying to access the array (data source) in my NSTableView to allow replacing the string that is in the row with a new string.The app consist of an NSTextfield for data entry and a button to add the entry so that it's displayed in the NSTableView. What I want is to be able to double click the NSTableView and replace what ever string is there with a new string, but I'm not sure how to do this.Here is my code so far
#implementation AppController
-(id)init
{
[super init];
[tableView setDataSource:self];
[tableView setDelegate:self];
array = [[NSMutableArray alloc ] init];
//NSLog(#"this is my delegate %#",[tableView delegate]);
return self;
}
-(IBAction)addItem:(id)sender
{
inputString = [textField stringValue];
[array addObject:inputString];
[tableView reloadData];
return;
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [array count];
}
- (id) tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
//NSLog(#"this is the object %#",[array objectAtIndex:rowIndex]);
return [array objectAtIndex:rowIndex];
}
-(IBAction) replaceItem:(id)sender
{
NSString *newString = [[NSString alloc]init];
NSLog(#"The selected row %d",[tableView selectedRow]);
newString = [textField stringValue];
[array addObject:newString];
[array replaceObjectAtIndex:[tableView selectedRow ] withObject: newString];
NSLog(#"this is the new sting %#",newString);
[tableView reloadData];
}
#end
I think you're looking for these datasource and delegate methods:
-tableView:setObjectValue:forTableColumn:row:
-tableView:shouldEditTableColumn:row: