I have a problem with my tableView. I want have 6 different cell in the tableView.
So, in the viewDidLoad, i have this:
[_tableView registerNib:[UINib nibWithNibName:#"KBCategoriePriceTableViewCell" bundle:nil]
forCellReuseIdentifier:#"KBCategoriePriceTableViewCell"];
For other cells, it's the same code and, in the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
NSString* nameCell = [array objectAtIndex:indexPath.row];
if([nameCell isEqualToString:CELL_VIEW_CATEGORY_PRICE]){
KBCategoriePriceTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"KBCategoriePriceTableViewCell"];
[cell setBackgroundColor:[[receive category] colorBackground]];
[cell.buttonCategorie setTitle:#"" forState:UIControlStateNormal];
[cell.buttonCategorie setEnabled:NO];
[cell.buttonCategorie setImage:[UIImage imageNamed:[[receive category] imageName]]forState:UIControlStateNormal];
[cell.buttonDevise setTitle:[[receive devise] symbole] forState:UIControlStateNormal];
[cell.buttonCategorie setEnabled:NO];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
}
But when i launch my app, it's crash ! With this error message :
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/jean-nicolasdefosse/Library/Application Support/iPhone Simulator/7.1/Applications/20031DF6-D297-44D0-9D67-4AD3439D85F7/KillTheBill.app> (loaded)' with name 'KBCategoriePriceTableViewCell''
I don't understand why it doesn't work.
I remove reference and add the files in the project, i check the target and the custom cell KBCategoriePriceTableViewCell have as identifier in "xib": KBCategoriePriceTableViewCell.
Please help me !
Error message is saying that you do not have a file named KBCategoriePriceTableViewCell.nib in your project.
Also you should always return a cell in the tableView:cellForRowAtIndexPath:.
You need to load Custom cell like below code..
You need to check for nil condition also as initially we need not have any custom cell to load..
static NSString *cellidentifier=#"cell";
KBCategoriePriceTableViewCell *cell=(KBCategoriePriceTableViewCell *)[explortableview dequeueReusableCellWithIdentifier:cellidentifier];
if (cell==nil)
{
[[NSBundle mainBundle]loadNibNamed:#"KBCategoriePriceTableViewCell" owner:self options:nil];
cell=self.categoriesCell; //categoriesCell is reference of KBCategoriePriceTableViewCell in which you connected at xib
}
Hope it helps you..!
Don't load the Nib file in viewDidLoad.
You can use the code below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=#"KBCategoriePriceTableViewCell";
KBCategoriePriceTableViewCell *cell=(KBCategoriePriceTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if(cell==nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"KBCategoriePriceTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.lbl_name.text=#"Meeting";
return cell;
}
Here, the cell = [nib objectAtIndex:0]; is the first cell like that you can load the dynamic cells.
Careful not to invoke a prototype cell using initWithXib because that will crash
Related
At first I had this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DropDownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"DropDownTableViewCell"];
if (!cell) {
NSArray *topLevelObjects =
[[NSBundle mainBundle] loadNibNamed:#"DropDownView"
owner:self
options:nil];
for (DropDownTableViewCell *object in topLevelObjects) {
if ([object class] == [DropDownTableViewCell class]) {
cell = object;
break;
}
}
NSAssert(cell, #"Cell must not be nil");
}
cell.nameLabel.text = [self.dataSource buttonDownPicker:self stringForRowAtIndexPath:indexPath];
return cell;
}
At first moment when I show tableView and tableView cell's starts loading from nib I have UI freezing for a few seconds (caused by loading from nib for EVERY displayed cell). This can be solved by loading cell from nib earlier:
- (void)awakeFromNib {
DropDownTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:#"DropDownView" owner:self options:nil] objectAtIndex:0];
}
But this way looks "hacky". Is there more appropriate solution?
Edit according to given answers:
I've tried to use registerNib forCellIdentifier but it didn't LOAD nib, it just BINDING nib with identifier and at first time when tableView appears all cells causing load nib to memory
This is not "hacky" at all. For loading cells from a nib what you usually do is load the nib and then register the cell for reusable identifier of your choosing. For instance in one of my projects I have a view controller with a table view. In the view did load I call:
[[NSBundle mainBundle] loadNibNamed:#"ChannelListCell" owner:self options:nil];
[self.channelListTableView registerNib:[UINib nibWithNibName:#"ChannelListCell" bundle:nil] forCellReuseIdentifier:#"channelListCell"];
Then in the cell for row at index path:
cell = [tableView dequeueReusableCellWithIdentifier:#"channelListCell" forIndexPath:indexPath];
In your case the dequeued cell is always nil since you did not register it.
Try like this.....
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UINib *nib = [UINib nibWithNibName:#"" bundle:nil];
[self.tblview registerNib:nib forCellReuseIdentifier:#"DropDownTableViewCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
DropDownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"DropDownTableViewCell"];
return cell;
}
You can register your class first like bellow in your viewDidLoad. It make faster and better
[self.tableViewObject registerClass: [DropDownTableViewCell class]forCellReuseIdentifier:#"DropDownTableViewCellIdentifier"];
And please add following method in your DropDownTableViewCell.m file
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"DropDownTableViewCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
self = [topLevelObjects objectAtIndex:0];
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier=#"DropDownTableViewCellIdentifier";
DropDownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
//Access all cell properties here.
return cell.
}
Please refer https://stackoverflow.com/a/30954273/914111 for better knowledge.
I have a custom UITableViewCell which links to a UITableVIewCell xib. When I run the app, I get an error.
I did a lot of searching and I came up with this. When I try dragging from the cells view to the file owner, it seems like the view is not clickable, or drag-able.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CategorieUITableVIewCell"];
if (cell == nil) {
UIViewController *tempVC = [[UIViewController alloc] initWithNibName:#"CategorieUITableVIewCell" bundle:nil];
cell = (CategorieUITableVIewCell *)tempVC.view;
}
return cell;
}
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "CategorieUITableVIewCell" nib but the view outlet was not set.'
Not sure if this is clear enough, but if you have any questions, please ask.
This will work for SURE. You can try this in cellForRowAtIndexPath method.
static NSString *categoryTableIdentifier = #"CategoryUITableViewCell";
CategoryUITableViewCell *cell = (CategoryUITableViewCell *)[tableView dequeueReusableCellWithIdentifier: categoryTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CategoryUITableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.CustomedLabel.text=[YourArray objectAtIndex:indexPath.row];
return cell;
And IMPORTANT thing to note is in your Custom cell class you will have to connect the outlet to "Table View Cell" and not the "File's Owner" when you are working with Custom Cell
Please check the identifier matches with the one you specified in your xib. And then just replace your
if (cell == nil) {
UIViewController *tempVC = [[UIViewController alloc] initWithNibName:#"CategorieUITableVIewCell" bundle:nil];
cell = (CategorieUITableVIewCell *)tempVC.view;
}
code to
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"CategorieUITableVIewCell" owner:nil options:nil] objectAtIndex:0];
}
Check cell identifier and class name in xib.
i'm trying to add a custom cell to a tableView
i've first created a xib file with 2 label "name" & "description" i've linked it to a controller "customCellAchievementController"
it seems that the nib isn't found when i try to create the tableView
any idea where this can fail ?
i've redefine the class of my xib to the controller, i've change the identifier to the one i'm using in the code, i've check if the xib file is in the build phases / copy bundle resources and everything seems good i really don't understand ...
there is my code
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [achievement count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *customCellIdentifier = #"CustomCell";
CustomCellAchievementController *cell = (CustomCellAchievementController *)[tableView dequeueReusableCellWithIdentifier:customCellIdentifier];
if(cell==nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.name.text= [achievement objectAtIndex:indexPath.row];
cell.description.text = [description objectAtIndex:indexPath.row];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *customCellIdentifier = #"CustomCell";
CustomCellAchievementController *cell=[tableView dequeueReusableCellWithIdentifier: customCellIdentifier];
if (cell==nil)
{
NSArray *arr1=[[NSBundle mainBundle]loadNibNamed:#"CustomCellAchievementController" owner:nil options:nil ];
for(UIView *vie in arr1)
{
if([vie isKindOfClass:[UITableViewCell class]])
{
cell=(CustomCellAchievementController*)vie;
cell.name.text= [achievement objectAtIndex:indexPath.row];
cell.description.text = [description objectAtIndex:indexPath.row];
}
}
}
return cell;
}
Check your outlets. Outlests should associate with class not with class owner. For detail refer Loaded nib but the view outlet was not set - new to InterfaceBuilder.
I am getting EXC_BadAccess error message while i am scrolling tableview.
the following is the code i have done in cellForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier=#"customCellHistory";
customCellHistory *cell=(customCellHistory*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects=[[NSBundle mainBundle]loadNibNamed:#"customCellHistory" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]] ) {
cell=(customCellHistory*)currentObject;
break;
}
}
}
cell.lb11.text=[cellArray1 objectAtIndex:indexpath.row];
cell.lbl2.text=[cellArray2 objectAtIndex:indexpath.row];
return cell;
}
I can sense the problem is arising due to some mistake in the above code.
I used CustomCell in the above code to display a customized cell.
can anyone tell me what wrong have i done in this code
Hey try the following code, dont forget to set the cell identifier in you custom XIB to customCellHistory
At the top
#import "customeCellHistory.h"
then
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"customCellHistory";
customCellHistory *cell = (customCellHistory *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"customCellHistory" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.lb11.text=[cellArray1 objectAtIndex:indexpath.row];
cell.lbl2.text=[cellArray2 objectAtIndex:indexpath.row];
return cell;
}
The problem seems to arise from the strange loop you have. Use the last object method to set cell from the nib.
You are reusing a cell and then changing the cell in your loop which probably leads to a cell not existing where you are looking.
if (cell == nil) {
NSArray *topLevelObjects=[[NSBundle mainBundle]loadNibNamed:#"customCellHistory" owner:self options:nil];
You don't need that code. Just register the nib with the table view, earlier on:
[self.tableView registerNib:[UINib nibWithNibName:#"customCellHistory" bundle:nil]
forCellReuseIdentifier:#"customCellHistory"];
Now when you call dequeueReusableCellWithIdentifier, the nib will be loaded and the cell will be delivered, if there is no spare cell in the reuse pile. This ensures that you consistently have a cell and that it is the right kind of cell. Use the methods the framework gives you.
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;
You should use UITableViewCell and not UIView in your XIB file.
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;
}
}
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/
- (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];
}
}
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.