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

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]];
}

Related

TableView code to display plist is not working, Is it because it has to be on a UITableView Controller? (Xcode)

I cannot get my plist to display on my app. I need to use TableView and not a TableView Controller. Im not sure if I have followed the wrong code when first attempting it. Please take a look.
m. file is:
#import "ECSlidingViewController.h"
#import "NewsFeedViewController.h"
#import "BuySharesViewController.h"
#import "SellSharesViewController.h"
#import "FinancesViewController.h"
#import "CurrentHoldingsViewController.h"
#import "TradingHistoryViewController.h"
#import "LeaderboardViewController.h"
#import "HowToPlayViewController.h"
#import "MenuViewController.h"
#import "InitViewController.h"
#interface SellSharesViewController ()
{
BOOL isSearching;
}
#property (nonatomic, readonly) NSDate *CurrentDate;
#property (nonatomic, strong) NSDictionary *shares;
#property (nonatomic, strong) NSArray *shareValue;
#property (nonatomic, strong) NSArray *number;
#property (nonatomic, strong) NSArray *shareName;
- (void)resetSearch;
- (void)handleSearchForTerm:(NSString *)searchTerm;
#end
#implementation SellSharesViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
isSearching = NO;
NSString *path=[[NSBundle mainBundle] pathForResource:#"shares" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.allNames = dict;
//NSArray *array = [[self.names allKeys] sortedArrayUsingSelector:#selector(compare:)];
//self.keys = (NSMutableArray *)array;
[self resetSearch];
[self.table reloadData];
[self.table setContentOffset:CGPointMake(0.0, 44.0) animated:NO];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (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 ([self.keys count] > 0) ? [self.keys count] : 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if ([self.keys count] == 0)
return 0 ;
NSString *key = [self.keys objectAtIndex:section];
NSArray *nameSection = [self.names objectForKey:key];
return nameSection.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [self.keys objectAtIndex:section];
NSArray *nameSection = [self.names objectForKey:key];
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if ([self.keys count] == 0)
return nil;
NSString *key = [self.keys objectAtIndex:section];
if (key == UITableViewIndexSearch)
return nil;
return key;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if (isSearching)
return nil;
return self.keys;
}
#pragma mark -
#pragma mark Custom Methods
- (void) resetSearch {
NSMutableDictionary *allNamesCopy = [self.allNames mutableDeepCopy];
self.names = allNamesCopy;
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
[keyArray addObjectsFromArray:[[self.allNames allKeys]
sortedArrayUsingSelector:#selector(compare:)]];
[keyArray insertObject:UITableViewIndexSearch atIndex:0];
self.keys = keyArray;
}
- (void)handleSearchForTerm:(NSString *)searchTerm {
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];
for (NSString *key in self.keys) {
NSMutableArray *array = [self.names valueForKey:key];
NSMutableArray *toRemove = [[NSMutableArray alloc] init];
for (NSString *name in array ) {
if ([name rangeOfString:searchTerm
options:NSCaseInsensitiveSearch].location == NSNotFound)
[toRemove addObject:name];
}
if ([array count] == [toRemove count])
[sectionsToRemove addObject:key];
[array removeObjectsInArray:toRemove];
}
[self.keys removeObjectsInArray:sectionsToRemove];
[self.table reloadData];
}
#pragma mark -
#pragma mark Table View Delegate Methods
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.search resignFirstResponder];
isSearching = NO;
self.search.text = #"";
[self.table reloadData];
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];
[self.table reloadData];
return;
}
[self handleSearchForTerm:searchTerm];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
isSearching = NO;
self.search.text = #"" ;
[self resetSearch];
[self.table reloadData];
[searchBar resignFirstResponder];
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
isSearching = YES;
[self.table reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)titleForHeaderInSection
atIndex:(NSInteger)index {
NSString *key = [self.keys objectAtIndex:index];
if (key == UITableViewIndexSearch) {
[tableView setContentOffset:CGPointZero animated:NO];
return NSNotFound;
}
else {
return index;
}
}
#end
h.file is:
#import <UIKit/UIKit.h>
#interface SellSharesViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *sellShares;
#property (weak, nonatomic) IBOutlet UILabel *rowSelectedDisplay;
#property (weak, nonatomic) IBOutlet UIButton *removeShare;
#property (strong, nonatomic) UIButton *menuBtn;
-(IBAction)removeShareButton:(id)sender;
#end
Feel free to ask for anything else that may help. Thanks.
Change the uitableview property to strong instead of weak. I do this to have a strong pointer to the tableview so it does not get deallicateded. Also you need to connect the delegate and datasource of the uitableview to the uiviewcontroller in the storyboard. So the protocol methods will be called in your uiviewcontroller. If they are not connected the protocol methods are not being called so nothing will show up in your table view.
If you have an array of dictionaries the fornumberofrows return [myarray count];
Connecting the datasource and delegate in the storyboard is overlooked a lot of times so check this first. I hope this helps!
Did you set Delegate and Datasource in the XIB?
Other things you need to check is ,
Put break point in below methods and check if it s firing
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Make sure you are not returning zero in the first two methods.
If either of first 2 methods are not firing then it means you did not added the datasource in XIB

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 :)

Using a UIpickerview in a xib file

i have created a xib file in Xcode 4.2 and i am looking at getting it to the tabbed application but i have been trying and I'm a little bit new to Xcode , coding so thats why i need your help i have my code below but there are a few little errors but what i would like you lot to help me with is maybe getting to a tabbed application... well telling me what to do anyhow. heres a video for you to have a look at in case i haven't explained it correct (this is my video i did just for this question http://www.youtube.com/watch?v=e7R5_kGClM0 hope the video help but more importantly you can help me.
There is the .h but the is no errors in this
#import <UIKit/UIKit.h>
#interface myview : UIViewController <UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>
#property (strong, nonatomic) IBOutlet UITableView* tableView;
#property (strong, nonatomic) IBOutlet UIPickerView* pickerView;
#property (strong, nonatomic) NSMutableArray* tableData;
#property (strong, nonatomic) NSMutableArray* pickerData;
#end
This is the .m but i will break it up
#import "myview.h"
#implementation myview
#synthesize tableView, pickerView, tableData, pickerData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
this is the -(void)viewDidLoad];etc..
- (void)viewDidUnload
{
[super viewDidUnload];
tableView.delegate = self;
tableView.dataSource = self;
pickerView.delegate = self;
pickerView.dataSource = self;
tableData = [[NSMutableArray alloc] init]; // table starts empty
pickerData = [[NSMutableArray alloc] initWithObjects:#"1", #"2", #"3", #"4", #"5", nil]; // picker starts with values 1, 2, 3, 4, 5
[tableView reloadData];
[pickerView reloadAllComponents]; // Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(bool)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
//The number of sections in UITableView
return 1;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
// The number of rows in the UITableView
return [tableData 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];
}
// Set the table cell text to the appropriate value in tableDate
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Whatever happens when you select a table view row.
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
// The number of sections in the UIPickerView
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
// The number of rows in the UIPickerView
return [pickerData count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
// The data for each row in the UIPickerView
return [pickerData objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// whatever you want to happen when a row is selected.
// here I am assuming you want to remove from the picker and add to the table on selection
[tableData addObject:[pickerData objectAtIndex:row]];
[pickerData removeObjectAtIndex:row];
[tableView reloadData];
[pickerView reloadAllComponents];
}
#end
Here Are The Errors In The Code There are Two
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
^^^^^^^^^^^^^^^^^
**Local declaration of 'tableView hides instance variable**
Second error
[pickerView reloadAllComponents];
^^^^^^^^^^^^^^
Local declaration of 'pickerView hides instance variable
Thanks Very Much I Hope To Here From You Soon !!!
Use this, "tableView hides instance variable" occurs if local variable and instance variable have the same name.
- (UITableViewCell*)tableView:(UITableView*)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [ [UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
and for pickerView use this
[self.pickerView reloadAllComponents];

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?

NSTableView not displaying data

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.

Resources