I'm new in iPhone development, I have one problem, my app contains navigation from button click to tableView (I Use storyboard). I created custum prototype cell, class for it and indentifier. But in my method when i debuged it comes to cell==nil and thow exeption:
'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"transportTableCell";
CNTransportCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(#"size of %d",transportArray.count);
cell.modelLabelView.text = #"sometext";
if(cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:
#"transportTableCell" owner: self ////options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}
code for navigate by click to table view:
- (IBAction)searchButtonClick:(id)sender {
CNJSONParser *parser = [[CNJSONParser alloc]init];
NSString *url = [parser getURL:#"43" :#"66"];
CNCarListWindowController *cars = [[CNCarListWindowController alloc]init];
cars.transportArray = [parser parseJSON:url];
[self.navigationController pushViewController:cars animated:TRUE];
}
just change your code as below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"transportTableCell";
CNTransportCell *cell;
if(cell == nil){
cell = (CNTransportCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
return cell;
first of all you are using storyboard and this method you have implemented is used when you use xib.
in storyboard you need to give identifier in "show the attribute inspector" of table view
Related
Im using a tableview with custom cell.
My custom cell has got label and I have defined its outlet at 'StatutoryMappingCell.h'
Following isn cellForRowAtIndexPath method,
StatutoryMappingCell * cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if (!cell)
{
[tableView registerNib:[UINib nibWithNibName:#"KnowledgeMainCell" bundle:nil] forCellReuseIdentifier:#"myCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(StatutoryMappingCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.statMapping.text = [self.items objectAtIndex:indexPath.row];
}
The problem is that the cells are not visible when I run my code. But the Why is that so?
This is my xib scene...
This is my storyboard scene
Look in your interface builder - you shuld have not conntected your custom cell, you should set it as a files owner , if yes check once the labels are connected or not,
try these code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"identifier";
CellItemList *cellA = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cellA == nil)
{
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:(isIpad)?#"CellItemList~ipad":#"CellItemList" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cellA = (CellItemList *)[topLevelObjects objectAtIndex:0];
}
cellA.statMapping.text = [tableData objectAtIndex:indexPath.row];
return cellA;
}
I have XIB that contain UITableView and in same XIB i created UITableViewCell, I did not set any class to cell, I just set identifier to the cell.
Now I want to load that cell to tableview delegate methd cellForRowAtIndexPath
i did is :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
return cell;
}
but it create new cell that doesn't include xib that i created.
So how do i get that cell from same xib that table view has ?
If you are creating a custom cell, then it will be better if you create custom classes of UITableViewCell type and then instantiate your UITableViewCell class in method.If you dont want to create custom class then below is the code that can help you:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = #"SimpleTableItem";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil)
{
cell = (UITableViewCell *)[[[NSBundle mainBundle] loadNibNamed:#"TMPartnerLoyalty" owner:self options:nil] objectAtIndex:0];
}
return cell;
}
Here, TMPartnerLoyalty is name of the xib.
please write this code in viewdidload , i hope it will work for you.
NSString *nibName = #"give your xib name here that contain Tablecell";
NSString *simpleTableIdentifier = #"SimpleTableItem";
UINib *customCellNib = [UINib nibWithNibName:nibName bundle:nil];
[tableView registerNib:customCellNib forCellReuseIdentifier:simpleTableIdentifier];
If you use prototype cell,Identifier name must same as dequeueReusableCellWithIdentifier name
NOTE:If you use prototype cell, you don't need to register the cell.Because Prototype cell is not a CustomCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(cell == nil)
{
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"";
return cell;
}
If you use UITableViewcell(Custom cell),follow the below steps
1.in viewDidLoad you must register the UITableViewCell or Custom Cell
UINib *cellNib = [UINib nibWithNibName:#"YourUITableViewCell" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:cellNib forCellReuseIdentifier:#"cell"];
2.OR in cellForRowAtIndexPath you can write the code below like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customTableIdentifier=#"cell";
YourUITableViewCell *cell=(YourUITableViewCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
if (cell==nil)
{
NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:#"YourUITableViewCell" owner:self options:nil];
cell=[nibs objectAtIndex:0];
}
cell.yourTableViewCellLabel.text = #"";
cell.yourTableViewCellTextField.text = #"";
cell.yourTableViewCellButton.text = #"";
return cell;
}
I have a tableview which shows data. But i want to customize the cells and i need a little help. So i have a nib for the tableview (HistoryViewController.xib). I just added the class and nib to customize the tableview cells (HistoryViewTableCell).
What are the steps i have to do next? I have this code already:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"TableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSDictionary * tempDictionary = [tableData objectAtIndex:indexPath.row];
cell.textLabel.text = [tempDictionary objectForKey:#"PickupAddress"];
return cell;
}
Try this link ..
Custom UITableViewCell Using Interface Builder
good demo with project try this
Customize Table View Cells for UITableView
hope it helps.
Please try below code -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HistoryViewTableCell *cell = [tableView dequeueReusableCellWithIdentifier:#"HistoryViewTableCell"];
if (cell == nil) {
// Create a temporary UIViewController to instantiate the custom cell.
UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:#"HistoryViewTableCell" bundle:nil];
// Grab a pointer to the custom cell.
cell = (HistoryViewTableCell *)temporaryController.view;
}
return cell;
}
OR
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HistoryViewTableCell *cell = [tableView dequeueReusableCellWithIdentifier:#"HistoryViewTableCell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"HistoryViewTableCell" 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;
}
Trying to set and access nib tableview cell. Basically the same as creating uitableview with a prototype tableviewcell and setting its identifier and accessing it through
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Fill table in with data
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
Question is how would you do this as a nib. As nib IB doesn't have prototype option?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
MPMediaItem *song = [self.songsList objectAtIndex:indexPath.row];
NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
NSString *durationLabel = [song valueForProperty: MPMediaItemPropertyGenre];
cell.textLabel.text = songTitle;
cell.detailTextLabel.text = durationLabel;
return cell;
}
-(void)viewDidLoad {
[self.tableView registerNib:[UINib nibWithNibName:#"MusicViewController" bundle:nil] forCellReuseIdentifier:#"Cell"];
}
Create a custom tableviewCell with the nib file.After creation you have three files YourCustomCell.h YourCustomCell.m and YourCustomCell.xib
Add the IBOutlet in YourCustomCell.h and connect them in YourCustomCell.xib with respective labels.
Now in cellForRowAtIndexPath: add the following:
YourCustomCell *yourCustomCell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
yourCustomCell.labelInCustomCell.textLabel.text = songTitle;
yourCustomCell.detailsLabelInCustomCell.textLabel.text = durationLabel;
return yourCustomCell;
Hope this help you.
You make your cell in the xib file and then in code (probably in viewDidLoad) register the nib with registerNib:forCellReuseIdentifier:. In cellForRowAtIndexPath, you just dequeue a cell with the same identifier that you passed to the register method. No need to put in the if (cell == nil) clause, because it will never be nil.
When I run my ios app, only the first cell displays in the TableView, then when clicked the second cell displays. I would like them both to display at the same time, but I cannot figure out this behavior.
Here is the code for my view did load
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ProductCellId";
ProductTableCell *cell =
(ProductTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"ProductTableCell" owner:self options:nil];
cell = self.productCell;
}
Product *product = [products objectAtIndex:indexPath.row];
[cell configureForProduct:product];
return cell;
}
Any help would be much appreciated!
Since you are loading your cell from Interface builder try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ProductTableCell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ProductTableCell" 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];
}
Product *product = [products objectAtIndex:indexPath.row];
[cell configureForProduct:product];
return cell;
}
Change this:
static NSString *CellIdentifier = #"ProductCellId";
to:
static NSString *CellIdentifier = #"ProductTableCell";
You can find additional information for what you are trying to do in this question: Load cells from XIB files