NSTableView Data Source Not Working - objective-c

I am trying to populate an NSTableView with an NSArray. I have a view controller, which I have set as my datasource, but every time I run the program, I get this error: Must implement numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:, both of which I have implemented.
Here is my header:
#import <Cocoa/Cocoa.h>
#interface ChatViewController : NSViewController <NSTableViewDataSource>
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex;
#end
And here is my implementation file:
#import "ChatViewController.h"
#import "Socket.h"
#interface ChatViewController ()
#property (strong) IBOutlet NSTableView *people;
#property (strong) IBOutlet NSTextField *message;
#property (strong) IBOutlet NSButton *send;
#property (strong) Socket *sock;
#property (strong, nonatomic) NSString *recievedText;
#property (strong, nonatomic) NSArray *tableData;
- (void)updateUI;
#end
#implementation ChatViewController
#synthesize sock;
#synthesize recievedText;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self.people setDataSource:self];
sock = [[Socket alloc] init];
[sock connectToServerWithIP:#"127.0.0.1" andPort:5001];
[self updateUI];
}
return self;
}
- (void)updateUI
{
[sock sendMessage:#"getPeople"];
recievedText = [NSString stringWithString:[sock recieveMessage]];
self.tableData = [recievedText componentsSeparatedByString:#";"];
NSLog(#"%#", self.tableData);
[self.people reloadData];
NSLog(#"%#", self.people);
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [self.tableData count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return [self.tableData objectAtIndex:rowIndex];
}
#end
Socket is a class that I created for opening and closing sockets to a server, which is fully functional. The tableData array is populated like a normal array.
Thanks for any help.

at first make your table view delegate and data source of file's owner through xib.
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
if(aTableView == tableViewS)
{
if([[aTableColumn identifier] isEqualToString:#"first"])
{
return [self.tableData objectAtIndex:rowIndex];
}
}
return nil;
}
you need to give identifier name for table column
tableViewS is IBOutlet of table view.

First of you don't need to declare the methods in your .h file since they are part of the protocol your class is implementing. And also have you really set you data source? Have you done something like this some where?
self.tableView.dataSource = self;

Related

Custom NSTableCellView

I want to make a NSTableView with customs NSTableCellView, here's what I've done so far :
AppDelegate.h :
#import <Cocoa/Cocoa.h>
#import "TheView.h"
#interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate>
#property (weak) IBOutlet NSTableView *tableView;
#property (copy) NSMutableArray *tableContent;
#property (assign) IBOutlet NSWindow *window;
-(IBAction)addRow:(id)sender;
#end
AppDelegate.m :
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize tableContent;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
tableContent = [[NSMutableArray alloc]init];
}
-(id)init{
self = [super init];
if (self) {
tableContent = [[NSMutableArray alloc]init];
}
return self;
}
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
NSLog(#"%#",tableContent); //Here tableContent is empty
return [tableContent count];
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSView *result = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
TheView *view = [tableContent objectAtIndex:row];
result = view;
return result;
}
-(IBAction)addRow:(id)sender {
TheView *view = [[TheView alloc]init];
[tableContent addObject:view];
NSLog(#"%#",tableContent); //Here tableContent result with the correct number of objects
[_tableView reloadData];
}
But when I try to add an object, nothing happens... TheView is a subclass of NSTableCellView, it's drawing a simple rect.
I've of course added NSTableViewDataSource and NSTableViewDelegate.
UPDATE 2
After further researches in my code, I found that in numberOfRowsInTableView, tableContent resulted empty or with the number of object I initialized my array with...
The views your creating seem to have no size. You could try replacing the view initialization, and use initWithFrame instead of init
[[TheView alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
Also, you can try set in fame on the viewForTableColumn method.

UITableView in a UIViewController on an iPad

I'm trying to develop a tiny app to manage the registry for a teacher. My app works so:
I present a UIViewController (only landscape) to select the class.
When I press the button (for example: 1F), I call a method to parse an XML file in which I got the name and the surname of the students. So I put this name and surname into an NSArray of NSDictionary (arrayStudents[0] = dictStudents and dictStudents is composed by 3 key: number, name and surname).
In the second UIViewController I put a UITableView on the left of the screen and a normal view to the right. In the UITableView I want to have the name and surname of the students and on the right I want to show the score about test of the student selected by tapping on the UITableView.
This is how my viewController looks like:
My problem is how to populate the UITableView. I connected the tableView with an IBOutlet to my second viewController and then I connect the delegate and the dataSource to the second viewController, but the app is still crashing. I will post here the code of my table view class and second view controller, so you can help me to fix this problem.
Here you can see the connection tab of my tableView:
tableView.h
#import <UIKit/UIKit.h>
#interface tableView : UITableView <UITableViewDelegate,UITableViewDataSource>
#property (nonatomic, strong) NSMutableArray *arrayStudents;
#end
tableView.m
#import "tableView.h"
#implementation tableView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.arrayStudents.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.arrayStudents[indexPath.row] objectForKey:#"name"];
cell.detailTextLabel.text = [self.arrayStudents[indexPath.row] objectForKey:#"surname"];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
#end
detailsViewController.h
#import <UIKit/UIKit.h>
#interface DetailsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *tableViewStudentsNameAndSurname;
#property (weak, nonatomic) IBOutlet UIView *viewDetails;
#property (weak, nonatomic) IBOutlet UILabel *labelName;
#property (weak, nonatomic) IBOutlet UILabel *labelSurname;
#property (weak, nonatomic) IBOutlet UITextField *textFieldTest1;
#property (weak, nonatomic) IBOutlet UITextField *textFieldTest2;
#property (weak, nonatomic) IBOutlet UITextField *textFieldTest3;
#property (weak, nonatomic) IBOutlet UITextField *textFieldText4;
#property (weak, nonatomic) IBOutlet UITextView *textViewValutation;
#property (weak, nonatomic) IBOutlet UILabel *labelTotalScore;
#property (nonatomic, strong) NSString *fileName;
- (IBAction)saveDataToPlistFile:(id)sender;
#end
detailsViewController.m
#import "DetailsViewController.h"
#import "ParserXML.h"
#import "tableView.h"
#interface DetailsViewController () {
ParserXML *parser;
tableView *table;
}
#end
#implementation DetailsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
parser = [[ParserXML alloc] init];
NSLog(#"DetailsViewController: %#", self.fileName);
parser.fileName = self.fileName;
[parser parseXML];
table = [[tableView alloc] init];
table.arrayStudents = [parser.arrayStudents mutableCopy];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveDataToPlistFile:(id)sender {
}
#end
ParserXML.h
#import <Foundation/Foundation.h>
#interface ParserXML : NSObject <NSXMLParserDelegate>
#property (nonatomic, strong) NSMutableArray *arrayStudents;
#property (nonatomic, strong) NSDictionary *dictStudents;
#property (nonatomic, strong) NSString *fileName;
- (void) parseXML;
#end
ParserXML.m
#import "ParserXML.h"
#interface ParserXML() {
NSXMLParser *nameStudentParser;
NSString *pathXmlFile;
}
#end
#implementation ParserXML
- (id) init {
self = [super init];
if (self) {
self.arrayStudents = [[NSMutableArray alloc] init];
pathXmlFile = [[NSBundle mainBundle] pathForResource:self.fileName ofType:#"xml"];
}
return self;
}
- (void) parseXML {
NSURL *xmlUrl = [NSURL URLWithString:pathXmlFile];
NSString *host = [xmlUrl host];
if (xmlUrl == nil || host == nil) {
NSData *data = [[NSData alloc] initWithContentsOfFile:pathXmlFile];
nameStudentParser = [[NSXMLParser alloc] initWithData:data];
} else {
nameStudentParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlUrl];
}
[nameStudentParser setDelegate:self];
[nameStudentParser setShouldProcessNamespaces:NO];
[nameStudentParser setShouldReportNamespacePrefixes:NO];
[nameStudentParser setShouldResolveExternalEntities:NO];
[nameStudentParser parse];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser {
NSLog(#"File trovato inizio il parsing del documento");
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSLog(#"Errore Parsing");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:#"students"]) {
} else if ([elementName isEqualToString:#"student"]) {
NSString *numberValue = attributeDict[#"number"];
NSString *nameValue = attributeDict[#"name"];
NSString *surnameValue = attributeDict[#"surname"];
//Inserire dizionario di array
self.dictStudents = #{#"number": numberValue,
#"name": nameValue,
#"surname" : surnameValue};
[self.arrayStudents addObject:self.dictStudents];
NSLog(#"arrayStudents dim = %d", self.arrayStudents.count);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(#"ArrayFuels = %#", self.arrayStudents);
}
#end
When I run the app Xcode says:
[DetailsViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x751da40
Can you help me to fix that problem? What I'm doing wrong?
Okay, So I'm pretty new to objective C so this is not an expert opinion. But from what I get, it is looking for the delegate method that should be there in DetailsViewController class. You have connected your table view delegate/datasource to DetailsViewController class, I think you're supposed to specify the class as being the delegate class of UITableView as well.
In DetailsViewController.h:
#interface DetailsViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
and then plug in the UITableView Delegate methods in DetailsViewController.m file.
Although, what I don't get is why are you using 2 table view classes? If you want to use tableView class, why not initialize in DetailsViewController::viewDidLoad and simply:
[self.view addSubView:tableView]
I don't think there is a need for tableViewStudentsNameAndSurname in your class.

Loading NSTableView from unarchived data

I'm trying to implement saving of user preferences in my OS X app. I have an NSTableView which stores its data to my .plist file, but I have troubles loading same data back to my NSTableView.
My model object:
#import <Foundation/Foundation.h>
#interface UserKeys : NSObject <NSCoding>
#property (nonatomic, copy) NSString *userKeyName;
#property (nonatomic, copy) NSString *userApi;
#property (nonatomic, copy) NSString *userCode;
#end
and its implementation:
#import "UserKeys.h"
#implementation UserKeys
- (id)init
{
self = [super init];
if (self) {
//
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.userKeyName = [aDecoder decodeObjectForKey:#"userKeyName"];
self.userApi = [aDecoder decodeObjectForKey:#"userApi"];
self.userCode = [aDecoder decodeObjectForKey:#"userCode"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)enCoder {
[enCoder encodeObject:self.userKeyName forKey:#"userKeyName"];
[enCoder encodeObject:self.userApi forKey:#"userApi"];
[enCoder encodeObject:self.userCode forKey:#"userCode"];
}
#end
My preferences view controller header:
#import <Cocoa/Cocoa.h>
#import "MASPreferencesViewController.h"
#import "UserKeys.h"
#interface PreferencesApiKeysViewController : NSViewController <MASPreferencesViewController>
#property (strong) NSMutableArray *allKeys;
#end
and its implementation:
#import "PreferencesApiKeysViewController.h"
#import "UserKeys.h"
#interface PreferencesApiKeysViewController ()
#property (weak) IBOutlet NSTableView *keysTableView;
#end
#implementation PreferencesApiKeysViewController
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle {
if ((self = [super initWithNibName:nibName bundle:bundle])) {
self.allKeys = [NSMutableArray array];
}
return self;
}
// delegating....
//
- (void)viewWillAppear {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:#"UserKeys"];
if (data != nil) {
NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if (oldArray != nil) {
self.allKeys = [[NSMutableArray alloc] initWithArray:oldArray];
} else {
self.allKeys = [NSMutableArray array];
}
}
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// some code...
return cellView;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [self.allKeys count];
}
The error I'm getting is:
-[UserKeys count]: unrecognized selector sent to instance 0x100534df0
When I put a breakpoint in - (void)viewWillAppear, I can see that data has around 250 bytes, but this oldArray seems to be empty. I understand that I'm doing some major mistake here with self.allKeys so that my table is not aware how many rows it should create, but after lots of attempts to get proper data, I'm really out of ideas why is this happening.
So, how to properly unarchive my data and present it in NSTableView?
(I'm using Xcode 4.6)
It is because the object you appear to be dearchiving is a pointer to an object of class UserKeys, rather than an object that is or descends from NSArray. When -initWithAray: hits your oldArray pointer to UserKeys, it doesn't know, or doesn't care what class it is, it simply sends it a -count message so as to ascertain the size it must grow to.

Call method from another view, I think?

I have a simple utility app, with a MainViewController.m & h and a FlipsideViewController.m & h. Within my storyboard I have a button on MainViewController. I want to be able to click the button and run a method in FlipsideViewController.m is this possible? this is my first app and I am a total novice. all comments / suggestion welcome.
enter code here
i have this in my FlipsideViewController.m this is what i want to call when i click the button.
- (void)SaveFPQData
{
NSLog(#"Data Saved");
}
and this is what i have in MainViewController.m
- (IBAction)saveButton:(id)sender
{
}
This is the code I have so far;
MainViewController.h
#import "FlipsideViewController.h"
#import "sqlite3.h"
#import "FPQCheck.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
#property (weak, nonatomic) IBOutlet UITextField *nameField;
#property (weak, nonatomic) IBOutlet UITextField *checkField;
#property (weak, nonatomic) IBOutlet UITextField *commentsField;
#property (weak, nonatomic) FlipsideViewController *flipsidecontroller;
- (IBAction)saveButton:(id)sender;
- (IBAction)showHistoryButton:(id)sender;
#end
MainViewController.m
#import "MainViewController.h"
#import "FlipsideViewController.h"
#import "sqlite3.h"
#interface MainViewController ()
#end
#implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
// Do any additional setup after loading the view, typically from a nib.
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showAlternate"]) {
[[segue destinationViewController] setDelegate:self];
}
}
- (IBAction)saveButton:(id)sender
{
[self.flipsidecontroller SaveFPQData];
//[[NSNotificationCenter defaultCenter] postNotificationName:#"SaveFPQData" object:nil];
}
- (IBAction)showHistoryButton:(id)sender
{
}
#end
FlipSideViewController.h
#import <UIKit/UIKit.h>
#import "FPQCheck.h"
#class FlipsideViewController;
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#end
#interface FlipsideViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
#property (weak, nonatomic) id <FlipsideViewControllerDelegate> delegate;
-(void)SaveFPQData;
- (IBAction)done:(id)sender;
- (IBAction)deleteEntry:(id)sender;
#end
FlipSideViewController.m
#import "FlipsideViewController.h"
#import "MainViewController.h"
#interface FlipsideViewController ()
{
NSMutableArray *arrayOfCheck;
sqlite3 *fpqDB;
NSString *dbPathString;
}
#end
#implementation FlipsideViewController
- (void)viewDidLoad
{
/*
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(SaveFPQData)
name:#"SaveFPQData"
object:nil];
*/
[super viewDidLoad];
arrayOfCheck = [[NSMutableArray alloc]init];
[self creatOrOpenDB];
[[self myTableView]setDelegate:self];
[[self myTableView]setDataSource:self];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)SaveFPQData
{
NSLog(#"Data Saved");
}
-(void)creatOrOpenDB
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:#"FPQ.db"];
char *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbPathString]) {
const char *dbPath = [dbPathString UTF8String];
//create db
if (sqlite3_open(dbPath, &fpqDB)==SQLITE_OK) {
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS FPQ (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, CHECK INTEGER, COMMENTS TEXT)";
sqlite3_exec(fpqDB, sql_stmt, NULL, NULL, &error);
sqlite3_close(fpqDB);
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Actions
- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];}
- (IBAction)deleteEntry:(id)sender {
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayOfCheck count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
FPQCheck *fpqCheck = [arrayOfCheck objectAtIndex:indexPath.row];
NSString *nameANDcheck = [NSString stringWithFormat:#"%#%d", fpqCheck.name, fpqCheck.check];
cell.textLabel.text = nameANDcheck;
cell.detailTextLabel.text = fpqCheck.comments;
return cell;
}
#end
You have mainly two ways:
add a property (eg. self.flipSideController) to your MainViewController to store a reference to the FlipsideViewController; then call SaveFPQData though it (eg. [self.flipSideController SaveFPQData]; or
use notification center to post a notification from saveButton: that triggers SaveFPQData; this would go like this:
//-- in flipsidecontroller `viewDidLoad`:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(SaveFPQData)
name:#"SaveFPQData"
object:nil];
//-- in saveButton:
[[NSNotificationCenter defaultCenter] postNotificationName:#"SaveFPQData" object:nil];
The second method is the simplest to implement, IMO, and it allows for the loosest coupling, at the expense of some clock cycles.
EDIT:
It is not entirely clear to me what you are trying to do (specifically, I don't understand fully how you can push the button in MainViewController once you FlipsideViewController is displayed; on the other hand, if you do not segue to the FlipsideViewController, then it is not there, so you cannot send a message to it), anyway you could try and initialise your self.flipsideViewController property in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showAlternate"]) {
UIViewController* controller = [segue destinationViewController];
[controller setDelegate:self];
if ([controller isKindOfClass:[FlipsideViewController class]])
self.flipsideViewController = (id)controller;
}
}
after doing that, your MainViewController will be able to send the saveFPQ message to the FlipsideViewController.
If you mean you would like to send the saveFPQ message before segue-ing to the FlipsideViewController, you should make the saveButton segue to it and the call the saveFPQ method.
What I suspect is you need some kind of "model" object accessible both from the main view and the flipside view controller.
Hope this helps.

Add NSTableView data to NSPopUpButton items

I have a NSTableView where items can be added and deleted. Once items have been added to the table, I would like those items to also show as items for an NSPopUpButton. I tried the addItemsWithTitles: method but it gives me an error.
#import "TableController.h"
#import "Favorites.h"
#interface TableController ()
#property NSMutableArray *array;
#property (weak) IBOutlet NSTableView *tableView;
#property (weak) IBOutlet NSPopUpButton *popButton;
#end
#implementation TableController
- (id)init {
self = [super init];
if (self) {
_array = [[NSMutableArray alloc] init];
}
return self;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [_array count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
Favorites *fav = [_array objectAtIndex:row];
NSString *ident = [tableColumn identifier];
return [fav valueForKey:ident];
}
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
Favorites *fav = [_array objectAtIndex:row];
NSString *ident = [tableColumn identifier];
[fav setValue:object forKey:ident];
}
- (IBAction)add:(id)sender {
[_array addObject:[[Favorites alloc] init]];
[_tableView reloadData];
[_popButton addItemsWithTitles:_array];
}
-(IBAction)delete:(id)sender {
NSInteger row = [_tableView selectedRow];
[_tableView abortEditing];
if (row != -1) {
[_array removeObjectAtIndex:row];
}
[_tableView reloadData];
}
#end
So I tried logging the objectAtIndex:0 for the array and didn't get a string but received some numbers instead:
Array string is <Favorites: 0x10013e820>
And also for reference my Favorites class is
#import "Favorites.h"
#interface Favorites ()
#property (copy) NSString *location;
#end
#implementation Favorites
- (id)init {
self = [super init];
if (self) {
_location = #"City, State or ZIP";
}
return self;
}
#end
Your array has instances of your class, Favorites, and in your addItemsWithTiles: method you are adding those instances, not some string property of those instances, which is what I presume you want. If you use [_array valueForKey:#"whateverKeyHasYourStrings"] as the argument to addItemsWithTitles: instead of just _array, I think it will work ok.