How to upload xib file for cell in tableview on iphone

I have seen that there are many resources available on this subject on this subject. I need to load another XIB file ( UIViewContoller ) for my cell. I developed my cell, and I want to load this project into my cell.

I wrote this code, but I get 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 nib file upload code

 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 all the XIB should contain). cell = [topLevelObjects objectAtIndex:0]; } return cell; 
+6
source share
5 answers

You should use a UITableViewCell , not a UIView in your XIB file.

+14
source

Here is a solution, and it is useful for me and at home for you or someone else.

  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; } } 
+2
source

Although this is not a direct answer to your question, I recommend you use this UITableViewCellFactory class, it is very convenient:

http://blog.carbonfive.com/2009/07/16/loading-uitableviewcells-from-a-nib-file/

+1
source
  - (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]; } } 
+1
source

This is what I did. Again, some of the methods are rather inconvenient, and I need an improvement.

First, I created a new subclass of UITableViewCell. The problem is that I have no way to check the "enable" xib. It is as if xib was intended only for the UIViewcontroller. I suppose you can subclass the UIViewController with the XIB, and then merge another subclass of the UITableViewCell and move the template into your subclass of the UIViewController.

Works.

Then I put these functions:

 @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 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 set this one thing first self.Address.text=biz.ShortenedAddress; 

[self addSubview:self.view]; embarrassing. This is what others say I have to do, and it wonโ€™t work without him. In fact, I want self.view to be itself, not a subView of itself. But hei .... I do not 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; } } 

Note that I am allocating alloc from init. This is awkward. This is why in my - (BGCRBusinessForDisplay2 *) initWithBiz: (Business *) biz , if the cell was initialized before, I just don't do the top of init. I simply assign Business * values โ€‹โ€‹to different outputs in BGCRBusinessForDisplay2.

I can improve my answers, they are welcome. While this is working.

0
source

Source: https://habr.com/ru/post/892472/


All Articles