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
Related
I am new in iPhone development. I am using UItableview for showing list.
And able to show all items successfully.
But they changed when I scroll tableview.
Please help me.
Try this code in cellForRowAtIndexPath method of tableview
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Please give me more information to show your case clearly.
Have a look at this function first because all rows are generated from here!
(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;
}
Check whether each row is exist,unless just reuse it later.
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;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
UITableViewCell *cellName=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell1"];
NSDictionary *contentdict=[innerarray objectAtIndex:indexPath.row];
static NSString *cellidentify=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentify];
}
this my code every thing is working but the crash appears when i scroll the tableview
even i have given the array value correctly and given the delegate and datasource as self
is there any thing i need to write r change ?????
i am using Xcode 4.5
Try this...may be it help....
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"] autorelease];
}
NSDictionary *contentdict = [[NSDictionary alloc]init];
if([innerarray count]>0)
{
contentdict =[innerarray objectAtIndex:indexPath.row];
}
// Write your other cell stuff....
return cell;
}
I think it's crashing because you are not returning any cells from the cellForRowAtIndexPath method. Change cellForRowAtIndexPath like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"] autorelease];
}
//do something with the cell
return cell;
}
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
How to set different icon image for each cell in the UItable View.
In your tableview delegate:
- (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.imageView.image=[someDataClass methodThatReturnsAnImage];
cell.textLabel.text=[someDataClass methodThatReturnsAString];
return cell;
}