loading UITableView in tab based iPhone application? - iphone

i have a problem while loading a UITableView on UITabBarController, i got this error while loading table using tab
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
my code is here
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
return cell;
}
i tried many, it may be simple.. anyone please help me?

//Editing in your code, it should work.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2;
}
- (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];
}
return cell;
}

The problems is that you are using dequeueReusableCellWithIdentifier but you have no code to handle if this returns nil (i.e. when no cells have yet been created to be re-used).
For example:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

Related

Programmatically adding a UITableView - how do I set the reuse identifier for the cells?

I have a UIViewController that at some point grows a UITableView, and when it does I simply init the TableView instance variable and add it to the view, but I'm not sure how to handle the dequeueing of cells to add to the view; I need a reuse identifier, but I'm not sure how to set it.
What do I do within this method?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"wot";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
}
Use the method initWithStyle:reuseIdentifier
check if cell exists
If it doesn't, then you need to initialize it.
code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath*)indexPath
{
static NSString *cellIdentifier = #"wot";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle: someStyle reuseIdentifier: cellIdentifier];
return cell;
}
The reuse identifier need not be explicitly defined.In the cellForRowAtIndexPath method the definitions you included in question is enough to work with
for Reference
Eg
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]];
}
Region *region = [regions objectAtIndex:indexPath.section];
TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row];
cell.textLabel.text = timeZoneWrapper.localeName;
return cell;
}

How to add Section and TableView Cell dynamically

I have a button that when pressed, a tableview section will be added and will also add a new row to that section. How can I implement this programatically?
I have an array of cells.
Here are my codes
- (IBAction)addCell:(id)sender {
UITableViewCell *newCell = [[UITableViewCell alloc] init];
counter++;
[cells addObject:newCell];
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [cells count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//this is where i want to adjust the row per section
//this code hide the recent sections, only the current created section appears
return [cells objectAtIndex:indexPath.row];
}
Hi try reading this:
How to display the UITableView programmatically?
Adding cells programmatically to UITableView
Hope this could help you.
First, update your's datamodel(NSArray or mutable). Second, when you want tableview refresh, add a code [self.tableView reloadData];
oops, your code some strange uitaleviewDataSource, Delegate Method.
also, your have a some mistake.
why you implements numberOfRowsInSection return 1? very strange.
I apprecated below code.
CASE 1. No has DataModel.
- (IBAction)addCell:(id)sender
{
cellRowsCount++;
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return cellRowsCount;
}
- (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];
}
return cell;
}
your codes, dataModel(NSArray or mutable) does not necessarily need, so I simply rowCount variable added and your rowsCount of tableviewCell has synchronized.
If you wanna with DataModel, below CASE2. refer plz.
CASE2. has DataModel.
- (IBAction)addCell:(id)sender
{
textCount ++;
NSString *cellText = [NSString stringWithFormat:"blah %d", textCount];
[myArray addObject:cellText];
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [myArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[myArray objectAtIndex:section] 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 = (NSString *)[myArray objectAtIndex:indexPath.row];
return cell;
}
You only have to add the new section and the new item to the array that holds the section and the items like so:
- (void)viewDidLoad
{
[super viewDidLoad];
animals = #{#"Section 1" : #[#"Item 1", #"Item 2", #"Item 3"],
#"Section 2" : #[#"Item 1", #"Item 2"]};
}
after that you just reload the tableView wherever your logic needs:
[self.tableView reloadData];

Loading UITableViewController from code

I am having issues with what I can only imagine is a very simple problem. I am loading a UITableViewController class called LocationViewController from one UIViewController:
LocationViewController *lvc = [[LocationViewController alloc] init];
[self.navigationController pushViewController:lvc animated:true];
In this class I have the 3 following method implemented:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CityCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = #"Test";
return cell;
}
And I am getting the following error:
UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
I have had this error before when transitioning between ViewControllers using the StoryBoard this was because the CellIdentifier was not correct. I have no idea what I am doing wrong. I have tried loading a nib file with this ViewController but that throws the same error.
You have to allocate the cell. use this code.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
use this code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = #"Test";
return cell;}
dequeueReusableCellWithIdentifier returns nil, if there is no object in the reusable-cell queue. check if cell is nil after calling dequeueReusableCellWithIdentifier, create a new UITableViewCell in that case.
dequeueReusableCellWithIdentifier is a cache of already created UITableViewCells with your identifier "CityCell"
If you use the code below, it tries to get a cell from the cache, and if it can't it will create one and then store it in the cell cache for later use.
You need the cache for large tables, as it drastically improves table performance when scrolling.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"CityCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
cell.textLabel.text = #"Test";
return cell;
}
Take a look at the table view programming guide to learn more about this:
Table Programming Guide

Starting iphone. UITableView Hello world with edit button fails

im learning Obj-C and im treating to make my first UITableView with "Hello world" text.
My problem is i can't show on screen right edit button. this is my code:
- (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] autorelease];
}
[[cell textLabel] setTextAlignment:(UITextAlignmentCenter)];
cell.textLabel.text=#"Hello World";
//[cell.textLabel setText:#"Hola2"];
return cell;
}
and:
- (void)viewDidUnload
{
[super viewDidUnload];
self.navigationItem.rightBarButtonItem=self.editButtonItem;
}
Am I missing something?
Thanks for help.
self.navigationItem.rightBarButtonItem=self.editButtonItem;
This must go in the viewDidLoad
are you also setting the rows and sections correct?
- (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 1;
}

How to insert data from second row in tableview in iphone

i need your help.I need to insert data dynamically but insertion should be from the second row in the tableview in iphone.How can i do this.
You may want to fetch the data from the array w r t (indexPath.row -1), in case you are populating the table with an array
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0)
{
//return blank cell;
}
//add cell contents
return cell;
}
- (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] autorelease];
}
// Configure the cell.
NSIndexPath *path1 = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
[self configureCell:cell atIndexPath:path1];
return cell;
}
I do not tested it but it should work, try it out