iPhone - "Class" may not respond to "method" - iphone

I am building a simple Navigation-based app using tables.
I have a custom "UITableViewCell" to customize the table cell data attached below.
#interface NewsCell : UITableViewCell
{
IBOutlet UILabel *newsTitle;
}
- (void)setNewsLabel:(NSString *)title;
#end
And then in my RootViewController, I set the text of the label "newsTitle" in "cellForRowAtIndexPath" method as follows.
static NSString *MyIdentifier = #"MyIdentifier";
MyIdentifier = #"NewsCell";
NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"NewsCell" owner:self options:nil];
cell = newsCell;
}
[cell setNewsLabel:#"hello testing"];
return cell;
When I run this, the app runs fine, but I get "NewsCell may not respond to '-setNewsLabel:'" warning.
Please help! Thank you.

In RootViewController.m, you need to `#import "NewsCell.h".
Or, stick #import "NewsCell.h" in your project's PCH (pre-compiled header) file.
The underlying issue is that the compiler only knows about methods that it has previously seen when parsing the header files (or ones in the PCH).

You're creating the nib, but not assigning it to anything. See this question to create it right.

Where does the variable newsCell come from? Is it really of type NewsCell?

Related

UITableView CustomCell crash on Button Click

This is very common question at SO though I have google and cross check my code few times but I am not able to figure out the crash
*** -[MyCustomCell performSelector:withObject:withObject:]: message sent to deallocated instance 0x96f6980
I have a CustomCell named MyCustomCell with XIB where I have 3 buttons for Facebook,Twitter,LinkedIn. I gave them all IBAction in CustomCell class.
When I click on any of them I get this kind of crash.
I am using ARC.
In my ViewController class I have cellForRowAtIndexPath
{
static NSString *CellIdentifier = #"MyCustomCell";
MyCustomCell *cell = (MyCustomCell*)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = (MyCustomCell *)[nib objectAtIndex:1];
}
return cell;
}
MyCustomCell.m
- (IBAction)facebookPressed:(BaseButton *)sender {
}
- (IBAction)twitterPressed:(BaseButton *)sender {
}
- (IBAction)linkedInPressed:(BaseButton *)sender {
}
I made this mistake just today! :) all you gotta do is replace the 2 lines
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"NameOfCustomCellNibFile" owner:self options:nil];
cell = [nib objectAtIndex:1];
You have to load the loadNibNamed: with the .xib file's name. I also thought it was with the identifier, but that is regarding the name that is referenced into the cell instead of the nib for the cell.
Hope this helps!
you can also code for Custom cell like this way. At nib of custom cell drag one UIButton and set it's tag in Xib. then use bellow Method:-
UIButton *btnMulSelected;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier =[NSString stringWithFormat:#"%d_%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle]loadNibNamed:#"cell_custome_iphones" owner:self options:nil];
cell = self.tblCell;
self.tblCell = nil;
btnMulSelected =(UIButton*)[cell.contentView viewWithTag:2];
[btnMulSelected addTarget:self action:#selector(MyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
Do not connect IBAction from Nib. just connect Custom-cell IBOutlate or put Unique UIButton tag from nib.
You've to keep the class name of cell and the XIB names same.
Keep the owner nil generally.
Also, if there is only 1 view ( i.e. 1 UITableViewCell in your case ), then this view is available at index 0 instead of index 1.
Inside the XIB file, make sure you link the button to your view itself, i.e. to IBOutlet or IBAction.
The linked buttons must be already added to your view, not floating outside in XIB. Otherwise they will instantiate and come at other indices of the loaded array. If you don't want a certain button for some time, then just keep it hidden.
So the code can be as below:
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"MyCustomCell" owner: nil options:nil];
MyCustomCell *cell = (MyCustomCell*)[views objectAtIndex:0];
Further make sure you do the following for creating XIB
Ok, then here goes the other portion simple process:
Create a MyCustomCell class derived from UITableViewCell class.
Create a separate .xib file with same name as MyCustomCell.xib.
Open .xib and delete the existing view in it. drag and drop a new
UITableViewCell from the objects of editor.
See the image to change the class name of the UITableViewCell in XIB to your class name.
Now the objectAtIndex 0 will return MyCustomCell.
Add all your buttons and other views in this UITableViewCell of your XIB.

Custom Table Cell Loading Data Objective C iOS -

Currently I have the following method, but it doesn't quite work...
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TVCell" owner:self options:nil];
cell = tvCell;
self.tvCell = nil;
}
UILabel *label;
label = (UILabel *)[cell viewWithTag:5];
label.text = [NSString stringWithFormat:#"Hole #%d", indexPath.row];
return cell;
}
The table view gets created with no errors, but each individual cell contains nothing, but clearly the TVCell.xib has a label with a tag of 5. The only question I have is this. I don't quite understand these steps apple gives here...
Select File’s Owner in the nib document window, open the Identity pane of the inspector, and set the class of File’s Owner to your custom view controller class.
Connect the cell outlet of File’s Owner (now the placeholder instance of your custom subclass) to the table-view cell object in the nib-file document.
Here is where those steps are...
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7
Can someone please explain those steps for a noob like me? I think that is what I messed up on, but I could have done anything wrong.
I don't think TcCell becomes a property of self. Try this instead when the cell queue is empty:
if (cell == nil) {
cell=[[NSBundle mainBundle] loadNibNamed:#"TVCell" owner:self options:nil];
}
It's kind of difficult to explain the steps that you said you don't understand in words. I would suggest looking for tutorials that have images or videos for that. (I did a quick search and there are lots available, but I didn't really want to choose one to direct you to without having read them more closely).
However, I prefer to create custom table view cells this way:
http://www.mobilesce.com/2011/12/27/the-best-way-to-do-custom-reusable-uitableviewcells/
It's slightly different from the way described in the apple docs, but I've seen a lot of people use it and I find it easier.
The NSBundle method loadNibNamed:owner:options: returns an NSArray containing the top level objects in your nib file. Try this:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:#"TVCell" owner:self options:nil];
cell = [nibArray objectAtIndex:0];
}

Custom UITableViewCell problem loading from NIB

UPDATE: I've solved this now - see my own comment. Amazing how often writing out the problem can lead to the resolution when you go through it again!
I must have read every thread on SO and the examples on Apple, but I just can't get this to work in my case and I wonder what I'm doing wrong?
I have made a custom cell in IB with XCode 4. The file's owner is set to the class of the custom view controller for the table and I have an IBOutlet for the UITableView cell which is hooked up OK from what I can tell. I normally do all my table cells via code and use the fast scrolling method, but for this project I really need to use nibs to load the cell content and I'm having some grief I can't figure out.
So in my interface file I have;
#interface myCustomTableViewController <various delegates> {
UITableViewCell *customNibCell;
...
}
#property (nonatomic,assign) IBOutlet UITableViewCell *customNibCell;
In the customTableViewController I do this in my cellForRowAtIndexPath;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#:"customDirCell"];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"DirectoryCellWithMenu" owner:self options: nil];
cell = customNibCell;
self.customNibCell = nil;
}
[self configureCell:cell atRow:indexPath.row];
And my configureCell method does this to reach into the cell and pull out the things I wish to customise.
UIImageView *fileImageView = (UIImageView *)[cell viewWithTag:3];
UILabel *headingTextLabel = (UILabel *)[cell viewWithTag:4];
UILabel *modifiedTextLabel = (UILabel *)[cell viewWithTag:5];
UILabel *sizeTextLabel = (UILabel *)[cell viewWithTag:6];
The Problem
The cells do load into the table OK in as much as I can see the correct layout. Furthermore, the custom actions I have defined on various buttons do all fire OK so it seems that the cell is hooked up properly.
BUT none of the customisation works - I can't seem to change the text of any of the labels for example and on investigation, the result of every [cell viewWithTag:nn] is nil which clearly signposts the problem, but I can't see what the cause is.
The identifier for the
UITableViewCell is set to
"customDirCell"
The object identities for the labels
etc are indeed 3,4,5,6 according to
IB
The xib contains exactly 1 object,
the UITableViewCell
I can't see anything wrong with either the nib file creation or the code, but I'm obviously missing something!
Can any kind soul shed light on where I should be looking?
Did you try this
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#:"customDirCell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"DirectoryCellWithMenu" owner:self options: nil];
cell =[nib objectAtIndex:0];
self.customNibCell = nil;
}
I've resolved this as per my comment.

Initializing and loading a custom uitableviewcell

I have Custom uitableviewcell: ScrollViewCell
I want to know what the difference is between the following code
static NSString *CellIdentifier = #"ScrollViewCell";
ScrollViewCell *cell = (ScrollViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//***** WHAT IS THE DIFFERENCE BETWEEN THIS CODE AND..
NSArray *xibObj = [[NSBundle mainBundle] loadNibNamed:#"ScrollViewCell" owner:nil options:nil];
for(id currentObj in xibObj){
if ([currentObj isKindOfClass:[ScrollViewCell class]]) {
cell = (ScrollViewCell *) currentObj;
}
}
//***** ..THIS CODE
cell = [[ScrollViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Which one should I use and why?
The first example will load a cell from a .xib file in your application's bundle. Each cell can handle it's own code, and behaves a lot like a UIViewController. This approach can get complicated when you try and load data from an array. You have to pass the object you're getting data out of to the cell, and have a very clear design before you start coding.
The other method allocates an empty instance of the UITableViewCell class as normal. This approach is typically used for programmatic configuration of the cells. You'll probably see this one in most places.
Good luck,
Aurum Aquila

Custom UITableViewCell erroring

I am trying to build a custom table view using a cell that I built in IB. I am getting a strange error:
<BroadcastViewController 0x4b4f5f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key postText.
Everything is wired up correctly in IB to the cell controller. Not really sure why this is happening.
This is what my cellForRowAtIndexPath looks like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the folder object of interest
Broadcast *messageAtIndex = [self.messages objectAtIndex:indexPath.row] ;
static NSString *CellIdentifier = #"BroadcastTableViewCell";
static NSString *CellNib = #"BroadcastTableViewCell";
BroadcastTableViewCell *cell = (BroadcastTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//ERRORING ON THIS LINE...
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (BroadcastTableViewCell *)[nib objectAtIndex:0];
}
cell.postText.text = messageAtIndex.replyText;
cell.authorName.text = messageAtIndex.postCreatorFirstName;
cell.postDate.text = messageAtIndex.creationDate;
return cell;
}
Anyone seen this kind of error before? Let me know if you need any more information...
What is really strange is that it complains that the class BroadcastViewController is not KVC compliant to postText.
As far as I can see, postText is a label in your cell, so the IBOutlet for this should be in the BroadcastTableViewCell class. So check out where you've linked the postText label in IB. Also, it can be that you had an IBOutlet in your view controller for this label, you've removed it but you forgot to delete the link in IB. Anyway, there somewhere is your problem. The fact that you have the error on that line is just because it's there you load your NIB, it doesn't have anything to do with the cell itself or with the owner.
Might has something to do with dequeueReusableCellWithIdentifier returning an UITableViewCell*.
I normaly do this:
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier...
CustomCell* acell = (CustomCell*)cell;
Set the owner to nil.
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:nil options:nil];
Ok figured it out. The connections in IB were indeed incorrect. I had them linked to the file's owner as opposed to the actual objects. I am going to give this too Stelian because he directed me to check out the nib. Thanks for all your help!