Why Can't I import a UITableViewCell subclass ? That's weird - import

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.

Related

how to solve NSInternalInconsistencyException?

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];

How to solve Thread1 : Signal SIGABRT ? with this line [[NSBundle mainBundle] loadNibNamed:#"DVDListingView" owner:self options:nil]; [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you load custom UITableViewCells from Xib files?
Here is my code ..
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"LibraryListingCell";
DVDListingViewCell *cell = (DVDListingViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"DVDListingView" owner:self options:nil];
cell = [_cell autorelease];
_cell = nil;
}
cell.titleLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:#"title"];
cell.featureLengthLabel.text = [NSString stringWithFormat:#"%# minutes",
[[dao libraryItemAtIndex:indexPath.row] valueForKey:#"featureLength"]];
cell.coverImageView.image = [UIImage
imageNamed:[[dao libraryItemAtIndex:indexPath.row] valueForKey:#"coverImage"]];
return cell;
}
How to solve Thread1 : Signal SIGABRT problem with this line [[NSBundle mainBundle] loadNibNamed:#"DVDListingView" owner:self options:nil];
I would advise to try this, while making sure that you "DVDListingView.xib" is the NIB for the custom cell ONLY
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"LibraryListingCell";
DVDListingViewCell *cell = (DVDListingViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"DVDListingView" owner:self options:nil];
for(id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[DVDListingViewCell class]]){
cell = (DVDListingViewCell *)currentObject;
break;
}
}
}
cell.titleLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:#"title"];
cell.featureLengthLabel.text = [NSString stringWithFormat:#"%# minutes",
[[dao libraryItemAtIndex:indexPath.row] valueForKey:#"featureLength"]];
cell.coverImageView.image = [UIImage
imageNamed:[[dao libraryItemAtIndex:indexPath.row] valueForKey:#"coverImage"]];
return cell;
}
The line
[[NSBundle mainBundle] loadNibNamed:#"DVDListingView" owner:self options:nil];
returns an NSArray. It needs to be like this:
NSArray *xibObjectArray = [[NSBundle mainBundle] loadNibNamed:#"DVDListingView" owner:self options:nil];
//retrieve items from the array
if (xibObjectArray.count == 1) {
cell = [xibObjectArray objectAtIndex:0];
} else {
for (UIView *randomView in xibObjectArray) {
if ([randomView isKindOfClass:[DVDListingViewCell class]]) {
cell = (DVDListingViewCell *)randomView;
}
}
}

Custom UITableViewCells repeats row after scrolling

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;
}
}
};

Custom TableViewCells on a UIViewController?

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;
}

request for member error when loading in customtableviewcell and setting its IBOUTLETS

I've spent the last 3 hours scratching my head at this issue because all seems correct, except I can't get through to my tableviewcell class to set the uilabels in my custom cell.
here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
//cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
//cell.imageViewSection =
// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *textTitle = [[stories objectAtIndex: storyIndex] objectForKey: #"title"];
NSURL *imageurl = [NSURL URLWithString:[[stories objectAtIndex: storyIndex] objectForKey: #"description"]];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:imageurl]];
cell.imageViewSection = image;
cell.titleLabelText.text = textTitle;
return cell;
}
If someone can help me out it would be awesome :)
You should not rely on the cell always being returned as object at index 0 in the array. Do a simple loop to find the actual cell.
And add typecasts for the cell subclass you use
-(UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* cellID = #"cellID";
MyCell* cell = (id)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCell"
owner:self
options:nil];
for (cell in nib) {
if ([cell isKindOfClass:[MyCell class]]) {
break;
}
}
}
// Safely do tuff to cell
return cell;
}
This snippet assumes that the cell is at least available, the behavior if no table view cell is returned is undefined.
You're assigning and casting TableViewCell to UITableViewCell variable and UITableViewCell does not have those outlets. Instead, you should do the following..
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = (TableViewCell *)[nib objectAtIndex:0];
}