Initialize NSMutableArray - objective-c

Im very new in xcode 3 and i really need help for this
I developed my apps using UITableView and XML to showed the content.
i had 3 .xib files which it rootViewController , SecondViewController and mainview.
So the problem is:
When i try to executed didSelectrow in rootViewController and access the NSMutableArray *array in SecondViewController and replace the *array value with new array value in rootViewController before pushed animation.
The array value on my SecondViewController was changed for the first time but when i pushed the back button and select the other row, my SecondViewController array kept read the previous array not change to a new one. I try to initialize but no luck
This is my code on rootViewController UITableview (didSelectRow):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(2ndController == nil)
2ndController = [[DetailViewController alloc] initWithNibName:#"SecondViewController" bundle:[NSBundle mainBundle]];
//Declare xml NSMutable array record
ListRecord *record = [self.entries objectAtIndex:indexPath.row];
//access SecondViewController NSMutable *record
2ndController.record = [[[NSMutableArray alloc] init] autorelease];
//inserting the value from firstview to secondview before push
2ndController.record = record;
//push animation
[self.navigationController pushViewController:2ndController animated:YES];
}
This is my second view controller :
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
switch(indexPath.section)
{
case 0:
[cell setText:record.name];
break;
case 1:
[cell setText:record.Age];
break;
case 2:
[cell setText:record.summary];
break;
}
return cell;
}
Hope someone can help me..
Thanks in advance.....

Few things,
You do,
2ndController.record = [[[NSMutableArray alloc] init] autorelease];
and follow it up with
[cell setText:record.name];
Clearly, the record property doesn't seem to be an instance of NSMutableArray so I think the array initialization part is incorrect as you do, and already mentioned,
2ndController.record = record;
But the problem I think is that you are retaining your UITableViewController subclass. Have you tried reloading the data?
[self.tableView reloadData];
Add it in the viewWillAppear method of your DetailViewController.

You should look at these two lines again:
2ndController.record = [[[NSMutableArray alloc] init] autorelease];
//inserting the value from firstview to secondview before push
2ndController.record = record;
The first line doesn't do anything useful for you. It creates and initializes a new NSMutableArray and sets the record property to that new array.
But then in the very next line you set the same 'record' property again to a different object and so that array in the first line is no longer referenced. So you might as well not have ever created it.
That's not your issue exactly, but this comment was too big for a comment. :)

Related

TableViewCell not displaying layout

Here's a question about tableViewCell. I am trying my hand at table views for the first time and trying to get my head around reusable cells etc. I have managed to get it working to some extent. I have a tableView that lives on a child view controller which is its delegate and data source. Code for delegate:
#import "ChildViewController.h"
#interface ChildViewController ()
#property NSArray *titles;
#property NSArray *thumblenails;
#end
#implementation ChildViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.titles = [NSArray arrayWithObjects:#"Title 1", #"Title 2", #"Title 3", #"Title 4",nil];
self.thumblenails = [NSArray arrayWithObjects:#"Image1", #"Image2", #"Image3", #"Image4", nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.titles.count;
}
-(UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// cell.textLabel.backgroundColor = [UIColor greenColor];
cell.textLabel.text = [self.titles objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[self.thumblenails objectAtIndex:indexPath.row]];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I also have a SimpleTableViewCell.xib:
I have set the class on the identity inspector to SimpleTableViewCell and imported the SimpleTableViewCell.h file to my ChildViewController.h. I have also set the identifier to "cell" in the attributes inspector of the Table view cell, but here is the thing. As I am not getting the look I want for my cell, I have tried misspelling it and nothing has changed so clearly the identifier is not doing anything. When I run my app I get this:
So the array of images and titles are being loaded but not the actual cell size and labels that I set on the xib file for the cell. I have tried changing something in the cell, like the background color in the attributes inspector.
and on the xib it looks like this:
but when I run it:
I am guessing all this is because the cell identifier is not actually linked, but I thought I had done that in the line:
SimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
Am I missing some important coding in here, linking something in the xib, or is it just a typo? any suggestions greatly appreciated.
//////////////////////////////////////////////////////////////////////UPDATE1////////////////////////////////////////////////////////////////////////
I have tried the second suggestion and the result is this:
So, hopefully this will throw some light into what I am doing wrong?
Also, I have corrected the original text, when I change the color to green it's using the attributes inspector.
Thanks for your help.
//////////////////////////////////////////////////////////////////////UPDATE2////////////////////////////////////////////////////////////////////////
I have removed the lines:
cell.textLabel.text = [self.titles objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[self.thumblenails objectAtIndex:indexPath.row]];
Also, added the code:
[self.tableView registerNib:[UINib nibWithNibName:#"SimpleTableCell" bundle:nil] forCellReuseIdentifier:#"cell"];
and the code:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [UIScreen mainScreen].bounds.size.width * (0.3);
}
Now my table looks like this:
So question now is how do I add the actual values of the original arrays of titles and images?
you should set the height of cell to get the exact same look in the nib file. use this delegate method to set height of cell.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [UIScreen mainScreen].bounds.size.width * (0.3);// 0.3 should be your aspect ratio for cell:
// for cell.height/ cell.width in nib file.
}
You forgot to register nib for cell identifier.
Put [self.tableView registerNib:[UINib nibWithNibName:#"SimpleTableViewCell" bundle:nil] forCellReuseIdentifier:#"cell"]; to viewDidLoad method.
And you can remove
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] in ios 7

I getting this error while using two UICollectionView.
i search for this error and get solution that collection view should initialise with registerNib but in my coding i have already done this.
my viewDidLoad looks like this
UINib *cellNib =[UINib nibWithNibName:#"myCell" bundle:nil];
[self.horizontalCollectionView registerNib:cellNib forCellWithReuseIdentifier:#"myCell"];
UICollectionViewFlowLayout *horizontalFlowLayout=[[UICollectionViewFlowLayout alloc]init];
[horizontalFlowLayout setItemSize:CGSizeMake(300, 150)];
[horizontalFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.horizontalCollectionView setCollectionViewLayout:horizontalFlowLayout];
UINib *cellNib1 =[UINib nibWithNibName:#"myCell1" bundle:nil];
[self.horizontalCollectionView registerNib:cellNib forCellWithReuseIdentifier:#"myCell"];
UICollectionViewFlowLayout *verticalFlowLayout=[[UICollectionViewFlowLayout alloc]init];
[verticalFlowLayout setItemSize:CGSizeMake(150, 150)];
[verticalFlowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
NSArray *a=[[NSArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",nil];
[self.verticalCollectionView setCollectionViewLayout:verticalFlowLayout];
self.dataArray=[[NSArray alloc]initWithObjects:a, nil];
and cellForItemAtIndexPath method is looks like below.
if (collectionView==self.horizontalCollectionView)
{
NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
NSString *cellData = [data objectAtIndex:indexPath.row];
static NSString *cellIdentifier = #"myCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:1];
titleLabel.textColor=[UIColor whiteColor];
[titleLabel setText:cellData];
return cell;
}
else
{
NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
NSString *cellData = [data objectAtIndex:indexPath.row];
static NSString *cellIdentifier1 = #"myCell";
UICollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier1 forIndexPath:indexPath];
UILabel *titleLabel = (UILabel *)[cell1 viewWithTag:2];
titleLabel.textColor=[UIColor whiteColor];
[titleLabel setText:cellData];
return cell1;
}
getting error while initialising cell1.
Full error message is as below.
* Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /SourceCache/UIKit_Sim/UIKit-2935.137/UICollectionView.m:3241
I've just had this. For me the issue was:
Collection Reusable View Identifier in the nib was set to something different than the identifier I had registered in code with [self.collectionView registerNib:nib forCellWithReuseIdentifier:aNib];
So I spent a total of 3 hours with this stupid issue. For me it was bad because other cells i did the exact same way worked except this one. This error is misleading. What it was for me was i had a GestureRecognizer in the xib... YUP it doesn't even have to be linked. Once i took that out it worked. I ended up just adding the gesture recognizer via code.
Odd because it works fine for custom UITableViewCell but not custom UICollectionViewCell.
Hope this helps anyone!
- Did you used Xib or Storyboard for cell?
I find if I Used Xib and in xib named it's Identifier "cell"
But I Used
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"reuseIdentifier"] in my code;
this Crashed
I change xib's Identifier as the same in my code, it run as well;
You should add to viewDidLoad method the following lines:
UINib *nib = [UINib nibWithNibName:#"CollectionViewCell" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:#"CollCell"];
Where CollectionViewCell is the name of the custom CollectioViewCell nib file, and CollCell is the identifier you use to dequeue the Cell in (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath method
I hope this help you

Custom NSTableView with custom NSTableCellView?

I would like to create an NSTableview with custom NSTableCellViews.
Here is what I have right now:
A nib file for the cell (view nib) called CustomCell.xib
A custom class for my cell called CustomCell
And the code in my AppDelegate.m:
Here I create my table view programmatically:
NSScrollView *tableContainer = [[NSScrollView alloc]initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];
NSTableView *tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];
NSTableColumn *firstColumn = [[[NSTableColumn alloc] initWithIdentifier:#"firstColumn"] autorelease];
[[firstColumn headerCell] setStringValue:#"First Column"];
[tableView addTableColumn:firstColumn];
tableView.dataSource = self;
tableView.delegate = self;
[tableContainer setDocumentView:tableView];
tableContainer.autoresizingMask = NSViewHeightSizable | NSViewMinXMargin;
[self.window.contentView addSubview: tableContainer];
And here is the delegate method where I would like to put my custom cell code:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// In IB the tableColumn has the identifier set to the same string as the keys in our dictionary
NSString *identifier = [tableColumn identifier];
if ([identifier isEqualToString:#"myCell"]) {
// We pass us as the owner so we can setup target/actions into this main controller object
CustomCell *cellView = [tableView makeViewWithIdentifier:identifier owner:self];
// Then setup properties on the cellView based on the column
cellView.textField.stringValue = #"Name";
return cellView;
}
return nil;
}
In the nib file for my custom cell I have hooked up the cell view with my custom class called CustomCell which subclasses NSTableCellView. I have not done any other steps as for now. So my CustomCell.m is just default initialization code. I haven't touched it. And I did not do anything else in my nib file, so I did not change file's owner or anything like that because I don't really know what to do.
Can anyone help out ? I looked at sample files from the Apple documentation, but after days of researching I have not found any solutions. I would really appreciate if you could help me.
This is what I ended up doing :
Of course you have to subclass NSTableCellView and return it like I did below. If you are familiar with table views in iOS you should be familiar with methods like:
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
//this will be called first. It will tell the table how many cells your table view will have to display
return [arrayToDisplay count];
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
//this is called after the count of rows is set. It will populate your table according to the data your array to display contains
[tableView setTarget:self];
[tableView setAction:#selector(click)];
NSString *identifier = [tableColumn identifier];
if ([identifier isEqualToString:#"TheCell"]) {
CustomCell *cellView = [tableView makeViewWithIdentifier:identifier owner:self];
cellView.cellText.stringValue = [arrayToDisplay objectAtIndex:row];
return cellView;
}
return nil;
}
And the click method that is triggered when a row is selected would look like this:
-(void)click{
int index = [table selectedRow];
// Do something with your data
//e.g
[[arrayToDisplay objectAtIndex:index] findHiggsBoson];
}
And something that has to be added to the NSTableView:
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:#"column"];
column.width = self.frame.size.width;
[tableView addTableColumn:column];
You do not need to subclass NSTableView to have custom NSTableViewCell subclasses.
You might consider using a view-based Table View also...

Error: unrecognized selector sent to instance

I have a custom object, Spaces:
#import "Spaces.h"
#implementation Spaces
#synthesize spaceName;
#synthesize spaceUsers;
#synthesize spaceIcon;
#synthesize spaceID;
#synthesize imageURLString;
- (void)dealloc
{
[spaceName release];
[spaceUsers release];
[spaceIcon release];
[imageURLString release];
[super dealloc];
}
#end
My root view controller implements a cellForRowAtIndexPath: and grabs from an NSArray of Spaces:
[[cell spaceName] setText:aSpace.spaceName];
[[cell spaceChatType] setText:#"People"];
[[cell spaceActiveUsers] setText:aSpace.spaceUsers];
This works fine and I can click to go into the detail view and back to the list, but after maybe 5-6 clicks back and forth between the table view and detail view, I get an error at [[cell spaceName] setText:aSpace.spaceName]; which is
'-[__NSCFSet spaceName]: unrecognized selector sent to instance 0x6047b90'"
Please help! Any insight will be very appreciated!
UPDATE:
I'm still getting the same error but I've narrowed it down to the this:
-I'm creating a detail view controller on didSelectRowAtIndexPath...
-The detail view is being pushed to the viewcontroller and displays fine, I have a back button added as well.
-The detail view loads information and refreshes on a timer
-Pressing the back button goes back to the table list view
This is the problem my detail view is not being released from memory so the more I go back and forth between the views the more timers were going off simultaneously. I added a check to viewWillDisappear that stops the timer by setting a bool value.
I noticed that the detail view is not unloading...
From the RootViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//no longer on initial view
isinit = NO;
//hide keyboard
[spacesSearch resignFirstResponder];
if (spaces != nil && spaces.count > 0)
{
//set back button reference
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Spaces" style:UIBarButtonItemStylePlain target:self action:#selector(returnSpacesList:)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
DetailViewController *details = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
//Grab Data from selected Space object and pass to DetailViewController
Spaces *aSpace = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
if ([self.filteredListContent count] == 0)
{
//self.lastSearchText
NSLog(#"Create new space code!");
}
else
{
aSpace = [self.filteredListContent objectAtIndex:indexPath.row];
}
}
else
{
aSpace = [spaces objectAtIndex:[indexPath row]];
}
//set title and display
self.navigationController.title = [NSString stringWithFormat:#"/%#/",aSpace.spaceName];
//pass data
[details passedValue:aSpace.spaceID :aSpace.spaceName];
[self.navigationController pushViewController:details animated:YES];
[aSpace release];
[details release];
}
}
How can I force the detail view to be released from memory?
Thank you
It sounds like [cell spaceName] has been autoReleased. I cannot see how you have defined that, but take a look at that part of your code.
If you need more help, you need to provide more code.
Perhaps your aSpace = [spaces objectAtIndex:[indexPath row]];
is not returning a Space object. Perhaps before you try and use it you test to make sure with something like if ([aSpace class] == [Spaces class])

Objective-C TableView Select Item Troubles

I'm having trouble having my app respond when an Item is selected in the table view. I'm running everything from my app delegate (the table functions that is like dataSource and TitleForHeaderAtSection etc) which are all being called fine. However it is not calling my selection method when I tap on a item in the list. I even put a NSLog to see just in case.
Here's my code: (its quite long and extensive and I know theres crap in there that doesn't need to be there but I put all of it just in case you needed it...)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Selected Row.", #"");
//Get the selected country
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSDictionary *dictionary = [data objectAtIndex:indexPath.section];
//NSArray *array = [dictionary objectForKey:#"My Wishlists"];
//NSString *selectedWishlist = [array objectAtIndex:indexPath.row];
//Initialize the detail view controller and display it.
WishlistDetailView *dvController = [[WishlistDetailView alloc] initWithNibName:#"WishlistDetailView" bundle:[NSBundle mainBundle]];
dvController.selectedWishlistId = [wishlistids objectAtIndex:indexPath.row];
NSLog(#"Selected row with wishlist id: %#", dvController.selectedWishlistId);
[[self navController] pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
The code compiles with NO errors.
Thanks for your persistant help!!
Christian Stewart
(by the way both of the selection allowed checkboxes are checked in Interface builder.)
tableView:didSelectRowAtIndexPath: is a UITableViewDelegate method. Is you controller the delegate of the table view?