Error with UITableViewCell - iphone

I am getting an error of:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull isEqualToString:]: unrecognized selector sent to instance 0x26e9d68'
I have a FriendViewCell class which has a UIImageView and two other labels as an IBOutlet. Setting the property and synthesize it (basic stuffs). I have connected the outlets to the corresponding label and imageview and change the class type to FriendViewCell, set the identity to that as well
The partial code I have is the following:
FriendViewCell *cell = (FriendViewCell *)[tableView dequeueReusableCellWithIdentifier:#"FriendViewCell"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:#"FriendViewCell"
owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (FriendViewCell *) currentObject;
break;
}
}
}

Rather than grabbing the array and searching for your cell, can you simply add an IBOutlet to your tableviewcontroller?
#property (nonatomic, assign) IBOutlet UITableViewCell *tvCell;
then in code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"FriendViewCell"];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"FriendViewCell" owner:self options:nil];
cell = tvCell;
}
You would need to link the IBOutlet to the cell in Interface Builder.

Have you tried to locate what is the instance 0x26e9d68 using the Allocations tracing?

Related

adding property to UITableView subclass

i'm creating UITableViewCell subclass which need to hold 3 types of images: facebook twitter and linkedin. the thing is that i need to know which service the cell support at the moment so i have try to add a property to the class called service which is NSString.
this is how i am setting up my cell to ad it to the table:
static NSString *sCellIdentifier = #"sCell";
SocialTableViewCell *sCell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];
sCell.service = #"service";
if (sCell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ExampleView" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[SocialTableViewCell class]])
{
sCell = (SocialTableViewCell *)currentObject;
break;
}
}
}
if (indexPath.row == 0) {
sCell.serviceImage.image = [UIImage imageNamed:#"icon_Twitter.png"];
sCell.service = #"twitter";
return sCell;
}
i am trying to set the property like this :
sCell.service = #"twitter";
and when i am trying to read it from the awakeFromNib method in the SocialTableViewCell class i get null. what should i do to manage this?
The awakeFromNib method is called before you have set the service property.
You could use the setter method of the service property :
-(void) setService:(NSString *)service {
// Set the value to the internal iVar
_service = service;
// Here you can do what you want with the service value like
self.titleLabel.text = service;
}

Determine different cells for iPhone and iPad in tableView:cellForRowAtIndexPath:

I just try to create an app for iPhone and iPad. For this i created a tableview that creates cells expecting the json that will be received when the app is loading. Inside the method
tableView:cellForRowAtIndexPath:
I initiate the cell that will be used for the iPhone and I set the specific values from the json. The cell I use is a custom Cell i created with a new object and NIB-File. The code looks like this:
static NSString *CellIdentifier = #"tvChannelIdentifier";
tvChannelCell *cell = (tvChannelCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"tvChannelCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
....
Now that I try to make the app work on the iPad too, I created a new custom cell, also with a new object and a NIB-File. The names of the variables are the same as in the customIPhoneCell so that I don't have to change the whole code. I just inserted a switch inside the tableView:cellForRowAtIndexPath: so that i show the right cell but it can't be accessed by the code:
static NSString *CellIdentifier = #"tvChannelCell";
static NSString *IPadCellIdentifier = #"tvChannelIPadCell";
//determination which device ---> choose cell
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
tvChannelCell *cell = (tvChannelCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}else{
tvChannelIPadCell = (tvChannelIPadCell *) [tableView dequeueReusableCellWithIdentifier:IPadCellIdentifier];
}
I tried to define the cell objects at the top of the class but then i can't use the same variable name. I also tried to choose the NIB file of the iPad to check if it'll be displayed but nothing happens. And I tried to set an UITableviewcell at the top,
UITableViewCell *cell
and set the specific cell type inside my switch in cellForRowAtIndexPath but then the variables can't be accessed.
So my question is, is it possible to do this switch inside the tableview method or do i have to code to several parts for each device where each celltype has its own variablename?
What I do:
I create only one UITableViewCell subclass with 2 xibs, one xib for iPad and another for iPhone.
tvChannelCell *cell = (tvChannelCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
nib = [[NSBundle mainBundle] loadNibNamed:#"tvChannelCell_iPhone" owner:self options:nil];
}else{
nib = [[NSBundle mainBundle] loadNibNamed:#"tvChannelCell_iPad" owner:self options:nil];
}
cell = [nib objectAtIndex:0];
}
this line of code useful for differenciating iphone and ipad,
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
tvChannelCell *cell = (tvChannelCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}else{
cell = (tvChannelIPadCell *) [tableView dequeueReusableCellWithIdentifier:IPadCellIdentifier];
}
It's quite simple, why don't you create another nib for iPad, and just access it when device is iPad.
You all code will remain same, i.e. you don't need to compare iPad like anywhere
Like:
if (cell == nil) {
NSArray *nib;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
nib = [[NSBundle mainBundle] loadNibNamed:#"tvChannelCell-iPad" owner:self options:nil];}
else{
nib = [[NSBundle mainBundle] loadNibNamed:#"tvChannelCell" owner:self options:nil];}
cell = [nib objectAtIndex:0];
}

-[__NSArrayM setTableViewStyle:]: error while creating a custom uitablevewcell

I've just created a custom cell in a tableview, I called the custom cell in UITableViewDelegate method
--(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
This is what my code looks like:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = #"cellIdetifier";
static NSString *cellId2 = #"cellId2";
tableCell *customCell = [tableView dequeueReusableCellWithIdentifier:cellId2];
if(customCell == nil){
NSArray *customObjects = [[NSBundle mainBundle]loadNibNamed:#"tableCell" owner:self options:nil];
for(id obj in customObjects){
if([obj isKindOfClass:[tableCell class]]){
customCell = (tableCell *)customObjects;
break;
}
}
}
return customCell;
}
I'm getting a error which says
creatingcustomcell[693:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM setTableViewStyle:]: unrecognized selector sent to instance 0x6d6c7f0'
Please help me, I have no clue what has just happened.
It's difficult to tell from this code if your coersion in line:
customCell = (tableCell *)customObjects;
is correct. At least it looks strange that you coerce an array:
NSArray *customObjects = [[NSBundle mainBundle]loadNibNamed:#"tableCell" owner:self options:nil];
to your tableCell
Do you mean:
customCell = (tableCell *)obj;
Also you might want to follow the recommendation that class names should be capitalized
if([obj isKindOfClass:[tableCell class]]){
customCell = (tableCell *)customObjects;
break;
}
You are setting the cell to the entire array
Do this instead
for (int i=0; i < customObjects.count; i++) {
if([customObjects objectAtIndex:i] isKindOfClass:[tableCell class]]){
customCell = (tableCell *)[customObjects objectAtIndex:i];
break;
}
}

Leak when I load custom UITableViewCells from Xib files?

I am getting leak in following code. Leak percentage # the end of the line. Can anyone tell me what is the problem.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
coustomMessage *cell = (coustomMessage *)[tableView dequeueReusableCellWithIdentifier:#"coustomMessage"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"coustomMessage" owner:self options:nil]; (93.1%)
cell = [topLevelObjects objectAtIndex:0];
}
cell.nameLable.text = [self.nameArray objectAtIndex:indexPath.row]; (3.4%)
cell.messageStatusLable.text = [[self.endPointCountArray objectAtIndex:indexPath.row] stringValue]; (3.4%)
return cell;}
are you sure you setted the "identifier" property in your XIB file with the same name you use in your code (with: dequeueReusableCellWithIdentifier:#"coustomMessage")?
Superb! I had exactly the same issue I misspelled the identifier in the XIB and so lots of leaks as the cell we being recreated not re-used. Thanks!

XIB backed View with Subclassing

I have a UITableViewCell defined in a XIB (with outlets setup) and the custom class set to 'CustomTableViewCell'. I'm now looking to subclass the 'CustomTableViewCell' with a 'SubclassCustomTableViewCell', however I'd like to use the same XIB (and not need to redefine my view. Is this possible? I currently have:
+ (SubclassCustomTableViewCell *)create
{
// Create undefined cell.
id cell = nil;
// Iterate over NIB and find a matching class.
NSBundle *bundle = [NSBundle mainBundle];
NSArray *cells = [bundle loadNibNamed:#"CustomTableViewCell" owner:nil options:nil];
for (cell in cells) if ([cell isKindOfClass:[self class]]) break;
// Return cell.
return cell;
}
But I can't figure out to to get it to return anything but the parent.
+ (SubclassCustomTableViewCell *)create
{
// Create undefined cell.
id cell = nil;
// Iterate over NIB and find a matching class.
NSBundle *bundle = [NSBundle mainBundle];
NSArray *cells = [bundle loadNibNamed:#"CustomTableViewCell" owner:nil options:nil];
for (cell in cells) if ([cell isKindOfClass:[self class]]) return cell; //change this
// Return cell.
return nil; //change this
}
could you tell me where + (SubclassCustomTableViewCell *)create is defined, in which class