Objective-C changing random values for variables - iphone

This really is my last resort as I am absolutely stumped and I just know it is something stupid!
I have a UITableView and a UISearchBar, the user uses the search bar to enter a location, which is then appended to a url with page=1. This is then sent to an api and a list of adverts are returned (this has been successful). The user can then scroll to the bottom of the UITableView and pull to load the next page of results (the page number is incremented and the api is called again, also successful).
If I hard code into the location variable the place "London" the adverts load fine for as many pages as possible, however when I use the searchBar.text (which seems to be correct), page 1 loads fine but page 2 crashes/url invalid. When I output the location variable it either isn't a string anymore and therefor crashes or it is some random data.
I have extensively searched online and found nothing and have been stuck on this for 2 days, any help would be greatly appreciated :)
My code is as follows:
PropertySearchTableViewController.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface PropertySearchTableViewController : UITableViewController <UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *propertiesTableView;
UISearchBar *propertiesTableSearch;
NSMutableArray *propertiesArray;
NSString *location;
}
#property (nonatomic, retain) IBOutlet UISearchBar *propertiesTableSearch;
#property (nonatomic, retain) NSMutableArray *propertiesArray;
#property (nonatomic, retain) NSString *location;
- (void)loadProperties;
- (void)callback:(NSDictionary *)response;
#end
PropertySearchTableViewController.m
#import "PropertySearchTableViewController.h"
#import "JSONHandler.h"
#implementation PropertySearchTableViewController
#synthesize location;
#synthesize propertiesArray;
#synthesize propertiesTableSearch;
int page = 1;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
location = #"London";
[self.propertiesTableSearch becomeFirstResponder];
self.propertiesArray = [[NSMutableArray alloc] init];
[self loadProperties];
self.title = #"Properties";
}
- (void)loadProperties {
NSLog(#"Calling: %#", ([location isKindOfClass:[NSString class]] ? location : #"No longer a string"));
NSString *url = [NSString stringWithFormat:#"http://local.somewebsite.co.uk/adverts/?where=%#&page=%i", location, page];
JSONHandler *handler = [[JSONHandler alloc] initWithUrl:url andData:nil callbackObject:self];
[handler handleConnection];
}
- (void)callback:(NSDictionary *)response {
NSArray *properties = [response objectForKey:#"results"];
NSEnumerator *enumerator = [properties objectEnumerator];
NSDictionary* item;
while (item = (NSDictionary*)[enumerator nextObject]) {
NSDictionary *d = item;
[propertiesArray addObject:d];
}
[self.tableView reloadData];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.propertiesArray count];
}
- (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] autorelease];
}
NSDictionary *d = [propertiesArray objectAtIndex:indexPath.row];
cell.textLabel.text = [d objectForKey:#"ad_title"];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGPoint p = scrollView.contentOffset;
CGSize s = scrollView.contentSize;
CGSize scrollSize = scrollView.bounds.size;
if((p.y+scrollSize.height) > (s.height+50)){
self.tableView.contentSize = CGSizeMake(0, s.height+50);
page++;
[self loadProperties];
}
}
//Search
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
self.tableView.scrollEnabled = NO;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text=#"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.tableView.scrollEnabled = YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
location = (NSString *) searchBar.text;
page = 1;
NSLog(#"%#", searchBar.text);
[propertiesArray removeAllObjects];
[self loadProperties];
[self.tableView reloadData];
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.tableView.scrollEnabled = YES;
}
#pragma mark -
#pragma mark Memory management
- (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 {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[location release];
[propertiesTableSearch release];
[super dealloc];
}
#end

This line's probably the culprit :
location = (NSString *) searchBar.text;
You need to retain/copy the string or it will go away!
[location release]; //!< Release the last location string
[location = [[searchBar text] copy]; //!< Get a copy of the new one

Related

Xcode Error with Linking View Controllers with UITableView

I have made an a Tabbed Bar Application in Xcode and the First Tab is Decors. I asked a previous question on integrating a XIB Project into a StoryBoard Tabbed Project.
I have Been Successfully in the integration of this, And it works but when I push on one of the Deocrs, Or Table Cells I get the following Error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "SelectedCellViewController" nib but the view outlet was not set.
If I put the Original Alert into my project The cell linking starts to work again.
Below are pics, links and code to my project, So you can see how it works
StoryBoard Project
XIB Project
DecorsViewController_iPhone.h
#import <UIKit/UIKit.h>
#interface DecorsViewController_iPhone : UIViewController
{
IBOutlet UITableView *tableViewDecors;
NSArray *sitesArray;
NSArray *imagesArray;
}
#property (nonatomic, retain) UITableView *tableViewDecors;
#property (nonatomic, retain) NSArray *sitesArray;
#property (nonatomic, retain) NSArray *imagesArray;
#end
DecorsViewController_iPhone.m
#import "DecorsViewController_iPhone.h"
#import "SelectedCellViewController.h"
#interface DecorsViewController_iPhone ()
#end
#implementation DecorsViewController_iPhone
#synthesize tableViewDecors;
#synthesize sitesArray;
#synthesize imagesArray;
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
// Load up the sitesArray with a dummy array : sites
NSArray *sites = [[NSArray alloc] initWithObjects:#"a", #"b", #"c", #"d", #"e", #"f", #"g", #"h", nil];
self.sitesArray = sites;
//[sites release];
UIImage *active = [UIImage imageNamed:#"a.png"];
UIImage *ae = [UIImage imageNamed:#"b.png"];
UIImage *audio = [UIImage imageNamed:#"c.png"];
UIImage *mobile = [UIImage imageNamed:#"d.png"];
UIImage *net = [UIImage imageNamed:#"e.png"];
UIImage *photo = [UIImage imageNamed:#"f.png"];
UIImage *psd = [UIImage imageNamed:#"g.png"];
UIImage *vector = [UIImage imageNamed:#"h.png"];
NSArray *images = [[NSArray alloc] initWithObjects: active, ae, audio, mobile, net, photo, psd, vector, nil];
self.imagesArray = images;
//[images release];
[super viewDidLoad];
}
#pragma mark - Table View datasource methods
// Required Methods
// Return the number of rows in a section
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [sitesArray count];
}
// Returns cell to render for each row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure cell
NSUInteger row = [indexPath row];
// Sets the text for the cell
//cell.textLabel.text = [sitesArray objectAtIndex:row];
// Sets the imageview for the cell
cell.imageView.image = [imagesArray objectAtIndex:row];
// Sets the accessory for the cell
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Sets the detailtext for the cell (subtitle)
//cell.detailTextLabel.text = [NSString stringWithFormat:#"This is row: %i", row + 1];
return cell;
}
// Optional
// Returns the number of section in a table view
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
#pragma mark -
#pragma mark Table View delegate methods
// Return the height for each cell
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 78;
}
// Sets the title for header in the tableview
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"Decors";
}
// Sets the title for footer
-(NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
return #"Decors";
}
// Sets the indentation for rows
-(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
return 0;
}
// Method that gets called from the "Done" button (From the #selector in the line - [viewControllerToShow.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView)] autorelease]];)
- (void)dismissView {
[self dismissViewControllerAnimated:YES completion:NULL];
}
// This method is run when the user taps the row in the tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
SelectedCellViewController *viewControllerToShow = [[SelectedCellViewController alloc] initWithNibName:#"SelectedCellViewController" bundle:[NSBundle mainBundle]];
[viewControllerToShow setLabelText:[NSString stringWithFormat:#"You selected cell: %d - %#", indexPath.row, [sitesArray objectAtIndex:indexPath.row]]];
[viewControllerToShow setImage:(UIImage *)[imagesArray objectAtIndex:indexPath.row]];
[viewControllerToShow setModalPresentationStyle:UIModalPresentationFormSheet];
[viewControllerToShow setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[viewControllerToShow.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView)]];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewControllerToShow];
viewControllerToShow = nil;
[self presentViewController:navController animated:YES completion:NULL];
navController = nil;
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Tapped row!"
// message:[NSString stringWithFormat:#"You tapped: %#", [sitesArray objectAtIndex:indexPath.row]]
// delegate:nil
// cancelButtonTitle:#"Yes, I did!"
// otherButtonTitles:nil];
// [alert show];
// [alert release];
}
#pragma mark - Memory management
- (void)didReceiveMemoryWarning {
NSLog(#"Memory Warning!");
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
self.sitesArray = nil;
self.imagesArray = nil;
[super viewDidUnload];
}
//- (void)dealloc {
//[sitesArray release];
//[imagesArray release];
// [super dealloc];
//}
#end
SelectedCellViewController.h
#interface SelectedCellViewController : UIViewController {
NSString *labelText;
UIImage *image;
IBOutlet UILabel *label;
IBOutlet UIImageView *imageView;
}
#property (nonatomic, copy) NSString *labelText;
#property (nonatomic, retain) UIImage *image;
#end
viewControllerToShow.m
#import "SelectedCellViewController.h"
#implementation SelectedCellViewController
#synthesize labelText;
#synthesize image;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
}
return self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[label setText:self.labelText];
[imageView setImage:self.image];
}
- (void)viewDidUnload {
self.labelText = nil;
self.image = nil;
//[label release];
// [imageView release];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
check the class inherited by the "The controller to be shown on click".XIB in the identity inspector .It seems you have not set the class for the "to be shown" XIB and the view outlet of the same XIB.

Xcode error - Thread 1: signal SIGABRT [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am creating a simple RSS application and I am not that good in Objective-c. The application will always build successful and there is no errors or warnings, in the UITableView which reads the RSS, whenever i press the cells it will terminate and in the main.m this thread will come "Thread 1: signal SIGABRT" in this line:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
The information of my app:
The app is created by Xcode version: 4.3.1
The app was created from the "Master-Detail Application" template for iPhone and on a MacBook.
The debugger I am using is LLDB and my iPhone simulator is 5.1
I am using Storyboard
Here is the Main.m:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
The AppDelegate.h is:
#import <UIKit/UIKit.h>
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
My AppDelegate.m is:
#import "AppDelegate.h"
#import "AppDelegate.h"
#import "MasterViewController.h"
#implementation AppDelegate
#synthesize window;
#synthesize navigationController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}
#end
This is the console message:
2012-03-17 17:32:29.498 Rahnavard[1862:12e03] fetch rss
2012-03-17 17:33:01.212 Rahnavard[1862:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/hassantavari/Library/Application Support/iPhone Simulator/5.1/Applications/48090189-E17C-40CF-9BF1-ACA18FC0B02B/Rahnavard.app> (loaded)' with name 'DetailViewController''
*** First throw call stack:
(0x16e4022 0x1875cd6 0x168ca48 0x168c9b9 0x366638 0x20c1fc 0x20c779 0x20c99b 0x20cd11 0x21e8fd 0x21eaef 0x21edbb 0x21f85f 0x21fe06 0x21fa24 0x393c 0x1d65c5 0x1d67fa 0xa6b85d 0x16b8936 0x16b83d7 0x161b790 0x161ad84 0x161ac9b 0x15cd7d8 0x15cd88a 0x145626 0x26a2 0x2615)
terminate called throwing an exception(lldb)
Here is were the fetch RSS:
-(void)fetchRss
{
NSLog(#"fetch rss");
NSData* xmlData = [[NSMutableData alloc] initWithContentsOfURL:[NSURL URLWithString: kRSSUrl] ];
NSError *error;
GDataXMLDocument* doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
if (doc != nil) {
self.loaded = YES;
GDataXMLNode* title = [[[doc rootElement] nodesForXPath:#"channel/title" error:&error] objectAtIndex:0];
[self.delegate updatedFeedTitle: [title stringValue] ];
NSArray* items = [[doc rootElement] nodesForXPath:#"channel/item" error:&error];
NSMutableArray* rssItems = [NSMutableArray arrayWithCapacity:[items count] ];
for (GDataXMLElement* xmlItem in items) {
[rssItems addObject: [self getItemFromXmlElement:xmlItem] ];
}
[self.delegate performSelectorOnMainThread:#selector(updatedFeedWithRSS:) withObject:rssItems waitUntilDone:YES];
} else {
[self.delegate performSelectorOnMainThread:#selector(failedFeedUpdateWithError:) withObject:error waitUntilDone:YES];
}
[doc autorelease];
[xmlData release];
}
MasterViewController.h:
#import <UIKit/UIKit.h>
#import "RSSLoader.h"
#import "DetailViewController.h"
#interface MasterViewController : UITableViewController<RSSLoaderDelegate> {
RSSLoader* rss;
NSMutableArray* rssItems;
}
#end
MasterViewController.m:
#import "MasterViewController.h"
#import "DetailViewController.h"
#implementation MasterViewController
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = #"RAHNAVARD";
self.navigationItem.prompt = #"LATEST NEWS";
rssItems = nil;
rss = nil;
self.tableView.backgroundColor = [UIColor whiteColor];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.tableView setIndicatorStyle:UIScrollViewIndicatorStyleWhite];
//self.tableView.tableHeaderView = [[TableHeaderView alloc] initWithText:#"fetching rss feed"];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (rss==nil) {
rss = [[RSSLoader alloc] init];
rss.delegate = self;
[rss load];
}
}
/*
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (rss.loaded == YES) {
return [rssItems count]*2;
} else {
return 1;
}
}
- (UITableViewCell *)getLoadingTableCellWithTableView:(UITableView *)tableView
{
static NSString *LoadingCellIdentifier = #"LoadingCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LoadingCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LoadingCellIdentifier] autorelease];
}
cell.textLabel.text = #"Loading...";
UIActivityIndicatorView* activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activity startAnimating];
[cell setAccessoryView: activity];
[activity release];
return cell;
}
- (UITableViewCell *)getTextCellWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
static NSString *TextCellIdentifier = #"TextCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TextCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TextCellIdentifier] autorelease];
}
NSDictionary* item = [rssItems objectAtIndex: (indexPath.row-1)/2];
//article preview
cell.textLabel.font = [UIFont systemFontOfSize:11];
cell.textLabel.numberOfLines = 3;
cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
CGRect f = cell.textLabel.frame;
[cell.textLabel setFrame: CGRectMake(f.origin.x+15, f.origin.y, f.size.width-15, f.size.height)];
cell.textLabel.text = [item objectForKey:#"description"];
return cell;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (rss.loaded == NO) {
return [self getLoadingTableCellWithTableView:tableView];
}
if (indexPath.row % 2 == 1) {
return [self getTextCellWithTableView:tableView atIndexPath:indexPath];
}
static NSString *CellIdentifier = #"TitleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
NSDictionary* item = [rssItems objectAtIndex: indexPath.row/2];
cell.textLabel.text = [item objectForKey:#"title"];
return cell;
}
/*
// 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:[NSArray arrayWithObject: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 -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
//DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.item = [rssItems objectAtIndex:floor(indexPath.row/2)];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
#pragma mark -
#pragma mark Memory management
- (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 {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[rssItems release];
rssItems = nil;
[rss release];
rss = nil;
[super dealloc];
}
#pragma mark -
#pragma mark RSSLoaderDelegate
-(void)updatedFeedWithRSS:(NSMutableArray*)items
{
rssItems = [items retain];
[self.tableView reloadData];
}
-(void)failedFeedUpdateWithError:(NSError *)error
{
//
}
#end
If you want more information just say it to me by answers and I will edit my question and then you will edit your answer.
I would really appreciate you help.
SIGABRT means in general that there is an uncaught exception. There should be more information on the console.
You are trying to load a XIB named DetailViewController, but no such XIB exists or it's not member of your current target.
SIGABRT is, as stated in other answers, a general uncaught exception. You should definitely learn a little bit more about Objective-C. The problem is probably in your UITableViewDelegate method didSelectRowAtIndexPath.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I can't tell you much more until you show us something of the code where you handle the table data source and delegate methods.

passing data from detail view to a table view ios 4

I have two views, the first (view A) is a table view. Every row has a disclosure button, when I click on the button I display the second view (view B) which is a table view too. When i click on a row, I want to pass the data to view A and display it as detailTextLabel.
#import <UIKit/UIKit.h>
#interface A : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSDictionary *listData;
NSArray *keys;
}
#property (nonatomic, retain) NSDictionary *listData;
#property (nonatomic, retain) NSArray *keys;
#end
#import "A.h"
#implementation A
#synthesize listData;
#synthesize keys;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[listData release];
[keys release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"Criteres" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.listData = dict;
[dict release];
NSArray *array = [[listData allKeys]sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
self.keys = array;
[super viewDidLoad];
}
- (void)viewDidUnload
{
self.listData = nil;
self.keys = nil;
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark Table View Data Source Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [keys count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [listData objectForKey:key];
return [nameSection count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *key = [keys objectAtIndex:section];
return key;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [listData objectForKey:key];
static NSString *SectionsTableIdentifier = #"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SectionsTableIdentifier] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
if (section == 0) {
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
if (row == 2)
cell.detailTextLabel.text = #"En m2";
if (row == 1)
cell.detailTextLabel.text = #"En €";
}
if (section == 1) {
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
}
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = 2;
return row;
}
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return indexPath;
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *ownerCell = [tableView cellForRowAtIndexPath:indexPath];
if ([ownerCell.textLabel.text isEqualToString:#"Ecole"]) {
B *prixview = [[B alloc]init];
[self.navigationController pushViewController:prixview animated:YES];
prixview.title = #"prix";
}
if ([ownerCell.textLabel.text isEqualToString:#"Crèche"]) {
B *prixview = [[B alloc]init];
[self.navigationController pushViewController:prixview animated:YES];
prixview.title = #"Prix";
}
}
#end
#import <UIKit/UIKit.h>
#interface B : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSArray *typetab;
NSInteger radioSelectionTypeBien;
NSString *radioSelectionTypeBienString;
}
#property (nonatomic, retain) NSArray *typetab;
#property (nonatomic, retain) NSString *radioSelectionTypeBienString;
#end
#import "B.h"
#implementation B
#synthesize typetab;
#synthesize radioSelectionTypeBienString;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[self.radioSelectionTypeBienString release];
[self.typetab release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Critères"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backButtonActionTypeBien:)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
radioSelectionTypeBien = -1;
NSArray *type = [[NSArray alloc]initWithObjects:#"Appartement",#"Maison",#"Commerce", nil ];
self.typetab = type;
[type release];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(IBAction)backButtonActionTypeBien: (id)sender
{
[self.navigationController pushViewController:self.navigationController.parentViewController animated:YES];
}
- (void)viewDidUnload
{
self.radioSelectionTypeBienString = nil;
self.typetab = nil;
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.typetab count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = [typetab objectAtIndex:indexPath.row];
if (indexPath.row == radioSelectionTypeBien){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
radioSelectionTypeBien = indexPath.row;
radioSelectionTypeBienString = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
[tableView reloadData];
}
#end
Usually i write a method in the controller B like:
-(voi)initVCwithString:(NSString*)_string andSomeObject:(NSObject *)_obj;
(so the controller B must have 2 ivar, a string and an object, that you have to set in this method)
then call this in your didSelectRowAtIndexPath: from the controller A
...
B _vcB=//alloc..init..etc
[_vcB initVCwithString:yourCell.textLabel.text andSomeObject:someObj];
[self.navigationController pushViewController:_vcB animated:YES];
[_vcB release];
Then in cellForRowAtIndexPath of B set cell.detailTextLabel.text=yourStringIvar;
Hope this helps.
Basically there are two possibilities that seems to be suitable. You can write a custom delegate method which pass the value to the A controller, or you can make a notification via notification centre, which can then assign the value to the A controller by catching the notification which will be triggered by picking a cell in the controller B.

Creating an array of pictures and creating avatars at the side of table

have only been learning objective C for a couple of days now and currently baffled by tables/ UITableView etc.. I've created a table and it lists 6 names I've read up how to set the avatar (image to the left of the text) to 1 picture however I have been playing about trying to set each individual image using an array. Heres what I have, no issues just the simulator crashes constantly? Thanks in advance.
#import <UIKit/UIKit.h>
#interface SImple_TableViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
NSArray * listData;
NSArray* listPics;
}
#property (nonatomic,retain) NSArray *listData;
#property (nonatomic,retain) NSArray* listPics;
#end
#import "SImple_TableViewController.h"
#implementation SImple_TableViewController
#synthesize listData, listPics;
- (void)dealloc
{
[listData release];
[listPics release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
NSArray *array = [[NSArray alloc] initWithObjects:#"Bradley", #"Glen",#"Alan",#"Sean",#"Mark",#"Luke",#"Jack", nil];
NSArray *pics = [[NSArray alloc] initWithObjects:#"ME.jpg",#"Sean.jpg",#"Glenn.jpg",#"Mark.jpg",#"Luke.jpg",#"Jack.jpg",#"Alan.jpg", nil];
self.listData = array;
// self.listPics = pics;
[array release];
[pics release];
[super viewDidLoad];
}
- (void)viewDidUnload
{
self.listData = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark Table View Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
return [self.listPics count];
}
-(UITableViewCell*)tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{ static NSString *SimpleTableIdentifier = #"SImpleTableIdentifier";
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier]autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
UIImage*image = [UIImage imageNamed: listPics];
cell.imageView.image = [listPics objectAtIndex:row];
return cell;
}
#end
Here's an important thing to remember: no code that occurs after return will ever be reached. FOr example:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listData count]; // METHOD RETURNS HERE
return [self.listPics count]; // THIS LINE IS NEVER REACHED
}
You can only return once.
This same error occurs in your method -(UITableViewCell*)tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
Here's another problem that I see. This line:
NSArray *pics = [[NSArray alloc] initWithObjects:#"ME.jpg",#"Sean.jpg",#"Glenn.jpg",#"Mark.jpg",#"Luke.jpg",#"Jack.jpg",#"Alan.jpg", nil];
creates an array of NSString objects. If you wish to put UIImages into an array, then in place of #"ME.jpg", you should have [UIImage imageNamed:#"ME.jpg"]. Make sure to add the image, with the exact same name (case sensitive) to the project.

iPhone App - Trouble Getting Cells To Display Array Data

I have a simple table view which I have built using an example from a book but it doesn't work..
It is supposed to take the values from an array and display them in the cells within a table view. I have connected the table views dataSource and delegate to the file's owner and have the following code in my controller class:
Simple_TableViewController.h
#interface Simple_TableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *listData;
}
#property (nonatomic, retain) NSArray *listData;
#end
Simple_TableViewController.m
#import "Simple_TableViewController.h"
#implementation Simple_TableViewController
#synthesize listData;
-(void)viewdidLoad{
NSArray *array = [[NSArray alloc] initWithObjects:#"Sleepy", #"Sneezy", #"Bashful", #"Happy", #"Doc", #"Grumpy", #"Dopey", #"Thorin",#"Dorin", #"Nori", #"Ori", #"Balin", #"Dwalin", #"Fili", #"Kili", #"Oin", #"Gloin", #"Bifur", #"Bofur", #"Bombur", nil];
self.listData = array;
[array release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.listData = nil;
[super viewDidUnload];
}
- (void)dealloc {
[listData release];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listData count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
#end
The project succeeds when compiling, but no text displays in the table cells...
UPDATE
#import <UIKit/UIKit.h>
#interface Simple_TableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *tv;
NSArray *listData;
}
#property (nonatomic, retain) NSArray* listData;
**#property (nonatomic, retain) UITableView* tv;**
#end
#import "Simple_TableViewController.h"
#implementation Simple_TableViewController
#synthesize listData, tv;
-(void)viewdidLoad{
NSArray *array = [[NSArray alloc] initWithObjects:#"Sleepy", #"Sneezy", #"Bashful", #"Happy", #"Doc", #"Grumpy", #"Dopey", #"Thorin",#"Dorin", #"Nori", #"Ori", #"Balin", #"Dwalin", #"Fili", #"Kili", #"Oin", #"Gloin", #"Bifur", #"Bofur", #"Bombur", nil];
self.listData = array;
**[tv reloadData];**
[array release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.listData = nil;
[super viewDidUnload];
}
- (void)dealloc {
[listData release];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listData count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSString *msg = #"message";
NSLog(#"%#", msg);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
#end
You're missing the numberOfSections datasource method.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
Also after changing the data in your array, you'll want to reload the table.
[yourTableViewHere reloadData];
From your last comment,you made tableview from xib file but where is IBOutlet correspond to tableview. You have to make an IBOutlet UITableView *tableview and connect it with your tableview in xib file