I added a new TableView in my application. I changed the cell-type to static and dragged some labels into the cells. Now i want to access the cells programmatically. For example: Cell 4 in Section 3 should open safari with google.com.
I created a new UITableViewController-class. Then i changed the number of sections to 3, and added a switch/case statement to the the numberofcellsinsection method.
If i run the app and open up the table view, the app crashes. Can someone help me, with setting up the TableView ?
Thanks!
EDIT
Here is the log
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'
*** First throw call stack:
I found a solution which is working.
i edited the cellForRowAtIndexPath method like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
return cell;
}
Now i can access my static cells with the if statements for example:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 2){
if (indexPath.row == 1){
NSLog(#"Tap");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://www.google.com"]];
}
}
}
Related
I am getting the following error when I press a button that opens a tableview:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier title - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Here is the code in the view controller for the tableview and the method that's causing the problem:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
I researched the error and I tried removing the forIndexPath:indexPath so the code looked like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
Now this caused a new error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
now I did some logging to find out that cell == nil was true so I added a check for that as suggested by some earlier questions:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc] init]
}
return cell;
}
Now this removed all errors but now when I open the tableview, the cells are empty when I want the ones that I created in the storyboard.
How do I fix this problem?
Here is what the View Controller looks like in the storyboard:
there are two cell recycle methods you can call on a UITable
View,
-(UITableViewCell *) dequeueReusableCellWithIdentifier:forIndexPath:
-(UITableViewCell *)dequeueReusableCellWithIdentifier:
its somewhat confusing, but they are quite different in how they are used. The one which takes a second argument (of type NSIndexPath) is dependant on you first having registered a class or xib file with the tableView, in order that the tableView can create a cell ad-hoc for you when there isn't one handy for recycling. This first method will always return a cell, so you can code your cellForRowAtIndexPath: much like you have.
the second method (which takes only one argument, the (NSString *)cellIdentifier can and will return nil when there is no cell handy for recycling. So when you use this one you should test the result for nil and create a cell in that case.
eg
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
//etc etc decorate your cell...
return cell;
}
In order to exploit your cell prototypes you will need to register a class or xib for each row/section, so that the table knows which cell to create. The recycling stuff only really works once enough cells have been created to fill the screen and you start scrolling. Good luck
If you are creating your prototype cells in a Storyboard you need to set the "Identifier" field for them to your "CellIdentifier" string. You can do this by selecting the cell and looking in the Attributes Inspector.
If you are creating a separate .xib file for your UITableViewCell you need to call this method on your UITableView in code somewhere:
registerNib:forCellReuseIdentifier:
If you are doing everything in code and just using a UITableViewCell subclass that knows how to layout itself then you need to call this method on your UITableView:
registerClass:forCellReuseIdentifier:
Here is a link to the reference docs
Getting this error when I load my tableview and I'm not sure why:
Assertion failure in -[UITableView
_configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:7344
I've added a UITableViewController in my storyboard and I'm using the prototype cells to design the cell. I linked the cell to my custom UITableViewCell subclass and I set its reuse identifier to "Cell". I am getting the assertion failure when I call dequeueReusableCellWithIdentifier in cellForRowAtIndexPath. From what I've read, this happens when I don't set the reuse identifier in the prototype cell, however I've checked countless times and it's the same identifier I'm using when I dequeue it.
Here are my datasource methods In the UITableViewController subclass:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
LanguageSelectionTableViewCell *cell = (LanguageSelectionTableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
return cell;
}
Try to use
[self.tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
instead of dequeueReusableCellWithIdentifier
You have to register the tableviewcell with the given reusablecellidentifier "Cell".
Example code is given below.
[yourTableView registerNib:[UINib nibWithNibName:#"YouTableViewCellNibName" bundle:nil] forCellReuseIdentifier:#"CellIdentifier"];
Where is the problem?
I want to make a Table View and if u click on the cell it pops up another Table View
What i must to do to fix that?
- (UITableViewCell )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier2 = #"TableViewCell_2";
TableViewController_2 *cell2 = (TableViewController_2)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
// Configure the cell...
int row = [indexPath row]; cell2.Spoj = _Spoj[row];
return cell2;
}
In your tableView:cellForRowAtIndexPath: use dequeueReusableCellWithIdentifier: instead of dequeueReusableCellWithIdentifier:forIndexPath:.
For correct dequeueing there are these steps:
On the attributes inspector for the xib file, add the identifier. (not in the restorationID field)
In your viewDidLoad of the ViewController owning the tableview, register the nib
[[self.tableview registerNib:[UINib nibWithName#"TheNibName" bundle:nil] withIdentifier:#"theSameIdentierInXib"];
call dequeue... in cellForRow (no need to cast cell type or check for nil from iOS 5+)
*note, registerClass can cause an assertion failure if used for cell xib.
I got exception on on dequeueReusableCellWithIdentifier:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"pointcell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];// Here is exception
if( cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
...
return cell;
}
Exception:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0xca51470> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key accessoryActionSegueTemplate.'
I did set identifier to Custom type cell in Storyboard. I have set Accessory Action segue in Storyboard. I think it's related to that particular type but I don't understand what should I do prevent crash.
It seems a bit too late, but I've just faced the same problem and found a solution.
When you Control+Dragging a segue from UITableViewCell in Interface Builder it offers you to create a segue for cell or accessory view. If you create it for accessory view and run your app under iOS 5 you are in trouble since accessory segues were not supported in that version.
You can drag a segue from ViewController instead and include this code (taken from here):
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"SegueID"
sender:[tableView cellForRowAtIndexPath:indexPath]];
}
So I have custom UITableViewCells (made with storyboard), and when I do not literally alloc, init the cells, the app crashes. However, since they are custom cells, I don't know how to alloc/init them.
This code crashes:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:#"TableViewCellsID"];
return cell;
}
with this printed to the console:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
I understand this means its crashing because there is no cell allocated yet, however when I try to allocate it, I have to specify a cell style.
So, when I build the cell as follows, it doesn't crash:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:#"TableViewCellsID"];
if (cell == nil)
cell = [[TableViewCells alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TableViewCellsID"];
return cell;
}
The problem is the code that works (the second set), requires me to set a UITableViewStyle. And as far as I know, there is no UITableViewStyleCustom.
Any helps would be appreciated.
It crashes in first case because there is no cell to reuse, and you are calling dequeueReusableCellWithIdentifier.
EDIT -
For custom cell initialisation you can check following threads -
How to make custom TableViewCell with initWithStyle after 3.0
initWithFrame vs initWithStyle