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.
Related
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";
Hi !
I want to fill up a tableview with a simple array with simple custom cells.
It seems I'm doing something wrong, but I'm not experienced enough in objective-c yet to figure it out myself. I could use some help ;)
This is my controller implementation :
#interface tongilKupGradenTableViewController ()
#end
#implementation tongilKupGradenTableViewController {
NSMutableArray *arrayBanden;
NSMutableArray *arrayOmschrijvingen;
}
- (void)viewDidLoad
{
[super viewDidLoad];
arrayBanden = [[NSMutableArray alloc] initWithObjects:#"Witte band",#"Gele band",#"Groene band", #"Blauwe band", #"Rode band", #"Zwarte band",nil];
arrayOmschrijvingen = [[NSMutableArray alloc] initWithObjects:#"Weinig to geen voorkennis in Taekwondo.", #"Introductie in de basistechnieken.", #"Basistechnieken onder de knie.", #"Technieken uitbreiden en verbeteren.", #"Technieken meesteren en perfectioneren.", #"Meester in de kunst.", nil];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 6;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 78;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"tongilKupTableViewCell";
tongilKupTableViewCell *cell = (tongilKupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"tongilKupTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.lblBandTitle.text = [arrayBanden objectAtIndex:indexPath.row];
cell.lblBandOmschrijving.text = [arrayOmschrijvingen objectAtIndex:indexPath.row];
return cell;
}
The error I'm receiving is:
*** Terminating app due to uncaught exception 'NSRangeException',
reason: '*** [__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x1ef1012 0x13d3e7e 0x1ea6b44 0x757ce4 0x547ee3 0x3cdf13 0x36240c 0x3cda7b 0x3d2919 0x3d29cf 0x3bb1bb 0x3cbb4b 0x3682dd 0x13e76b0 0x7042fc0 0x703733c 0x7037150 0x6fb50bc 0x6fb6227 0x6fb68e2 0x1eb9afe 0x1eb9a3d 0x1e977c2 0x1e96f44 0x1e96e1b 0x21297e3 0x2129668 0x317ffc 0x21fd 0x2125 0x1)
libc++abi.dylib: terminate called throwing an exception
Maybe relevant, when debugging with breakpoints I see that the code passes the numberOfRowsInSection and numberOfSections twice, I'm wondering if this could be the cause?
Inside of viewDidLoad add the following code to load the nib you want to use for your table cells:
UINib *nib = [UINib nibWithNibName:#"tongilKupTableViewCell" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:nib forCellReuseIdentifier:#"tongilKupTableViewCell"];
Then inside of tableView:cellForRowAtIndexPath: all you need to do is:
tongilKupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"tongilKupTableViewCell"];
cell.lblBandTitle.text = [arrayBanden objectAtIndex:indexPath.row];
cell.lblBandOmschrijving.text = [arrayOmschrijvingen objectAtIndex:indexPath.row];
return cell;
Just be sure that you have your nib properly named and that you have set the reuse identifier properly on the cell inside of the nib.
What are you doing man here in this code?
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"tongilKupTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
You loaded an nib!!! And from the nib you tried to access it like an array.
Instead of loading a nib you need to create a new cell.
You should do something like this, style you can choose according to need.
if(cell==nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"eventCell"];
}
Edit 2:
Also if your nib contains some predefined cell, then you can directly use the string you created, why again hardcoding? :
static NSString *CellIdentifier = #"tongilKupTableViewCell";
....
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
Try setting your two arrays as properties in your interface :
#interface tongilKupGradenTableViewController ()
#property (nonatomic, strong) NSMutableArray *arrayBanden;
#property (nonatomic, strong) NSMutableArray *arrayOmschrijvingen;
#end
And then synthesize them :
#implementation tongilKupGradenTableViewController
#synthesize arrayBanden;
#synthesize arrayOmschrijvingen;
...
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"Cell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
CurrentCell *cell = (CurrentCell *)[self.tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil)
{
NSLog(#"cell == nil");
cell = [[CurrentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NSLog(#"the cell is %#", cell);
Current *current = [currents objectAtIndex:indexPath.section];
[cell setCellWithCurrent:current];
return cell;
}
I have added reuse identifier in storyboard, set delegate and datasource but still got this error:
*** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit/UIKit-2372/UITableView.m:5471
2013-11-04 23:47:34.206 MyApp[7417:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
*** First throw call stack:
(0x394452a3 0x3333497f 0x3944515d 0x342082af 0x32773f79 0x325c50a1 0x325c5055 0x325c255d 0x325a730b 0x325be7c7 0x3257a803 0x35e4ed63 0x35e4e901 0x35e4f835 0x35e4f21b 0x35e4f029 0x325808eb 0x3941a6cd 0x394189c1 0x39418d17 0x3938bebd 0x3938bd49 0x377592eb 0x325cb2f9 0xec869 0x36bf7b20)
libc++abi.dylib: terminate called throwing an exception
(lldb)
How i show my view with table:
else if ([segue.identifier isEqualToString:#"showCurrents"]) {
MainViewController *mainViewController = (MainViewController*)segue.destinationViewController;
}
CurrentCell.m:
#import "CurrentCell.h"
#interface CurrentCell ()
#end
#implementation CurrentCell
{
UIColor *badgeColor;
NSString *badgeText;
}
#synthesize topTitleLabel;
#synthesize descriptionLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void) setCellWithCurrent:(Current*) current
{
NSString *title = [current title];
[self.topTitleLabel setText:title];
[self.descriptionLabel setText:[current description]];
}
#end
PS: I do not get this error immediately, but after a quick scrolling list for some time.
Use dequeueReusableCellWithIdentifier:forIndexPath: instead of dequeueReusableCellWithIdentifier:
And remove that business about checking to see if the cell is nil.
Since your cell is a custom subclass of UITableViewCell, make sure that the cell's Class is set to you custom subclass in IB (see Identity Inspector).
Try loading cell with loadNibNamed method of NSBundle.
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"Your nib file name for CurrentCell" owner:nil options:nil] objectAtIndex:0];
}
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.
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"