UITableViewCell - super dealloc EXC_BAD_ACCESS - objective-c

What could cause a [super dealloc]; to crash?
So I have this in my UITableViewCell
- (void)dealloc
{
// this is ok
[others release];
// this crash
[super dealloc];
}
Basically I have a UITabViewController. When I navigate to another tab, and come back to this tab that contains the UITableViewCell, it will crash at [super dealloc];
This is how the cell is getting called.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"DynamicSizedCell";
DynamicSizedCell *cell = (DynamicSizedCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[DynamicSizedCell alloc] init] autorelease];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
Thanks,
Tee

Related

TableViewCell subclass not getting initialized

I want to create a custom tableViewCell programmatically.
This is what I do:
Creating a tableViewCell subclass and importing it to the tableViewController
In the tableViewController m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"StoreCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;}
In the CustomCell m:
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSLog(#"Hello!");
}
return self;
}
(Sorry for not getting the code highlighting thing to work)
My problem is that the CustomCell don't get initialized. The initWithStyle never gets triggered. I followed a couple of tutorials and they do the exact same thing, but successfully..
With iOS 6, dequeReusableCellWithIdentifier:forIndexPath: always returns a cell, so your if-case never gets called.
If a cell with that identifier is not available, it initializes it itself.
Try implementing initWithCoder: in the UITableViewCell subclass, that's what gets called in that case.
In cellForRowAtIndexPath try this
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"StoreCell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
I finally figured it out. The cells didn't get initialized because I was using prototypes cells in the storyboard. I set the prototypes cells to 0 and it works :)

iOS 5, Storyboards, ARC: UITableview doesn't get populated with data from custom delegate and datasource

This is my first time I am working on UITableView connected to custom delegate and datasource. Until today, I used to connect to "self". The prequel for this question is here.
So, I have two additional UIViews. Using segmented control I put one of the over mentioned UIViews on self.view...
Have created two UITableView subclasses and set them as delegate and datasource. Works fine, no leaks or crashes. Checked if the classes getting initialized on segmentedControl index change:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
NSLog(#"Nominals got init");
}
return self;
}
NSLog works when changing segment.
The question:
In my custom delegate class I override the methods required by UITableViewDelegate and UITableViewDataSource protocols.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(12, 12, 200, 12)];
lbl.text=#"some nominal";
cell.textLabel.text=#"Nominal";
[cell addSubview:lbl];
return cell;
}
At moment I am getting exception:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
Well, isn't the cell in my custom delegated class is the same cell of my UITableView I've created:
-(void)populateNominals:(int)subCountryID
{
NominalsTableViewDelegate *del=[[NominalsTableViewDelegate alloc]init];
[self setNominalsDelegate:del];
UITableView *nominalsTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 372) style:UITableViewStylePlain];
[nominalsTableView setDelegate:nominalsDelegate];
[nominalsTableView setDataSource:nominalsDelegate];
[nominalsTableView reloadData];
[self.nominalsView addSubview:nominalsTableView];
}
what's my mistake? Thank you in advance.
You aretrying to dequeue a cell using dequeueReusableCellWithIdentifier. If there is no cell in the queue what happens ? Definitely crash.
So you need to allocate a cell, add this line too:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] init];
}
On your tableView:cellForRowAtIndexPath: method, you are missing the case where there are no cells to be dequeued. Take a look at your code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(12, 12, 200, 12)];
lbl.text=#"some nominal";
cell.textLabel.text=#"Nominal";
[cell addSubview:lbl];
return cell;
}
You should do something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] init];
}
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(12, 12, 200, 12)];
lbl.text=#"some nominal";
cell.textLabel.text=#"Nominal";
[cell addSubview:lbl];
return cell;
}
because by doing that, if you can't dequeue a cell, you're creating a new one.
the dequeueReusableCellWithIdentifier will return UITableViewCell instances when the cells are being reused while scrolling through the tableView.
So add
if (nil == cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
You have to use the initWithStyle:reuseIdentifier: which is the designated initializer for tableview cells.

Return a custom cell from cellForRowAtIndexPath:

I ran into a trouble and I'm looking for help.
I need to save a identifier in a cell, so I did a subclass
of UITableViewCell and added to it the identifier property. In my view
controller I create the cells as follow:
- (SidebarCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* tableViewCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
SidebarCell * cell = (SidebarCell *)tableViewCell;
Category *category = [categoryDataController.categories objectAtIndex:indexPath.row];
cell.textLabel.text = category.name;
if (category.checked == 1) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
The problem is that when I select one of the cells, the method
cellForRowAtIndexPath: returns an UTableViewCell object instead
of a SidebarCell, so I don't have access to the identifier property:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
SidebarCell *cell = (SidebarCell *)[tableView cellForRowAtIndexPath:indexPath]; // Trying to cast from UITableViewCell to SidebarCell
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[CategoryDataController updateRow:cell.identifier status:1]; // Here the app crashes
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
[CategoryDataController updateRow:cell.identifier status:0];
}
}
is there a way for cellForRowAtIndexPath: to return an Sidebarcell object?
Thanks beforehand.
Edit
The SidebarCell.h
#import <UIKit/UIKit.h>
#interface SidebarCell : UITableViewCell
#property (nonatomic) NSInteger identifier;
#end
SidebarCell.m:
#import "SidebarCell.h"
#implementation SidebarCell
#synthesize identifier;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
The error:
2012-04-24 09:21:08.543 Dongo[2993:11603] -[UITableViewCell identifier]: unrecognized selector sent to instance 0x6d87d50
2012-04-24 09:21:08.571 Dongo[2993:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell identifier]: unrecognized selector sent to instance 0x6d87d50'
*** First throw call stack:
(0x14b1052 0x1a65d0a 0x14b2ced 0x1417f00 0x1417ce2 0x116bc 0x48e71d 0x48e952 0xd1686d 0x1485966 0x1485407 0x13e87c0 0x13e7db4 0x13e7ccb 0x239d879 0x239d93e 0x3fea9b 0x2e85 0x2715 0x1)
terminate called throwing an exception(lldb)
You should leave the tableView:cellForRowAtIndexPath: method signature intact: it should return (UITableViewCell *) and not your custom cell class. Since your SidebarCell is a subclass of UITableViewCell, it should all "just work". No need to call super or any of that:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SidebarCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
cell = [[SidebarCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"CellIdentifier#"];
}
// ... set up the cell here ...
return cell;
}
In other methods, the cast should work just as you have it:
SidebarCell *cell = (SidebarCell *)[tableView cellForRowAtIndexPath:indexPath];
UITableViewCell* tableViewCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
The above line of code must be returning a UITableViewCell and not a SidebarCell.
SidebarCell * cell = (SidebarCell *)tableViewCell;
Then on your second line you cast the cell to a SidebarCell, but you are simply lying to the compiler and telling it that it should be a SidebarCell when it is in fact not. When you cast something it needs to start out as the correct type.
Instead of those two lines, you should be creating an actual SidebarCell. How you do this depends on how you have defined it.
Let me know if it is in a storyboard, xib, or created in code and I can provide an example if needed.
EDIT
Since you are creating the cell in code, you simply need to replace the first two lines in cellForRowAtIndexPath with this:
SidebarCell * cell = [[SidebarCell alloc] init];
Everything else looks like it should work as-is.
In my case i have used custom cell of a class tempTableViewcell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = #"cellidentifier";
tempTableViewcell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = #"abc";
return cell;
}

Why does my application crash with the following error?

When I scroll my table view up and down, after about 6-8 times my application crashes and I get the following in debug window:
myapp[250:207] *** -[NSIndexPath row]: message sent to deallocated instance 0xdd0eab0
Here is my code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [remoteRecipientItems count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"RemoteRecipientItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];
// Configure the cell...
[[cell textLabel]setText:[remoteRecipientItems objectAtIndex:[indexPath row]]];
cell.accessoryType = (row == oldRow && lastIndexPath !=nil)? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = (lastIndexPath !=nil)?[lastIndexPath row]:-1;
if (newRow != oldRow) {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated: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;
remoteRecipientItems = nil;
remoteRecipientID = nil;
xmlData = nil;
lastIndexPath = nil;
}
- (void)dealloc {
[remoteRecipientItems release];
[remoteRecipientID release];
[xmlData release];
[lastIndexPath release];
[super dealloc];
}
You don't own the indexPath variable, so you need to retain it.
Try replacing this:
lastIndexPath = indexPath;
With this:
lastIndexPath = [indexPath retain];

iOS: Push array to UITableView

Can you run me through the steps to push an array to a UITableview?
I'm new to Cocoa/Objective-C, so could you explain how the .m, .h work with the delegate?
Do this:
#interface SomeInstance : UITableViewController {
NSArray *theArray;
}
#end
#implementation SomeInstance
- (void)viewDidLoad {
theArray = [[NSArray alloc] initWithObjects:#"Apple",#"Pineapple",#"Banana",nil];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [theArray 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];
}
cell.textLabel.text = [theArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Selected a row" message:[theArray objectAtIndex:indexPath.row] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)dealloc {
[theArray release];
[super dealloc];
}
#end
If you're new to ObjC, I would suggest starting with something more simple before diving into UITableViews.
(sorry if that's not what you want to hear, but I figure you'll be more productive in the long run).
To answer your specific question, A class that inherits from UITableViewControllerDelegate is expected to support various methods required by the UITableViewController protocol.