 |
 |
|
 |
09-12-2008, 09:37 PM
|
#1 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 158
|
Custom UITableViewCell
I have a view xib that contains just a UITableView, which loads fine, I have it listing out data from my database. But I want to use a custom UITableViewCell, so I added one to the same xib. The reason is so that I can design it in Interface Builder instead of trying to code it.
I changed the class of the ListViewCell to a class I created that extends UITableViewCell. I've tried two ways of allocating this class in the cellForRowAtIndexPath method. First I tried:
Code:
cell = [[[ListViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
But that doesn't seem to use the design from within Interface Builder.
I then tried setting an IBOutlet on my UITableViewController class, and linked it to the TableViewCell using IB, and then this line:
Code:
cell = [[listViewCell initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
But as you might imagine, it doesn't properly create a new ViewCell for each row, it gets messed up: the first two become blank and only the last row actually has a design to it.
|
|
|
09-12-2008, 09:39 PM
|
#2 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 158
|
Basically I was looking for something similar to initWithNibName, but that's not available on UITableViewCell.
|
|
|
09-12-2008, 10:26 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 158
|
I thought I was on the right path, but no luck so far...here's what I tried:
Create another UIView set: UIViewController class, UIView class and a xib file, set the propreties in the xib to the respective classes.
In the initWithFrame method for the UITableViewCell class that was created previously, add this:
Code:
CGRect tzvFrame = CGRectMake(0.0, 0.0, self.contentView.bounds.size.width, self.contentView.bounds.size.height);
ListViewCellController *listViewCellController = [[ListViewCellController alloc] initWithNibName:@"ListViewCellView" bundle:nil];
listViewCellView =[listViewCellController.listViewCellView initWithFrame:tzvFrame];
listViewCellView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:listViewCellView];
I thought that might work, but no luck. What's odd is that I get no errors, just a blank cell.
|
|
|
09-12-2008, 11:07 PM
|
#4 (permalink)
|
|
New Member
Join Date: Sep 2008
Posts: 1,431
|
Using alloc/init or loading a nib are orthogonal methods for creating objects. Use one or the other. If you load a nib you get one of each of the objects that are freeze-dried in that nib. If you need more than one you need to load the nib again, and again.
It's possible that you could create a nib that holds a single instance of a UITableViewCell subclass and then load that nib many times using the apis in UINibLoading.h. If you like that method then it should work fine. I agree that laying out a UI in code is so 20th century.
Having said that, I generate my UITableViewCells in code. You seem to be mixing these two strategies with no good results. Give that up. One or the other.
|
|
|
09-12-2008, 11:27 PM
|
#5 (permalink)
|
|
Registered Member
Join Date: Aug 2008
Posts: 158
|
Well I gave up, I'm just going to lay it out programatically. I'm not about to waste time over this when I have the rest of the app to build.
|
|
|
08-24-2009, 09:03 PM
|
#6 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 2
|
In IB create the UItableviewcell and save it. Select write class files under menu and add it to the project.
you have to write the initwithcgrectframe ( dont remember the exact function name) by yourself and in the initialization function do this:
if(self=[ super init])
{
self=[ [ [ NSBundle mainBundle ] loadNibNamed:@"NIB name without extension" owner:self options:nil] lastObject];
}
return self;
that should do the trick
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 431 |
| 44 members and 387 guests |
| anandahdaa, bensj, bobvanratingen, BrianSlick, CAmaninacan, craiggil, Eagle11, efreeet, Erle, firearasi, flamingliquid, fredidf, ftm, gandohr, gbaldwin9, georgeburns, Gi-lo, goodcode, harrytheshark, HemiMG, hm50, hoisty, JasonR, javaconvert, Leopard, mac514, Meoz, NeoNate, Noise, not_too_shabby, paolo, prathumca, redon, Roi, Rudy, scatterbrn, schoash, scotopia, shad1236, shark4ever, The Blind Bandit, wassupdoc, yannickd60, ZunePod |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,296
Threads: 39,091
Posts: 171,393
Top Poster: smasher (2,575)
|
| Welcome to our newest member, Issue |
|