I am trying to display a custom TableView on a UIViewController but am getting an error "UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:"
I had connected the TableView to datasource and delegate.
Any suggestion to go about implementing so or do I need a UITableViewController?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:#"CustomCell"
owner:nil options:nil];
for (id currentObjects in topLevelObjects){
if ([currentObjects isKindOfClass:[UITableView class]]){
cell = (CustomCell *) currentObjects;
break;
}
}
}
//---set the text to display for the cell---
cell.cellNameLabel.text = #"This is name";
cell.cellValueLabel.text = #"This is Value";
return cell;
ERROR:
//NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:#"CustomCell"
owner:nil options:nil];
//in above owner should be self
// if ([currentObjects isKindOfClass:[UITableView class]]){
change this line to
if ([currentObjects isKindOfClass:[CustomCell class]]){
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:#"CustomCell"
owner:self options:nil];//owner should be self
for (id currentObjects in topLevelObjects){
if ([currentObjects isKindOfClass:[CustomCell class]]){
cell = (CustomCell *) currentObjects;
break;
}
}
}
//---set the text to display for the cell---
cell.cellNameLabel.text = #"This is name";
cell.cellValueLabel.text = #"This is Value";
return cell;
}
Related
i have two tableviews in my view. i have issue in implementing the delegate methods. Can anyone help me on this. Here the problem is, this method requires a return value of type UITableViewCell right. what should i return?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==ContactTableview)
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *totalString = [ContactArray objectAtIndex:indexPath.row];
NSArray *afterSeparate = [[NSArray alloc]init];
afterSeparate = [totalString componentsSeparatedByString:#"+"];
NSString *cellText = [afterSeparate objectAtIndex:0];
//NSString *detailText = [afterSeprate objectAtIndex:1];
cell.textLabel.text = cellText;
//cell.detailTextLabel.text=detailText;
return cell;
}
if (tableView==ContactTableViewLabel)
{
static NSString *CellIdentifier = #"ContactListCustomCell";
ContactListCustomCell *cell = (ContactListCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ContactListCustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (ContactListCustomCell *) currentObject;
break;
}
}
}
cell.ContactLabel.text = [LabelArray objectAtIndex:indexPath.row];
cell.ContactValue.text = [ValueArray objectAtIndex:indexPath.row];
return cell;
}
}
Use this edited code. It will also get rid you of any warning regarding return cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
static NSString *CellIdentifier_contact = #"ContactListCustomCell";
UITableViewCell *cell;
if(tableView==ContactTableview)
{
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *totalString = [ContactArray objectAtIndex:indexPath.row];
NSArray *afterSeparate = [[NSArray alloc]init];
afterSeparate = [totalString componentsSeparatedByString:#"+"];
NSString *cellText = [afterSeparate objectAtIndex:0];
//NSString *detailText = [afterSeprate objectAtIndex:1];
cell.textLabel.text = cellText;
//cell.detailTextLabel.text=detailText;
}
if (tableView==ContactTableViewLabel)
{
ContactListCustomCell *cell1 = (ContactListCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell1 == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ContactListCustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell1 = (ContactListCustomCell *) currentObject;
break;
}
}
}
cell1.ContactLabel.text = [LabelArray objectAtIndex:indexPath.row];
cell1.ContactValue.text = [ValueArray objectAtIndex:indexPath.row];
cell = (UITableViewCell*) cell1;
}
return cell;
}
There is no need to cast ContactListCustomCell as a UITableViewCell. You can use ContactListCustomCell as a subclass of UITableViewCell. You can update your code like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if(tableView==ContactTableview)
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
.
.
}
else if(tableView==ContactTableViewLabel)
{
static NSString *CellIdentifier = #"CustomCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"yourCellNibName" owner:self options:nil];
cell = self.yourCellProperty;
self.yourCellProperty = nil;
}
.
.
}
return cell;
}
GoodLuck !!!
I am getting an error when I try to show custom cell on my tableView. I have a nibFile connected with cells but I always get thread sigbart here
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CellForOffers" owner:self options:nil];
I don't know why, and my cell for row at index implementation is
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellForOffers";
CellForOffers *cell =(CellForOffers *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CellForOffers" owner:self options:nil];
cell = [nib objectAtIndex:0];
appDC * application = [dataArray objectAtIndex:[indexPath row]];
cell.namelbl.text=application.o_name;
cell.descriptionlbl.text=application.o_description;
[cell.imageView setImageWithURL:[NSURL URLWithString:application.o_image_url]];
}
return cell;
}
Add nib with its class like bellow...
cell = (CellForOffers *)[nib objectAtIndex:0];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
cell.nameLabel.text = [arrareasdata objectAtIndex:indexPath.row];
cell.designLabel.text = [arrareasdata1 objectAtIndex:indexPath.row];
if(([arrareasdata2 objectAtIndex:indexPath.row].length > 0)
{
cell.emailLabel.text = [arrareasdata2 objectAtIndex:indexPath.row]
}
else
{
cell.emailLabel.text = #"";
}
cell.webLabel.text = [arrareasdata3 objectAtIndex:indexPath.row];
return cell;
}
the problem is here
if(([arrareasdata2 objectAtIndex:indexPath.row].length > 0)
what you must do is get object from the array and cast it into appropriate type. for example if the objects in array are NSString type then do following
NSString *value = (NSString*)[arrareasdata2 objectAtIndex:indexPath.row];
then in your if statement check for length
if (value.length > 0)
I'm looking for solution...
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"ItemCardapio";
NSString *nibName = #"ItemCardapioCell";
ItemCardapioCell *cell = (ItemCardapioCell *) [self.restaurantsList dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
cell = (ItemCardapioCell *)[nib objectAtIndex:0];
[cell initCellWithRestaurant:#"" tipoRestaurante:#""];
}
return cell;
}
I find that with problems with cell repeating, the problem tends to occur when you put stuff you shouldn't in the if (cell == nil) bit.
The reason being is that the table view will end up using an incorrect re-used cell if you don't update certain aspects of your cell appropriately.
Try taking anything which directly changes your cell out of that if (cell == nil) bit, and it should stop problems with repeating cells.
Heres what I use for Custom cells:
static NSString *cellIdentifier = #"ItemCardapioCell";
ItemCardapioCell *cell = (ItemCardapioCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSLog(#"Creating Cell");
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ItemCardapioCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[ItemCardapioCell class]])
{
cell = (ItemCardapioCell *)currentObject;
break;
}
}
};
It's like this, I created a UITableViewCell subclass called NewsItemCell, then I wanna use it in my FirstViewController.m, then I tried to import it, but the compiler keeps telling me this
Below is my code, it is driving me mad, thank you if you can help.
#import "NewsItemCell.h"
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize computers;
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"NewsItemCellIdentifier";
NewsItemcell *cell = (NewsItemcell *)[tableView
dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"NewsItemCell"
owner:self options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[NewsItemcell class]])
cell = (NewsItemcell *)oneObject;
}
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.computers objectAtIndex:row];
cell.newsTitle.text = [rowData objectForKey:#"Color"];
cell.newsDate.text = [rowData objectForKey:#"Name"];
return cell;
}
Jason
Should this code
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"NewsItemCell"
owner:self options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[NewsItemcell class]])
cell = (NewsItemcell *)oneObject;
}
not be
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"NewsItemCell"
owner:self options:nil];
cell = newsItemCellView
}
and NewsItemcell *cell be NewsItemcell *newsItemCellView, as cell may confuse the compiler.