[UITableViewCellContentView setText:]: unrecognized selector sent to instance - ios

I have created a custom UITableViewCell with a XIB and connected the labels on it as outlets to the property. When I use that cell in my UITableViewController class to set the text on it, the app crashes during runtime with "[UITableViewCellContentView setText:]: unrecognized selector sent to instance" error. Below is my code:
Custom Table View cell .h and .m files:
#import <UIKit/UIKit.h>
#interface CustomTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *branchName;
#property (weak, nonatomic) IBOutlet UILabel *address;
#end
#implementation CustomTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
My UITableViewController code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *branchListCellId = #"branchList";
CustomTableViewCell *branchListCell = [tableView dequeueReusableCellWithIdentifier:branchListCellId];
if (!branchListCell) {
[tableView registerNib:[UINib nibWithNibName:#"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:branchListCellId];
branchListCell = [tableView dequeueReusableCellWithIdentifier:branchListCellId];
branchListCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
branchListCell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
branchListCell.textLabel.numberOfLines = 0;
branchListCell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
}
return branchListCell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(CustomTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//UILabel *branchName = (UILabel *)[cell.branchName viewWithTag:1];
cell.branchName.text = [self.branchList objectAtIndex:[indexPath row]];
}
Looking at the solution to other questions for the same crash here at SO, I even tries setting viewWithTag to something other than 0. I even tried recreating the outlets. Nothing helped.
What am I missing here?

Related

iOS - UILabel loading, but no text displaying

I have implemented my own UITableViewCell and created a .xib for it. I have connected all the labels using a strong IBOutlet and initialized the UILabels in the awakeFromNib() method. However, whenever I run the iOS Simulator, I run into an issue where the UILabel is (null) in NSLog.
I am wondering if it has to do with how I am loading in the text for the UILabel. I have tried to create a shortened version of the project below that outlines the issue I'm running into.
I would also like to note that I can click on the actual rows, but that there is still no text displaying.
My code:
ToDoCell.h
#import <UIKit/UIKit.h>
#interface ToDoCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *minutesLeft;
#property (strong, nonatomic) IBOutlet UILabel *hoursLeft;
#property (strong, nonatomic) IBOutlet UILabel *daysLeft;
#property (strong, nonatomic) IBOutlet UILabel *taskLabel;
#end
ToDoCell.m
#import "ToDoCell.h"
#implementation ToDoCell
#end
ToDoViewController.m
#import "ToDoViewController.h"
#import "ToDoCell.h"
#interface ToDoViewController ()
#property NSMutableArray *toDoItems;
#end
#implementation ToDoViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
}
- (void)loadInitialData {
NSString *item1 = #"Testing";
[self.toDoItems addObject:item1];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ToDoCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ToDoCell" forIndexPath:indexPath];
cell.taskLabel.text = #"Testing";
NSLog(#"Fudge Monkeys: %#", cell.taskLabel.text);
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
#end
Issue is you are creating new objects for labels but then you are not adding them to superview but calling [self addSubview:label] ssigning them to properties does not add them to super view.
But this is totally unnecessary you should not init them in awakeNib. , and one thing more I will say about your code now you don't need to use #synthesis anymore as well. Any particular reason you are making IBOutlets strong?
Update you need to register nib with tableView first add following lines in viewDidLoad
[tableView registerNib:[UINib nibWithNibName:#"ToDoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"ToDoCell"];
You can read more about this here
If the UILabel instances are connected in Interface Builder via IBOutlet, you must not initialize them explicitly.
Just delete the complete awakeFromNib() method
Add this in viewDidLoad
NSString *strName = NSStringFromClass([ToDoCell class]);
[self.toDoItems registerNib:[UINib nibWithNibName:strName bundle:nil] forCellReuseIdentifier:strName];
You are using xib file but not loading it by registering it for the tableView. This is required when you use separate xib file. If you do not register, then only class will load and xib won't. And your awakeFromNib won't be called.
First of all delete awakeFromNib and make sure that you gave Cell Identifier in cell nib file.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"ToDoCell";
ToDoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell)
cell = [[ToDoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifier];
cell.taskLabel.text = #"Testing";
return cell;
}
Don't forget to add following code for register nib.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.tableView registerNib:[UINib nibWithNibName:#"ToDoCellTableViewCell" bundle:nil] forCellReuseIdentifier:#"ToDoCell"];
}
Here is the sample code for your problem.
May this help to solve your problem.
You .xib why cell should be set through:
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
ToDoCell *cell = [tableView dequeueReusableCellWithIdentifier:[ToDoCell reuseIdentifier]];
if (!cell) {
cell = (ToDoCell *)
[[NSBundle mainBundle] loadNibNamed:#"ToDoCell" owner:self options:nil].firstObject;
}
cell.taskLabel.text = #"Testing";
NSLog(#"Fudge Monkeys: %#", cell.taskLabel.text);
return cell;
}

Can't set Custom UITableViewCell property - iOS

First of all I want to apologize for my bad english.
I'm having trouble to set the properties of my custom UITableViewCell (HistoricoCell).
When I try to set a property of my cell I get: Signal SIGABRT error:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Dequeue the cell.
HistoricoCell *cell = (HistoricoCell *)[self.tblHistorico dequeueReusableCellWithIdentifier:#"CellIdentifier" forIndexPath:indexPath];
// Fetch Item
NSDictionary *item = [self.dbManager.arrColumnNames objectAtIndex:indexPath.row];
// Configure Table View Cell
[cell.lblCodigo setText:[NSString stringWithFormat:#"%#", item[#"codigo"]]];
[cell.btnFavoritar addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
I followed a lot of tutorials and questions on the web but I stil with my error.
Can someone help me?
My code:
HistoricoCell.h
#import <UIKit/UIKit.h>
#interface HistoricoCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *lblCodigo;
#property (weak, nonatomic) IBOutlet UIButton *btnFavoritar;
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tblHistorico;
SecondViewController.m
#import "SecondViewController.h"
#import "DBManager.h"
#import "HistoricoCell.h"
#interface SecondViewController ()
#property (nonatomic, strong) DBManager *dbManager;
#property (nonatomic, strong) NSArray *arrPeopleInfo;
-(void)loadData;
#end
#implementation SecondViewController
static NSString *CellIdentifier = #"CellIdentifier";
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Make self the delegate and datasource of the table view.
self.tblHistorico.delegate = self;
self.tblHistorico.dataSource = self;
// Initialize the dbManager property.
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:#"bernoullidb.sql"];
[self.tblHistorico registerClass:[HistoricoCell class] forCellReuseIdentifier:#"CellIdentifier"];
[self loadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)loadData{
// Form the query.
NSString *query = #"select * from tbHistorico";
// Get the results.
if (self.arrPeopleInfo != nil) {
self.arrPeopleInfo = nil;
}
self.arrPeopleInfo = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
// Reload the table view.
//[self.tblHistorico reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arrPeopleInfo.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60.0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Dequeue the cell.
HistoricoCell *cell = (HistoricoCell *)[self.tblHistorico dequeueReusableCellWithIdentifier:#"CellIdentifier" forIndexPath:indexPath];
// Fetch Item
NSDictionary *item = [self.dbManager.arrColumnNames objectAtIndex:indexPath.row];
// Configure Table View Cell
[cell.lblCodigo setText:[NSString stringWithFormat:#"%#", item[#"codigo"]]];
[cell.btnFavoritar addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)didTapButton:(id)sender {
NSLog(#"%s", __PRETTY_FUNCTION__);
}
#end
You should set cell indentifier "CellIdentifier" for your cell in File Inspector
Or register your nib file if you add cell with nib:
UINib *itemNib = [UINib nibWithNibName:#"yourCell" bundle:nil];
[self.tableView registerNib:itemNib forCellReuseIdentifier:#"yourCellReuseIndentifier"];
I think your problem is in your cell creation: you try to dequeue a cell if it exists (i.e. recycle a previously used cell). that is OK, but, especially when the TableView is displayed for the first time, no previously used cell for this table exists. So, you have to create one if the dequeueReusableCellWithIdentifier call return nil.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Dequeue the cell.
HistoricoCell *cell = (HistoricoCell *)[self.tblHistorico dequeueReusableCellWithIdentifier:#"HistoricoCellIdentifier" forIndexPath:indexPath];
if( cell == nil ) // no queuded cell to dequeue
{
// you have to create a fresh new one
cell = [HistoricoCell alloc] initWithStyle:<your cell style> reuseIdentifier:#"HistoricoCellIdentifier"];
}
// Fetch Item
NSDictionary *item = [self.dbManager.arrColumnNames objectAtIndex:indexPath.row];
// Configure Table View Cell
[cell.lblCodigo setText:[NSString stringWithFormat:#"%#", item[#"codigo"]]];
[cell.btnFavoritar addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}

TitleLabel is broken when you put a UIButton in UITableViewCell

I have UITableView with custom UITableViewCell. Custom cell contains UIButton and UILabel.
Here I observe that UILabel text is change as I expected but UIButton text is not changing.
When I scroll out the button outside of the screen, change the label of UIButton.
Why it is not working? I have use Xcode 6 and use below code.
ViewController.h
#import <UIKit/UIKit.h>
#import "customTableViewCell.h"
#interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerNib:[UINib nibWithNibName:#"customTableViewCell" bundle:nil] forCellReuseIdentifier:#"customTableViewCell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
customTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"customTableViewCell" forIndexPath:indexPath];
cell.button.titleLabel.text = #"Label does not change immediately";
cell.label.text = #"change label";
return cell;
}
customeTableViewCell.h
#import <UIKit/UIKit.h>
#interface customTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIButton *button;
#property (weak, nonatomic) IBOutlet UILabel *label;
#end
This is because the UIButton class has multiple states (Normal, Selected, Highlighted, Disabled). When you change the text property of it's internal UILabel (textLabel property of UIButton), it's property is overriden by setState function, which is called when table is loaded.
To change text in the label of a button you need to call setTitle:forState: method. Here is your code fixed to work:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
customTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"customTableViewCell" forIndexPath:indexPath];
[cell.button setTitle:#"Now it works" forState:UIControlStateNormal];
cell.label.text = #"change label";
return cell;
}
For the sake of completion, you can also use setAttributedTitle:forState: method with NSAttributedString, so you can actually set your own specifically formatted string as a title.
Use this
[cell.button setTitle:#"MyNewTitle" forState:UIControlStateNormal];

custom uitablecellcant connect tablecell to files owner

I may be barking up the wrong tree but I'm trying to duplicate functionality I wrote for ios5 in a project thats been updated to ios7.
I've copied and pasted the code across, I get this error
*** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m:5261
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier TableView - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(0x30829f4b 0x3ac6a6af 0x30829e25 0x311d1fe3 0x3310891f 0x11bebf 0x330cea5b 0x33076e7d 0x33076699 0x32f9cda3 0x32c23c6b 0x32c1f47b 0x32c1f30d 0x32c1ed1f 0x32c1eb2f 0x32c1885d 0x307f51cd 0x307f2b71 0x307f2eb3 0x3075dc27 0x3075da0b 0x35484283 0x33001049 0x81597 0x7b558)
Everything is the same as another controller where it works except when I examine the xib
Working one:
Crashing one:
The difference being there no Referencing outlet tableCell -> File's Owner
HOW? WHY?
Here's the code just in case
Viewcontroller.h
#import <UIKit/UIKit.h>
#import "blogCellVC.h"
#interface BlogsListingiPhoneVC : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
NSArray *blogsListing;
UITableView *tableView;
IBOutlet blogCellVC *tableCell;
}
Viewcontroller.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIndentifier = #"MyCell";
blogCellVC *cell = (blogCellVC *)[self.tableView dequeueReusableCellWithIdentifier:MyIndentifier forIndexPath:indexPath];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"blogCellVC" owner:self options:nil];
cell = tableCell;
}
blogsListingModel *info = [_blogsListing objectAtIndex:indexPath.row];
if (info.blogActive == 0)
{
[cell cellHead:info.blogName];
[cell cellHeadCol:[UIColor lightGrayColor]];
[cell cellIcon:#"blank.png"];
}else {
[cell cellHead:info.blogName];
[cell cellHeadCol:[UIColor blackColor]];
[cell cellIcon:#"tick.png"];
}
return cell;
}
Customcell.h
#import <UIKit/UIKit.h>
#interface blogCellVC : UITableViewCell {
IBOutlet UILabel *cellHead;
IBOutlet UIImageView *cellIcon;
}
- (void)cellHead:(NSString *)_text;
- (void)cellIcon:(NSString *)_text;
- (void)cellHeadCol:(UIColor *)aCol;
#end
Customcell.m
#import "blogCellVC.h"
#interface blogCellVC ()
#end
#implementation blogCellVC
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
}
return self;
}
- (void)cellHead:(NSString *)_text {
cellHead.text = _text;
}
- (void)cellHeadCol:(UIColor *)aCol {
cellHead.textColor = aCol;
//cellStandfirst.textColor = aCol;
}
- (void)cellIcon:(NSString *)_text {
cellIcon.image = [UIImage imageNamed:_text];
}
- (void)dealloc {
[super dealloc];
}
#end
PS. I have set the custom class of the File's Owner in the Identity inspector to 'blogCellVC' and the identifier in the attributes inspector to 'MyCell'
I found the solution here:
No Visible View in Custom Table View Cell
In the custom uitableviewcell, in the identity inspector assign the Files Owner's class as the view with the table, and override uitableview cell with your custom class
My mistake as trying to re-use the custom class with a second view.
For completeness' sake, minimum code
parent.h
#import "blogCellVC.h"
#interface BlogsListingiPhoneVC : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
UITableView *tableView;
IBOutlet blogCellVC *tableCell;
}
parent.m
- (void)viewDidLoad
{
[super viewDidLoad];
[tableView registerClass: [blogCellVC class] forCellReuseIdentifier:#"MyCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIndentifier = #"MyCell";
blogCellVC *cell = (blogCellVC *)[self.tableView dequeueReusableCellWithIdentifier:MyIndentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[blogCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
[cell cellHead:info.blogName];
[cell cellHeadCol:[UIColor lightGrayColor]];
[cell cellIcon:#"blank.png"];
return cell;
}
blogCellVC.h
#import <UIKit/UIKit.h>
#interface blogCellVC : UITableViewCell {
IBOutlet UILabel *cellHead;
IBOutlet UIImageView *cellIcon;
}
- (void)cellHead:(NSString *)_text;
- (void)cellIcon:(NSString *)_text;
- (void)cellHeadCol:(UIColor *)aCol;
#end
blogCellVC.m
#interface blogCellVC ()
#end
#implementation blogCellVC
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"blogCellVC" owner:self options:nil];
self = [nib objectAtIndex:0];
}
return self;
}
Lastly make sure in the xib/storyboard you've put "MyCell" or whatever in the Identifier field in the Attributes Inspector.
This works in X Code 5, ios7

Custom UITableViewCell not showing content

I'm having major issues trying to get a custom UITableViewCell show any custom labels/controls. I have a similar project using same code and it works perfectly...the only difference is I use the table cell in a UITableViewController, as opposed to the problem page where I use the table cell in a UITableView embedded in a UIViewController. . Some searching of stackoverflow has suggested the following issues, all of which I have set up correctly:
1) CellIdentifier in tableView:cellForRowAtIndexPath: matches the storyboard identifier
2) All IBOutlets are connected properly
I suspect it may be something to do with the way I load the page (I do lots of page loading in viewDidLayoutSubviews method as I use storyboard autolayout and so I need access to frame sizes etc which isn't available in viewDidLoad)
Any suggestions?
Andy
EDIT: By the way it works perfectly if I use a standard UITableViewCell and instead of my custom label just use the cell.textLabel.text = #"..." etc. It seems to be something related to my custom cell.
viewController.h
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, weak) IBOutlet UITableView *tableView;
#property (weak, nonatomic) IBOutlet UIView *flipContainerView;
...
#end
viewController.m
#implementation ViewController
#synthesize tableView = _tableView;
#synthesize flipContainerView = _flipContainerView;
...
- (void)viewDidLayoutSubviews {
self.tableView.frame = CGRectMake((self.flipContainerView.frame.size.width / 2) - (self.flipContainerView.frame.size.width / 2),
(self.flipContainerView.frame.size.height / 2) - (self.flipContainerView.frame.size.height / 2),
self.flipContainerView.frame.size.width,
self.flipContainerView.frame.size.height);
...
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"List Cell";
ListCell *cell = (ListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[ListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.testLabel.text = nil;
cell.testLabel.text = #"test value";
return cell;
}
ListCell.h
#interface ListCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *testLabel;
#end
ListCell.m
#import "ListCell.h"
#implementation ListCell
#synthesize testLabel = _testLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end

Resources