Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-11-2011, 07:30 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Question Custom UITableViewCell?

I'm trying to create a custom UITableViewCell, but it crashes (EXC_BAD_ACCESS) after cellForRowAtIndexPath is called.
Is there anything else required when returing a custom cell?

Code:
@interface MyTableViewCell : UITableViewCell {
    UILabel *myLabel1;
    UILabel *myLabel2;
}
@property (nonatomic,retain) UILabel *myLabel1;
@property (nonatomic,retain) UILabel *myLabel2;
@end
Code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *cellIdentifier = @"MyCell";

  MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

  if (cell == nil) {
     cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
  }

  cell.myLabel1.text = @"One";
  cell.myLabel2.text = @"Two";

  return cell;
}
I don't understand why it crashes when I use MyTableViewCell instead of UITableViewCell.
Isn't this the correct way to use custom cells?

But, it seems kind of odd it should work anyway, since the method is returning UITableViewCell.
samurle is offline   Reply With Quote
Old 10-11-2011, 08:36 AM   #2 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 60
GFish is on a distinguished road
Default

Quote:
Originally Posted by samurle View Post
I'm trying to create a custom UITableViewCell, but it crashes (EXC_BAD_ACCESS) after cellForRowAtIndexPath is called.
Is there anything else required when returing a custom cell?

Code:
@interface MyTableViewCell : UITableViewCell {
    UILabel *myLabel1;
    UILabel *myLabel2;
}
@property (nonatomic,retain) UILabel *myLabel1;
@property (nonatomic,retain) UILabel *myLabel2;
@end
Code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *cellIdentifier = @"MyCell";

  MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

  if (cell == nil) {
     cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
  }

  cell.myLabel1.text = @"One";
  cell.myLabel2.text = @"Two";

  return cell;
}
I don't understand why it crashes when I use MyTableViewCell instead of UITableViewCell.
Isn't this the correct way to use custom cells?

But, it seems kind of odd it should work anyway, since the method is returning UITableViewCell.

In the
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you need to load the CustomCell you created... something like this...

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
	
	MyTableViewCell *cell = (MyTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
    if (cell == nil) {
		NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:nil options:nil];
		
		for (id currentObject in topLevelObjects) {
			if ([currentObject isKindOfClass:[UITableViewCell class]]) {
				cell = (MyTableViewCell *) currentObject;
				break;
			}
		}		
    }
    
    // Configure the cell.
    cell.MyLabel1.text = @"One";
    cell.MyLabel2.text = @"Two";
	
    return cell;
}
should work fine...
GFish is offline   Reply With Quote
Old 10-11-2011, 01:57 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Question

Quote:
Originally Posted by GFish View Post
In the... you need to load the CustomCell you created... something like this...
should work fine...
Thanks, but I should also mention I'm not using NIBs or the Interface Builder.
This is all being done programmatically.

And the crash occurs after the method cellForRowAtIndexPath has returned, and not in any
specific place while the method is being called.

But, after much frustration, I did manage to find the bug after I turned on Zombie Objects for
first time. The bug was in my custom initWithStyle method, where I allocate both custom labels.

For some reason, I had to use "self.myLabel1 = variable", instead of "myLabel1 = variable".
I don't understand why these two lines aren't the same. I synthesized both variables:

@synthesize myLabel1;
@synthesize myLabel2;

Is there some Objective-C rule stating that I can't use synthesized variables (setters and getters)
in constructors (initializers)?
samurle is offline   Reply With Quote
Old 10-11-2011, 02:17 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by samurle View Post
Thanks, but I should also mention I'm not using NIBs or the Interface Builder.
This is all being done programmatically.

And the crash occurs after the method cellForRowAtIndexPath has returned, and not in any
specific place while the method is being called.

But, after much frustration, I did manage to find the bug after I turned on Zombie Objects for
first time. The bug was in my custom initWithStyle method, where I allocate both custom labels.

For some reason, I had to use "self.myLabel1 = variable", instead of "myLabel1 = variable".
I don't understand why these two lines aren't the same. I synthesized both variables:

@synthesize myLabel1;
@synthesize myLabel2;

Is there some Objective-C rule stating that I can't use synthesized variables (setters and getters)
in constructors (initializers)?

Code:
self.myLabel1 = variable;
and
Code:
myLabel1 = variable;
are indeed different. The first form uses the property. That code ends up calling the setter method for the property. If myLabel1 is set up as a retained property, the setter will retain the value assigned to it.

The second form does a simple assignment to the instance variable.

When you create a property for an ivar, you should really use the property to access the variable and stop using the ivar directly.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-11-2011, 04:22 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Code:
self.myLabel1 = variable;
and
Code:
myLabel1 = variable;
are indeed different. The first form uses the property. That code ends up calling the setter method for the property. If myLabel1 is set up as a retained property, the setter will retain the value assigned to it.

The second form does a simple assignment to the instance variable.

When you create a property for an ivar, you should really use the property to access the variable and stop using the ivar directly.

Thanks, but I think I still misunderstand some fundamental fact about synthesized variables.

So, you're saying I can't use them for setting?

I thought that was the reason why I synthesize them in the first place.
samurle is offline   Reply With Quote
Old 10-11-2011, 05:09 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by samurle View Post
Thanks, but I think I still misunderstand some fundamental fact about synthesized variables.

So, you're saying I can't use them for setting?

I thought that was the reason why I synthesize them in the first place.
The @synthesize directive asks the compiler to create a getter and a setter for a property you defined. That's all it does.

The statement:

[/code]
Code:
myLabel1 = variable;
changes the value of the underlying instance variable without invoking the setter. The code

[/code]
Code:
self.myLabel1 = variable;
on the other hand, invokes the setter, and does whatever housekeeping the setter is set up to do (including retaining the new object, if the property is a retained property.)

If you create a property, you should use the self.property syntax (or the alternate [self property] and [self setProperty: value] syntax) and not refer to the instance variable directly, unless you know what you're doing and why.

Note that if you are writing a custom setter or getter (instead of using the @synthesized version) then you have to refer to the instance variable directly, to avoid endless recursion. That's an exception. There are a few more, but until you have a reason to access the instance variable directly, and know why you're doing it, don't.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-11-2011, 05:28 PM   #7 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Thumbs up

Thanks, I have a better understanding now.
samurle is offline   Reply With Quote
Reply

Bookmarks

Tags
cellforrowatindexpath, custom, uitableviewcell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
19 members and 383 guests
13dario13, 7twenty7, buggen, eski, EvilElf, glenn_sayers, HemiMG, jarv, LunarMoon, morterbaher, n00b, pbart, Pudding, QuantumDoja, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:23 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0