Filling a table with array with values from plist - objective-c

I'm trying to populate a table view with an array with values from a plist. Everything looks right but when I compile it doesn't show any table what so ever. Any clues as to why?
#import "Person.h"
#implementation Person
-(void) setAge:(int)age{
_age = age;
}
-(int) age {
return _age;
}
-(void) setName:(NSString *)name{
_name = name;
}
-(NSString *) name{
return _name;
}
-(void) setJob:(NSString *)job{
_job = job;
}
-(NSString *) job{
return _job;
}
#interface Person : NSObject {
int _age;
NSString * _name;
NSString * _job;
}
-(void) setAge: (int) age;
-(int) age;
-(void) setName: (NSString *) name;
-(NSString *) name;
-(void) setJob: (NSString *) job;
-(NSString *) job;
-(NSString *) summaryString;
#end
#import <UIKit/UIKit.h>
#interface PeopleListViewController : UITableViewController
//Array to hold list of people
#property(nonatomic, strong) NSArray *people;
#end
#import "PeopleListViewController.h"
#import "Person.h"
#interface PeopleListViewController ()
#end
#implementation PeopleListViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *peopleArray = [NSMutableArray array];
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"people" ofType:#"plist"]];
for(NSDictionary *dict in array){
Person *person = [[Person alloc] init];
person.job = [dict objectForKey:#"name"];
person.age = [[dict objectForKey:#"age"] intValue];
person.job = [dict objectForKey:#"job"];
[peopleArray addObject:person];
}
self.people = peopleArray;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 15;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =
[[self.people objectAtIndex:indexPath.row] objectForKey:#"name"];
return cell;
}

Related

NSArray - storing and retrieving data for a table using a button

I am trying to store data into NSArray using a button and then retrieve it to be displayed in a table. However, the table remains empty after the button has been pushed. I think the problem might have to do with how the Car object is being added to the array and then stored in NSData.
CarTableViewController.h
#import <UIKit/UIKit.h>
#import "CarDetailViewController.h"
#import "Car.h"
#interface CarListTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
#property Car *selectedCar;
#property NSMutableArray *listOfCars;
#property (strong, nonatomic) IBOutlet UITableView *carTableView;
#end
CarTableViewController.m
#import "CarListTableViewController.h"
#import "CarEntryViewController.h"
#implementation CarListTableViewController
#synthesize selectedCar, listOfCars, carTableView;
- (void)viewDidLoad {
[super viewDidLoad];
carTableView.delegate = self;
carTableView.dataSource = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return listOfCars.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"carCell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"carCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
}
Car *tempCar =[listOfCars objectAtIndex:indexPath.row];
cell.textLabel.text =tempCar.make;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:true];
selectedCar = [listOfCars objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:#"viewDetailsSegue" sender:nil];
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
CarDetailViewController* vc = [segue destinationViewController];
vc.carObject = selectedCar;
}
- (NSArray*) retrieveDataFromNSUserDefaults {
NSMutableArray *objectArray = [NSMutableArray new];
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:#"savedArray"];
if (dataRepresentingSavedArray != nil) {
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
objectArray = [[NSMutableArray alloc]
initWithArray:oldSavedArray];
else
objectArray = [[NSMutableArray alloc] init];
}
return objectArray;
}
- (void)storeDataInNSUserDefaults:(Car *)carToStore {
NSMutableArray *objectArray = [NSMutableArray arrayWithArray:[self retrieveDataFromNSUserDefaults]];
[objectArray addObject:carToStore];
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:objectArray] forKey:#"savedArray"];
}
#end
your code looks fine to me but I think you are not calling method to add data [self storeDataInNSUserDefaults:car] anywhere.

After creating an NSMutableArray and adding objects, count shows as 0

The fast enumeration works, and shows the memory address for my 3 objects. But a simple count on my brothersArray (brothersArray.count) comes up with 0. I'm a bit of a noob. What I am missing?
I'm not receiving any errors or warnings. Everything appears to work fine. I just can't figure out why my array.count comes up as zero. Thanks in advance!
Here is my entire MasterViewController.m
// MasterViewController.m
// Matt_Bug
//
// Created by Matthew Toto on 5/17/14.
// Copyright (c) 2014 Matthew Toto. All rights reserved.
//
#import "MasterViewController.h"
#import "DetailViewController.h"
#import "MPTBrothers.h"
#interface MasterViewController () {
NSMutableArray *_objects;
}
#end
#implementation MasterViewController
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Brothers";
MPTBrothers *First = [[MPTBrothers alloc]initWithInfo:#"JP" age:37 height:5];
MPTBrothers *Second = [[MPTBrothers alloc]initWithInfo:#"Matt" age:33 height:6];
MPTBrothers *Third = [[MPTBrothers alloc]initWithInfo:#"Kevin" age:27 height:5];
NSMutableArray *brothersArray = [NSMutableArray arrayWithObjects:First, Second, Third, nil];
for (id brotherObjects in brothersArray) {
NSLog(#"%#", brotherObjects);
}
NSLog(#"The array count is %lu",brothersArray.count);
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
MPTBrothers *brotherCell = [self.brothersArray objectAtIndex:indexPath.row];
cell.textLabel.text = brotherCell.name;
NSLog(#"BrotherCell Name = %#", brotherCell.name);
// NSDate *object = _objects[indexPath.row];
// cell.textLabel.text = [object description];
return cell;
}
Here is my MPTBrothers.m
#import "MPTBrothers.h"
#implementation MPTBrothers
-(id)initWithInfo:(NSString *)name age:(int)age height:(int)height {
if (self = [self init])
_name = name;
_age = age;
_height = height;
return self;
}

UISearchBarDelegate on UITableView with Sections Issue

I'm having issues with implementing the UISearchBarDelegate on my UITableView with sections issue where every time I try to search for something inside the UITableView, either it doesn't display the desired result or it crashes the app with an error saying:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
Here's inside my header file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate>
{
UITableView *mainTableView;
NSMutableArray *contentsList;
NSMutableArray *searchResults;
NSString *savedSearchTerm;
NSMutableArray *ivSectionKeys;
NSMutableDictionary *ivSectionContents;
}
#property (nonatomic, retain) IBOutlet UITableView *mainTableView;
#property (nonatomic, retain) NSMutableArray *contentsList;
#property (nonatomic, retain) NSMutableArray *searchResults;
#property (nonatomic, copy) NSString *savedSearchTerm;
#property (nonatomic, retain) NSMutableArray *sectionKeys;
#property (nonatomic, retain) NSMutableDictionary *sectionContents;
- (void)handleSearchForTerm:(NSString *)searchTerm;
#end
while this is inside my implementation file:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize mainTableView;
#synthesize contentsList;
#synthesize searchResults;
#synthesize savedSearchTerm;
#synthesize sectionKeys = ivSectionKeys;
#synthesize sectionContents = ivSectionContents;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *colorKey = #"Colors";
NSString *clothingKey = #"Clothing";
NSString *miscKey = #"Misc";
[contents setObject:[NSArray arrayWithObjects:#"Red", #"Blue", nil] forKey:colorKey];
[contents setObject:[NSArray arrayWithObjects:#"Pants", #"Shirt", #"Socks", nil] forKey:clothingKey];
[contents setObject:[NSArray arrayWithObjects:#"Wankle Rotary Engine", nil] forKey:miscKey];
[keys addObject:clothingKey];
[keys addObject:miscKey];
[keys addObject:colorKey];
self.sectionKeys = keys;
self.sectionContents = contents;
// Restore search term
if (self.savedSearchTerm)
{
self.searchDisplayController.searchBar.text = self.savedSearchTerm;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Save the state of the search UI so that it can be restored if the view is re-created.
self.savedSearchTerm = self.searchDisplayController.searchBar.text;
self.searchResults = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.mainTableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)handleSearchForTerm:(NSString *)searchTerm
{
self.savedSearchTerm = searchTerm;
if (self.searchResults == nil)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
self.searchResults = array;
array = nil;
}
[self.searchResults removeAllObjects];
if ([self.savedSearchTerm length] != 0)
{
for (NSString *currentString in self.sectionContents)
{
if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[self.searchResults addObject:currentString];
}
}
}
}
#pragma mark -
#pragma mark UITableViewDataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger sections = [self.sectionKeys count];
return sections;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
NSString *key = [self.sectionKeys objectAtIndex:section];
return key;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
NSString *key = [self.sectionKeys objectAtIndex:section];
NSArray *contents = [self.sectionContents objectForKey:key];
NSInteger rows;
if (tableView == [[self searchDisplayController] searchResultsTableView])
rows = [self.searchResults count];
else
rows = [contents count];
NSLog(#"rows is: %d", rows);
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [self.sectionKeys objectAtIndex:indexPath.section];
NSArray *contents = [self.sectionContents objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:indexPath.row];
if (tableView == [self.searchDisplayController searchResultsTableView])
contentForThisRow = [self.searchResults objectAtIndex:indexPath.row];
else
contentForThisRow = [contents objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = contentForThisRow;
return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#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.savedSearchTerm = nil;
[self.mainTableView reloadData];
}
#end
I think my issue is inside my for-loop but I'm not really sure.
You need to update all of your table view data source and delegate methods to handle both tables (you only do this in some):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger sections = [self.sectionKeys count];
return sections;
}
should be:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == self.tableView) {
NSInteger sections = [self.sectionKeys count];
return sections;
} else { // assume it's the search table
// replace this with the actual result
return numberOfSectionsForSearchTable;
}
}

How to pass data from uitableview to detail view in storyboard

I'm creating my first app in iOS 5 using storyboards. I have a tableviewcontroller embedded in a navigation controller and when you click on the cells in the tableviewcontroller some detail about that cell topic & a URL should be passed to a detail view. I use a PList to populate the tableview. I failed to pass the detail to detail view...
Here is my code (4 classes):
// NormalDataCell.m
#import "NormalDataCell.h"
#implementation NormalDataCell
#synthesize image = _image;
#synthesize nameLabel = _nameLabel;
#synthesize category = _category;
#synthesize webSiteURL = _webSiteURL;
#end
// normalData.m
#import "NormalData.h"
#implementation NormalData
#synthesize name = _name;
#synthesize image = _image;
#synthesize webSiteURL = _webSiteURL;
#synthesize category = _category;
- (id)initWithItem:(NSDictionary *)item {
self = [super init];
if (self) {
_name = [item objectForKey:#"name"];
_image = [UIImage imageNamed:[item objectForKey:#"image"]];
_webSiteURL = [item objectForKey:#"webSiteURL"];
_category = [item objectForKey:#"category"];
}
return self;
}
#end
#implementation NormalCategory
#synthesize category = _category;
#synthesize normalList = _normalList;
- (id)initWithArray:(NSMutableArray *)normalList {
self = [super init];
if (self) {
NSMutableArray *list = [[NSMutableArray alloc]init];
for (NSDictionary *item in normalList) {
NormalData *data = [[NormalData alloc]initWithItem:item];
[list addObject:data];
}
NormalData *data = [list objectAtIndex:0];
_category = data.category;
_normalList = list;
}
return self;
}
#end
// normalViewController.m
#import "NormalViewController.h"
#import "NormalData.h"
#import "NormalDataCell.h"
#import "NormalDetailView.h"
#interface NormalViewController ()
{
#private
NSMutableArray *_cellContentList;
}
#end
#implementation NormalViewController
#synthesize tableView; // Add this line of code
#synthesize roleArray;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://vfttx.web.fc2.com/normal.plist"];
NSMutableArray *array = [NSMutableArray arrayWithContentsOfURL:url];
_cellContentList = [[NSMutableArray alloc]init];
for (NSMutableArray *item in array)
{
NormalCategory *category = [[NormalCategory alloc]initWithArray:item];
[_cellContentList addObject:category];
}
}
//How do I pass the name & webSiteURL to DetailView?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"TableToWebSegue"]) {
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
NSString *cellName = [[_cellContentList objectAtIndex:path.row] objectForKey:#"name"];
[segue.destinationViewController setDetailItem2:cellName];
NSString *urlPath = [[_cellContentList objectAtIndex:path.row] objectForKey:#"webSiteURL"];
[segue.destinationViewController setWebSiteURL:urlPath];
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_cellContentList count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NormalCategory *category = [_cellContentList objectAtIndex:section];
return [category category];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NormalCategory *category = [_cellContentList objectAtIndex:section];
return [category.normalList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"normalDataCell";
NormalDataCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[NormalDataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NormalCategory *category = [_cellContentList objectAtIndex:[indexPath section]];
NormalData *data = [category.normalList objectAtIndex:[indexPath row]];
[cell.nameLabel setText:data.name];
[cell.image setImage:data.image];
cell.webSiteURL = data.webSiteURL;
return cell;
}
#end
// NormalDetailView.m
#import "NormalDetailView.h"
#interface NormalDetailView ()
#end
#implementation NormalDetailView
#synthesize webSiteURL;
#synthesize webView;
#synthesize name = _name;
#synthesize detailItem2;
#synthesize testNameLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
testNameLabel.text = detailItem2;
//If the URL is right, it will work.
NSString *strUrl = webSiteURL;
strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
#end
Presumably you're using segues to move between the different views.
Implement - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender on the view controller containing your table view and it will be called after the user taps the cell. It contains a reference to the segue object which you can use to find the sourceViewController and the destinationViewController, you can use these to set the data in the destination.

how to get information of an object instance from ID number

I have a program that has 2 tables on two different windows. One table holds the customer name and id number and the other one hold the item name and number. They are both stored in a array and also in a .plist file.
What i would like to do is that on a third page there is going to be a sales page where the user will enter the customer id and item id and the program should be able to find the name and display it to a label. I don't know where to start and go. Could somebody please help or show me how to do i? I can upload any code that anybody wants to see but as I dont know where to start I dont know what to upload.
this is the customer.h file
#import <Foundation/Foundation.h>
NSString *name;
int memberNumber;
#interface Customer : NSObject <NSCoding>
{
NSString *name;
int memberNumber;
}
#property (nonatomic, copy) NSString *name;
#property int memberNumber;
#end
this is the customer.m
#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;
}
- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init])
{
self.name = [decoder decodeObjectForKey:#"name"];
self.memberNumber = [decoder decodeIntForKey:#"memberNumber"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:name forKey:#"name"];
[encoder encodeInt:memberNumber forKey:#"memberNumber"];
}
-(void)dealloc
{
[name release];
[super dealloc];
}
#end
this is the tableView.h file
#import <Foundation/Foundation.h>
#include <stdlib.h>
NSString *filepath;
#interface tableViewData : NSObject <NSTableViewDataSource>
{
#private
IBOutlet NSTableView *tableView;
NSMutableArray *list;
NSString *filepath;
}
-(IBAction)add:(id)sender;
-(IBAction)remove:(id)sender;
#end
this is the tableView.m file
#import "tableViewData.h"
#import "Customer.h"
#implementation tableViewData
-(void)awakeFromNib{
filepath = #"/Users/Desktop/CustomerNames.plist";
if ([[NSFileManager defaultManager]fileExistsAtPath:filepath])
{
NSMutableArray *archive = [NSKeyedUnarchiver unarchiveObjectWithFile:filepath];
list = archive;
}
else
list=[[NSMutableArray alloc]init];
}
-(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];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:list];
[data writeToFile:filepath options:NSDataWritingAtomic error:nil];
}
-(IBAction)add:(id)sender
{
[list addObject:[[Customer alloc]init]];
[tableView reloadData];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:list];
[data writeToFile:filepath options:NSDataWritingAtomic error:nil];
for (id name in list)
NSLog(#"obj: %#", name);
NSLog (#"array:%#",list);
}
-(IBAction)remove:(id)sender
{
NSInteger row = [tableView selectedRow];
if (row != -1)
{
[list removeObjectAtIndex:row];
}
[tableView reloadData];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:list];
[data writeToFile:filepath options:NSDataWritingAtomic error:nil];
}
-(void)dealloc
{
[super dealloc];
}
#end
hope this helps
(xcode 4.2.1 for OS X)
For example:
if([myNumber isKindOfClass:[NSNumber class]])
{
//do something here
}
That check if object myNumber is a NSNumber class Object. You can use whatever class you want of course. Read the documentation
If items you loaded to table are at the same order in your table you can use
[myArray objectAtIndex:[indexPath row]];
Then use can use the variables of the array that you needed.
First in that code:
-(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];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:list];
[data writeToFile:filepath options:NSDataWritingAtomic error:nil];
}
change Customer *Customer to Customer *customer ALWAYS use that way.
Then use that method to understand which row is selected.In your case which customer is selected. I understand from your code that every row has a costumer and those customers are in your list array.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//with this code you can get the selected customer from your list,
Customer *customer=[list objectAtIndex:[indexPath row]];
}
Then you can reach the values of the customer.