Can anybody tell me why am I getting this error?
Here's my .m file:
#import "ListaParticipantesViewController.h"
#import "Participantes.h"
#import "ModParticipantesViewController.h"
#implementation ListaParticipantesViewController
#synthesize dao;
#synthesize participantes;
- (void)viewDidLoad
{
dao = [[ParticipantesDAO alloc] init];
participantes = [[NSMutableArray alloc] init];
participantes = [dao getDatos];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [participantes count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"celda";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [[participantes objectAtIndex:[indexPath row]] valueForKey:#"nombre"];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ModParticipantesViewController *destino = [self.storyboard instantiateViewControllerWithIdentifier:#"visualizacion"];
Participantes *tmp = [participantes objectAtIndex:[indexPath row]];
destino.participante = tmp;
[self.navigationController pushViewController:destino animated:YES];
}
#end
and the error displayed is:
2013-04-13 17:40:20.235 Registro[5314:c07] *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:5471
2013-04-13 17:40:20.237 Registro[5314:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
*** First throw call stack:
(0x209d012 0x11aae7e 0x209ce78 0xc40665 0x1a4c1b 0x13940c 0x1a4a7b 0x1a9919 0x1a99cf 0x1921bb 0x1a2b4b 0x13f2dd 0x11be6b0 0x2699fc0 0x268e33c 0x2699eaf 0x1de2bd 0x126b56 0x12566f 0x125589 0x1247e4 0x12461e 0x1253d9 0x1282d2 0x1d299c 0x11f574 0x11f76f 0x11f905 0x128917 0xec96c 0xed94b 0xfecb5 0xffbeb 0xf1698 0x1ff8df9 0x1ff8ad0 0x2012bf5 0x2012962 0x2043bb6 0x2042f44 0x2042e1b 0xed17a 0xeeffc 0x306d 0x1ef5)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Thanks in advance!
The storyboard or XIB does not have a cell defined with the cell identifier you specified.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cell; // at the end of your method
}
You need to set delegate and datasource of tableview in header.
Objecitve c
<UITableViewDataSource, UITableViewDelegate>
Swift
class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
}
Have you subclassed your ListaParticipantesViewController with the classes UITableViewDelegate, UITableViewDataSource? They are needed to be able to use delegate and data source methods.
In Swift:
class ListaParticipantesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
}
I got the very same error. It turns out it was the same problem, that it couldn't find my cell identifier, but for a different reason - I was subclassing UITableViewCell, but hadn't registered my .XIB file with my UITableView for a reuseIdentifier.
I solved it by resgitering in the viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
//LOAD XIB
UINib *nib = [UINib nibWithNibName:#"NAME_OF_NIB_FILE"
bundle:nil];
//REGISTER XIB, CONTAINING THE CELL (IDENTIFIER NAME USUALLY SAME AS NIB NAME)
[[self tableView] registerNib:nib
forCellReuseIdentifier:#"IDENTIFIER_NAME"];
}
And then creating the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//GET THE RECYCLED CELL
TableCellSubclass *cell = [tableView dequeueReusableCellWithIdentifier:#"IDENTIFIER_NAME"];
//CONFIGURE THE CELL
return cell;
}
Hope this helps someone!
In my case I had a simple mistake..
My number of rows was defined by array.count.
When I refreshed this array I was removing all the objects, initiating the connection, adding the objects to the array and then reloading the tableview.
I put the objects removal once the connection was completed and it solved my issue.
Related
I'm trying to create a simple View : a tableView which takes like 80% of the screen, and a collectionView with 4 rows at the bottom. Everything works fine with my tableView but I can't get things right with my collectionView. I always get the following exception :
Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'could not dequeue a view of kind: UICollectionElementKindCell with
identifier simpleCollectionCell - must register a nib or a class for the
identifier or connect a prototype cell in a storyboard'.
I've been looking in my View Controller : there are 2 refering outlets for my collectionVIew : datasource and delegate. So it's fine. And I'm using the method "withReuseIdentifier" so I don't have to declare an identifier in the nib. I've been trying to create a property for collectionView, but it changes nothing.
So I really don't see what I am missing ...
.h :
#import <UIKit/UIKit.h>
#interface MainViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
#end
.m :
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController {
NSArray *recipes;
NSArray *images; }
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self; }
- (void)viewDidLoad {
[super viewDidLoad];
recipes = [NSArray arrayWithObjects:#"Label1", #"Label2", #"Label3", #"Label4", #"Label5", #"Label6", #"Label7", #"Label8", #"Label9", #"Label10", #"Label11", #"Label12", #"Label13", #"Label14", #"Label15", nil];
images = [NSArray arrayWithObjects:#"img1", #"img2", #"img3", #"img4", nil];
// Do any additional setup after loading the view. }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated. }
#pragma mark TableView methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [recipes count]; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
return cell;
}
#pragma mark CollectionView methods
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection: (NSInteger)section {
return [images count];
}
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
return cell;
}
#end
Thanks in advance !
Make sure you have set the proper Custom Class(if any) as well as the Cell Identifier for the Collection View Cells in the Interface builder. Check this Screenshot.
This is for the Custom Class and the one below is for Cell Identifier.
The Identifier in your case is "cell". Make sure you have these set. They might be the problem here. Also the Class name of your CollectionViewCell and the one in your cellForItemAtIndexPath should be same.
Hope this helps.
You have To register your collection view cell
[self.dashBoardCollection registerNib:[UINib nibWithNibName:#"DashBoardCell" bundle:nil] forCellWithReuseIdentifier:#"DashBoardCell"];
may be in identity Inspector your are missing to name the Class
Oky u can try with this, just a simple way,
First create a new file in user interface select view and name as NibCell.
Then select the device (choose any one of them).
Delete what ever the content in the view.
Just drag and drop a new CollectionViewCell that's it.. :)
in the collectinview cell u can add image view (or any other objects u want) and set its tag to access.
for example
in viewDidLoad method
- (void)viewDidLoad
{
[super viewDidLoad];
//....other code
//nib register
UINib *cellNib = [UINib nibWithNibName:#"NibCell" bundle:nil];
[_collectionView registerNib:cellNib forCellWithReuseIdentifier:#"Cell"];
}
and in this method
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell"; //u know the identifier
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; //get the cell
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100]; //for example i put a label and set it's tag to 100 and i an accessing it
[titleLabel setText:cellData];
return cell;
}
I am trying to load a custom UIView over my ViewController , this UIView loads with a xib file and has a tableView , so when I connect dataSource and delegate with its file's owner and when I try to lunch the app , it crashes due to this message :
TABLE VIEW LOAD
-[DinoViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to
instance 0x993b070 *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason:
'-[DinoViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to
instance 0x993b070'
*** First throw call stack:
here is my code :
//DinoViewController
- (IBAction)searchDino:(id)sender {
DinoTable* tableview = [[DinoTable alloc]initWithFrame:CGRectMake(0,0,384,1024)];
NSArray * dinoTableView = [[NSBundle mainBundle] loadNibNamed:#"DinoTable" owner:self options:nil];
tableview = dinoTableView[0];
[self.view addSubview:tableview];
}
.TableView:
// DinoTable.h
#interface DinoTable : UIView <UITableViewDataSource , UITableViewDelegate> {
NSArray *list;
}
// DinoTable.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSString *filePath = [[NSBundle mainBundle]pathForResource:#"dinoName" ofType:#"plist"] ;
list = [NSArray arrayWithContentsOfFile:filePath];
NSLog(#"TABLE VIEW LOAD");
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [list objectAtIndex:indexPath.row];
return cell;
}
From the code, DinoTable is a UIView and has no UITableView. I'm guessing you're pointing the datasource and delegate to DinoTable based on the error. These should point to a UITableView.
Add a UITableView inside DinoTable and add methods that your class can set the delegate/datasource with.
I want to display a list of data on "UITableView".
When I run the code I get the following error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'unable to dequeue a cell with identifier Cell - must register a nib or a class for the
identifier or connect a prototype cell in a storyboard'
I get the error given above when I run the code below. The error is thrown at the
cellForRowAtIndexPath function. How can I solve this error?
#interface TheViewController ()
#property (strong, nonatomic) NSMutableArray *myTData;
#property (weak, nonatomic) IBOutlet UITableView *tableViewOutlet;
#end
#implementation TheViewController
- (void)viewDidLoad
{
self.tableViewOutlet.delegate = self;
self.tableViewOutlet.dataSource = self;
self.tableViewOutlet.scrollEnabled = YES;
self.myData = [[NSMutableArray alloc] initWithObjects:#"obj1", #"obj2", nil];
[self.tableViewOutlet reloadData];
[super viewDidLoad];
}
#pragma mark UITableViewDataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
return myData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"hede"];
}
[cell.textLabel setText: [myData objectAtIndex:indexPath.row]];
[cell.detailTextLabel setText:#"hede hodo"];
//
// Configure the cell..
return cell;
}
#pragma mark UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
#end
The exception is exactly what it says it is.
In iOS 5, you would call [tableView dequeueReusableCellWithIdentifier:CellIdentifier] and if it didn't return one, you would create one. The new call -dequeueReusableCellWithIdentifier:forIndexPath: should have a NIB or cell class registered and it will then always succeed.
You should call either
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
or
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
in your -viewDidLoad method.
In your storyboard, select the prototype cell, go to inspector and give the cell the identifier "Cell" or whatever you want as long as you have the same here : static NSString *CellIdentifier = #"Cell";
I'm trying to learn table views and I've hit a snag. I have the view delegate and datasource connected correctly in Storyboard, but I get the following runtime error when I get to the section of my app containing the table view.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationItem tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
Here's the chunk from my implementation file
#implementation CraftingViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Detail";
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
That error message is displaying because tableView:numberOfRowsInSection: is being sent to an object of type UINavigationItem while it seems like your CraftingViewController class is probably of type UITableViewController. I would make sure that you have connected your delegate to the correct class, because it doesn't seem like CraftingViewController is connected properly.
if the datasource is controller of the nib file.
please check alloc of this controller use "CraftingViewController" not "UIViewController"
Newbie question here ...
I've created a Master-Detail Application project in Xcode 4.2 using ARC and Storyboard. I've modified the template to be:
MasterViewController.h
#import <UIKit/UIKit.h>
#class DetailViewController;
#interface MasterViewController : UITableViewController
{
NSMutableArray *items;
}
#property (strong, nonatomic) DetailViewController *detailViewController;
#property (strong, nonatomic) NSMutableArray *items;
#end
MasterViewController.m (snipits)
....
#synthesize items;
....
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.detailViewController = (DetailViewController)[[self.splitViewController.viewControllers lastObject] topViewController];
items = [[NSMutableArray alloc] initWithObjects:#"item 1", #"item 2", nil];
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
....
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"Test Section";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
cell.textLabel.text = [items objectAtIndex:indexPath.row];
return cell;
}
This code will fail on this line when the program runs:
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
with this exception:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
If I cut the list(items) down to a single item, the MasterViewController will load without errors. I'm obviously doing something wrong, but I can't for the life of me figure out what it is. Anyone care to point out the obvious for me?
The template includes a UITableView that is set up for static cells. And it's actually one static cell. (this is why making your array one item long happens to kind of work)
But it seems you don't want static content here. So you just have to go into the storyboard, select the UITableView, go to the Attributes Inspector, and change the Content type to Dynamic Prototypes.
That should get you past this issue.
EDIT
A somewhat related issue is that you probably also want to use the prototype cell in the storyboard. To do that, just set the cell identifier of that prototype to the cell identifier you are using in your tableView:cellForRowAtIndexPath:.
And then omit the whole 'if (cell==nil)' part. The cell won't be nil.
So that method will look like this now:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell"; // <-- Make sure this matches what you have in the storyboard
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell.
cell.textLabel.text = [self.items objectAtIndex:indexPath.row];
return cell;
}
Hope that helps.