NSTableView not displaying data - cocoa

Here's my code:
test.h
#interface testAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
IBOutlet NSTableView *test;
NSMutableArray *internalArray;
}
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic, retain) NSTableView *test;
-(id) initWithArray: (NSArray*) objects;
-(int)numberOfRowsInTableView:(NSTableView *)aTableView;
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
-(void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
test.m (top pieces missing for simplicity's sake)
#synthesize window;
#synthesize test;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
PSClient *client = [PSClient applicationClient];
NSURL *url = [NSURL URLWithString:#"http://www.apple.com/main/rss/hotnews/hotnews.rss"];
PSFeed *feed = [client addFeedWithURL:url];
internalArray = [NSMutableArray array];
// Retrieve the entries as an unsorted enumerator
NSEnumerator *entries = [feed entryEnumeratorSortedBy: nil];
PSEntry *entry;
// Go through each entry and print out the title, authors, and content
while (entry = [entries nextObject]) {
[internalArray addObject:entry.title];
//NSLog(#"Entry Title:%#", entry.title);
//NSLog(#"Entry Authors:%#", entry.authorsForDisplay);
[test reloadData];
//NSLog(#"Entry Content:%#", entry.content.plainTextString);
}
}
-(int)numberOfRowsInTableView:(NSTableView *)aTableView{
NSLog(#"%#", [internalArray count]);
return [internalArray count];
}
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
NSString *string = [internalArray objectAtIndex:rowIndex];
NSLog(#"%#", string);
// when I debug, I get same pointers with invalid data
// each object has "name" message
// this following line gives invalid pointer and
// it crashes
return string;
}
-(void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
NSString *string = [internalArray objectAtIndex:rowIndex];
NSLog(#"%#", string);
// when I debug, I get same pointers with invalid data
// each object has "name" message
// this following line gives invalid pointer and
// it crashes
return string;
}
#end
In IB I have the tableview (test) hooked up to the app delegate: test, datasource, and delegate
For some reason the table isn't displaying the data, and when I call [test reloadData]; the app crashes

Number one with a bullet - you don't retain internalArray after you create it. Use one of:
internalArray = [[NSMutableArray] alloc] init];
internalArray = [[NSMutableArray array] retain];
instead of what you currently have:
internalArray = [NSMutableArray array];
Also, I think you probably don't want %# for the format in this method:
-(int)numberOfRowsInTableView:(NSTableView *)aTableView{
NSLog(#"%#", [internalArray count]);
return [internalArray count];
}
That code will certainly (well, often) cause a crash when you do [tableView reloadData].
In applicationDidFinishLaunching:, you don't need to call reloadData every time you add an object to internalArray. Just do it once at the end of that loop.
Your implementation of setObjectValue:forTableColumn:row: also doesn't make sense. It should look more like this:
-(void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
NSLog(#"%#", anObject);
[internalArray replaceObjectAtIndex:rowIndex withObject:anObject];
}
Hopefully those are some ideas to get you started.

Related

Performing a Segue from a table with searchbar to an UIImage in Detail View in xcode

I'm pretty new to xcode so I'm having trouble performing this task. I've created a table with a search bar with names that are passed to a detail view with a UILabel that shows the corresponding name of the cell clicked. The search bar works and it filters the results. I used this tutorial to help me with it:
http://www.appcoda.com/how-to-add-search-bar-uitableview/
Now I want to have an image in the detail view instead of a UILabel, that corresponds to each of the cells but I'm having trouble figuring out how to do that. Here is the code I'm working with:
TableViewController.h:
#interface SearchViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
#property (nonatomic, strong) IBOutlet UITableView *tableView;
TableViewController.m:
#interface SearchViewController ()
#end
#implementation SearchViewController {
NSArray *cards;
NSArray *searchResults;}
#synthesize tableView = _tableView;
-(void)viewDidLoad
{
[super viewDidLoad];
cards = [NSArray arrayWithObjects:
#"Snivy",
#"Servine",
#"Serperior",
#"Tepig",
#"Pignite",
#"Emboar",
#"Oshawott",
#"Dewott",
#"Samurott", nil];
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"SELF contains[cd] %#",
searchText];
searchResults = [cards filteredArrayUsingPredicate:resultPredicate];}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [cards count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SearchCardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [cards objectAtIndex:indexPath.row];
}
return cell;}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"ShowSearchCard"]) {
SearchCardViewController *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = nil;
if ([self.searchDisplayController isActive]) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
destViewController.cardName = [searchResults objectAtIndex:indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
destViewController.cardName = [cards objectAtIndex:indexPath.row];
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
[self performSegueWithIdentifier: #"ShowSearchCard" sender: self];
}
}
UIViewController.h:
#property (strong, nonatomic) IBOutlet UILabel *cardLabel;
#property (strong, nonatomic) NSString *cardName;
#property (strong, nonatomic) NSArray *searchCardDetail;
UIViewController.m:
#implementation SearchCardViewController
#synthesize cardLabel;
#synthesize cardName;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
cardLabel.text = cardName;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
So in detail, the "cards" are the names in the table. Right now, it segue to a UILabel of the name of the card and I would like to have it instead, segue to the corresponding image of the card in the regular table and filtered table when searched. I appreciate your time and help! Thanks!
I'm not sure if I understanded your question correctly, but I would use one of these three approaches:
1st:
Easiest way is to follow this tutorial
2nd: Create custom cell which you will use instead of default cell. To get you on track, you can check this good tutorial How to create custom cell. In your custom cell, you'll declare additional variable, UIImage, which will or won't be displayed in your table view. Point is, you can send it in prepareForSegue method to your detailViewController.
3rd: Create NSDictionary where you will have values (images of your items) for keys (name of item). Then, pass it in prepareForSegue to your NSDictionary in detailViewController. After that, just assign your UIImage to UIImageView in your detailViewController based on what name of item did you receive. (So you'll still be sending name and then detailImage = [yourdict objectForKey:myItemName];)
I'm writing this on my windows laptop, because I'm not at work right now (that's where I have my mac mini), so there may be some syntax errors in my answer :)

NSTableView data source editing

I'm just a beginner in Cocoa developing for Snow Leopard and I have problem with editing values in data array that displayed in NSTableView.
I tried to edit some property of my object in -tableView:setObjectValue:forTableColumn:row: and I have EXC_BAD_ACCESS after this.
My NSTableView contains one column with NSButtonCell cells and this column have identifier 'checked'.
My code is simple and looks like this:
#interface MyObj: NSObject {
BOOL checked;
}
#property (assign) BOOL checked;
#end
#imlpementation MyObj
#synthesize checked;
#end
#interface MyAppDelegate: NSObject <NSApplicationDelegate, NSTableViewDelegate, NSTableViewDataSource> {
NSMutableArray *data;
NSTableView *table;
}
#property (assign) IBOutlet NSTableView *table;
- (NSInteger) numberOfRowsInTableView:(NSTableView *)aTableView;
- (id) tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex;
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex;
#end
#implementation MyAppDelegate
#synthesize table;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
data = [[NSMutableArray array] retain];
// some code that add few objects to data
// each object is instance of MyObj and I call alloc+init+retain for each
// ...
[table reloadData];
}
- (void)dealloc {
[data release];
[super dealloc];
}
- (NSInteger) numberOfRowsInTableView:(NSTableView *)aTableView {
return (data == nil ? 0 : [data count]);
}
- (id) tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
MyObj *obj;
obj = [data objectAtIndex:rowIndex];
NSString *identifier;
identifier = [aTableColumn identifier];
return [obj performSelector:NSSelectorFromString(identifier)];
}
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
MyObj *obj;
obj = [data objectAtIndex:rowIndex];
NSString *identifier;
identifier = [aTableColumn identifier];
if ([identifier isEqualTo:#"checked"]) {
BOOL value = [anObject boolValue];
obj.checked = value;
[data replaceObjectAtIndex:rowIndex withObject:obj];
}
[table reloadData];
}
#end
And I have raised objc_msgSend_vtable5 from -[NSButtonCell setObjectValue] method.
I found solution for my problem. I changed BOOL checked to NSNumber *checked.
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
MyObj *obj = (MyObj *)[data objectAtIndex:rowIndex];
NSString *identifier;
identifier = [aTableColumn identifier];
if ([identifier isEqualTo:#"checked"]) {
[obj setValue:[[NSNumber numberWithBool:[anObject boolValue]] retain] forKey:[aTableColumn identifier]];
}
[table reloadData];
}
And all works fine right now. I hope it helps someone.

NSTableView crashing

With the delegate and datasource connections made, I have the following controller:
#import <Foundation/Foundation.h>
#interface KextTable : NSObject <NSTableViewDataSource> {
#private
NSArray *klist;
}
- (int)numberOfRowsInTableView:(NSTableView *)tableView;
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row;
#end
and
#import "KextTable.h"
#implementation KextTable
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
-(void) awakeFromNib
{
klist = [[NSArray alloc] init];
klist = [NSArray arrayWithObjects: #"1", #"2",
#"3", #"4", nil]; // debugging values only
}
- (void)dealloc
{
[super dealloc];
}
- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
return [klist count];
}
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row
{
return [klist objectAtIndex:row];
}
#end
And this code is crashing with EXC_BAD_ACCESS in my main interface control where the view is switched to the tab containing the table view. What is wrong?
(I know that connections are right, if I create the array in objectValueForTableColumn it works)
It's because the klist you're creating (the debug one) is using the constructor that autoreleases it. So you should add in:
klist = [[NSArray arrayWithObjects: #"1", #"2", #"3", #"4", nil] retain];
Be sure to note that in what you've done there's a memory leak (you create an NSArray and then re-assign the variable to something else...)

objectAtIndex - Message sent to deallocated instance

I am having a real problem finding where my problem is in my search controller. This is a table view with search bar and search display controller. It used to work fine, but all the sudden it stopped working. I turned on NSZombieEnabled and it shows that my NSArray called searchDataSource is the zombie.
When you type a search term the "shouldReloadTableForSearchTerm" executes the handleSearchForTerm function. The handleSearchForTerm" function accesses my ProductInfo class that query a SQLite database and returns the query results. Those results are then placed in my searchDataSource Array. Everything appears to work fine there. However, once I get to the "cellForRowAtIndexPath" function and I try to load the cells from the searchDataSource, that is when I run in to the problem of the Array having been deallocated.
Here is my code for the search controller:
//
// SearchViewController.h
// Priority Wire
//
// Created by Keith Yohn on 2/2/11.
// Copyright 2011 Priority Wire & Cable. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface FourthViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate> {
UITableView *mainTableView;
NSArray *searchDataSource;
NSMutableArray *contentsList;
NSMutableArray *searchResults;
NSString *savedSearchTerm;
NSString *webURL;
}
#property (nonatomic, retain) IBOutlet UITableView *mainTableView;
#property (nonatomic, retain) IBOutlet NSArray *searchDataSource;
#property (nonatomic, retain) NSMutableArray *contentsList;
#property (nonatomic, retain) NSMutableArray *searchResults;
#property (nonatomic, copy) NSString *savedSearchTerm;
#property (nonatomic, retain) NSString *webURL;
- (void)handleSearchForTerm:(NSString *)searchTerm;
#end
SearchViewController.m
//
// SearchViewController.m
// Priority Wire
//
// Created by Keith Yohn on 2/2/11.
// Copyright 2011 Priority Wire & Cable. All rights reserved.
//
#import "FourthViewController.h"
#import "ProductsDatabase.h"
#import "ProductInfo.h"
#import "WebViewController.h"
#implementation FourthViewController
#synthesize mainTableView;
#synthesize searchDataSource;
#synthesize contentsList;
#synthesize searchResults;
#synthesize savedSearchTerm;
#synthesize webURL;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.searchDataSource count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Set up the cell...
ProductInfo *info = [searchDataSource objectAtIndex:indexPath.row]; //This is where I get the 'message sent to deallocated instance' message.
[cell.textLabel setText:info.sName];
[cell.detailTextLabel setText:info.sType];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ProductInfo *info = [searchDataSource objectAtIndex:indexPath.row];
webURL = [NSString stringWithFormat:#"http://www.prioritywire.com/specs/%#", info.sFile];
WebViewController *wvController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]];
wvController.URL = webURL;
wvController.navTitle = #"Spec Sheet";
[self.navigationController pushViewController:wvController animated:YES];
[wvController release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Save the state of the search UI so that it can be restored if the view is re-created.
[self setSavedSearchTerm:[[[self searchDisplayController] searchBar] text]];
[self setSearchResults:nil];
}
- (void)dealloc {
[searchDataSource release], searchDataSource = nil;
[mainTableView release];
[contentsList release];
[searchResults release];
[savedSearchTerm release];
[super dealloc];
}
- (void)handleSearchForTerm:(NSString *)searchTerm
{
[self setSavedSearchTerm:searchTerm];
if ([self searchResults] == nil)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[self setSearchResults:array];
[array release], array = nil;
} else {
NSArray *productInfo = [[ProductsDatabase database] searchListing:searchTerm];
self.searchDataSource = productInfo;
[self.mainTableView reloadData];
[productInfo release];
}
[[self searchResults] removeAllObjects];
if ([[self savedSearchTerm] length] != 0)
{
for (NSString *currentString in [self contentsList])
{
if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[[self searchResults] addObject:currentString];
}
}
}
}
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self handleSearchForTerm:searchString];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
[self setSavedSearchTerm:nil];
self.searchDataSource = nil;
[self.mainTableView reloadData];
}
#end
I am quite new to objective-C and can't understand what I did wrong. I have spent days trying to figure this out and have had no luck. I would appreciate any help anyone can offer.
Keith
This bit of code seems to be the only place searchDataSource gets set:
NSArray *productInfo = [[ProductsDatabase database] searchListing:searchTerm];
self.searchDataSource = productInfo;
[self.mainTableView reloadData];
[productInfo release];
If ProductsDatabase follows the rules, you don't own the returned array (i.e. it is already autoreleased) so the release on the fourth line is incorrect.
Don't you mean to use your searchResults-array instead of your searchDataSource, because in handleSearchForTerm: you are adding the results to it. Why do you even have the searchResult ivar? It's only used in handleSearchForTerm:, maybe some mixup there?

How do I edit a row in NSTableView to allow deleting the data in that row and replacing with new data?

I'm building a to-do-list application and I want to be able to edit the entries in the table and replace them with new entries. I'm close to being able to do what I want but not quit. Here is my code so far:
/*
IBOutlet NSTextField *textField;
IBOutlet NSTabView *tableView;
IBOutlet NSButton *button;
NSMutableArray *myArray;
*/
#import "AppController.h"
#implementation AppController
-(IBAction)addNewItem:(id)sender
{
[myArray addObject:[textField stringValue]];
[tableView reloadData];
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [myArray count];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
return [myArray objectAtIndex:rowIndex];
}
- (id)init
{
[super init];
myArray = [[NSMutableArray alloc] init];
return self;
}
-(IBAction)removeItem:(id)sender
{
NSLog(#"This is the index of the selected row: %d",[tableView selectedRow]);
NSLog(#"the clicked row is %d",[tableView clickedRow]);
[myArray replaceObjectAtIndex:[tableView selectedRow] withObject:[textField stringValue]];
[myArray addObject:[textField stringValue]];
//[tableView reloadData];
}
#end
It's not clear what problem you're having, so here's a better way to implement editing instead:
Why not just have your data source respond to tableView:setObjectValue:forTableColumn:row: messages messages? Then the user can edit the values right in the table view by double-clicking them; no need for a separate text field.
There's also a delegate method you can implement if you want to allow only editing some columns and not others.
Peter's answer is correct, but just in case someone would be looking for complete method for editing row:
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
YourClassWhichHoldsRowRecord *abc = [yourMutableArray objectAtIndex:row];
[abc setValue:object forKey: [tableColumn identifier]];
}

Resources