Reputation: 1387
I saw there are lots of resources available on net regarding this question. I have to load a different XIB (UIViewContoller
) file for my cell. I have designed my cell looks there and I want to load that design in my cell.
I wrote this code but I am getting an exception.
-[UIView setTableViewStyle:]: unrecognized selector sent to instance 0x6040de0
2011-07-11 14:42:27.461 TableViewTest[2776:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setTableViewStyle:]: unrecognized selector sent to instance 0x6040de0'
And here is my code of loading nib file
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"loadXibfile" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
return cell;
Upvotes: 6
Views: 5838
Reputation: 407
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];
LatestNewsCell *cell = (LatestNewsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"LatestNewsCell" owner:self options:nil] objectAtIndex:0];
}
}
Upvotes: 1
Reputation: 33050
This is what I did. Again some of the technique is quite awkward and I need improvement.
First I created a new subclass of UITableViewCell. The problem is I do not get an option to check "include" xib. It's as if xib is meant only for UIViewcontroller. I suppose you can create a subclass of UIViewController with XIB and then crate another subclass of UITableViewCell and move the template to your subclass of UIViewController.
Works.
Then I put these function:
@implementation BGCRBusinessForDisplay2
- (NSString *) reuseIdentifier {
return [[self class] reuseIdentifier];
};
+ (NSString *) reuseIdentifier {
return NSStringFromClass([self class]);
};
To initialize I do:
- (BGCRBusinessForDisplay2 *) initWithBiz: (Business *) biz
{
if (self.biz == nil) //First time set up
{
self = [super init]; //If use dequeueReusableCellWithIdentifier then I shouldn't change the address self points to right
NSString * className = NSStringFromClass([self class]);
PO (className);
[[NSBundle mainBundle] loadNibNamed:className owner:self options:nil];
[self addSubview:self.view]; //What is this for? self.view is of type BGCRBusinessForDisplay2. That view should be self, not one of it's subview Things don't work without it though
}
if (biz==nil)
{
return self; //Useful if we only want to know the height of the cell
}
self.biz = biz;
self.Title.text = biz.Title; //Let's set this one thing first
self.Address.text=biz.ShortenedAddress;
The [self addSubview:self.view];
is kind of awkward. It's what other says I should do and it won't work without it. Actually I want self.view to be self, rather than a subView of self. But hei.... Don't know how to do it otherwise.
...
Then I implement this for cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//[FetchClass singleton].FetchController
if([BGMDCRFetchClass singleton].FetchController.fetchedObjects.count!=0){
BGCRBusinessForDisplay2 *cell = (BGCRBusinessForDisplay2*)[tableView dequeueReusableCellWithIdentifier:[BGCRBusinessForDisplay2 reuseIdentifier]];
if (cell == nil)
{
cell =[BGCRBusinessForDisplay2 alloc];
}
else{
while (false);
}
Business * theBiz=[[BGMDCRFetchClass singleton].FetchController objectAtIndexPath:indexPath];
cell = [cell initWithBiz:theBiz];
return cell;
//return theBiz.CustomCell;
}else{
UITableViewCell * tvc=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tvc"];
return tvc;
}
}
Notice that I separate alloc from init. That's kind of awkward. That is why in my - (BGCRBusinessForDisplay2 *) initWithBiz: (Business *) biz
if a cell has been initialized before, I simply don't do the upper part of the init. I simply assign values of Business * to the various outlet in the BGCRBusinessForDisplay2.
I someone can improve my answers they are welcome. So far it works.
Upvotes: 0
Reputation: 3480
Here is the solution and it is useful for me and home for you or some else may be.
cell = [[[YourCustomcell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease];
NSArray *toplavelobject=[[NSBundle mainBundle]loadNibNamed:@"YourCustomcell" owner:self options:nil];
for(id c in toplavelobject)
{
if ([c isKindOfClass:[UITableViewCell class]])
{
cell=(YourCustomcell *) c;
break;
}
}
Upvotes: 2
Reputation: 69459
You should use UITableViewCell
and not UIView
in your XIB file.
Upvotes: 14
Reputation: 11834
Though not a direct answer to your issue, I recommend you make use of this UITableViewCellFactory class, it's very convenient:
http://blog.carbonfive.com/2009/07/16/loading-uitableviewcells-from-a-nib-file/
Upvotes: 1