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
Related
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?
I'm new in iPhone development and have problem with prototype cell.
In my storyboard, I have navigation controller. I pushed it with view controller (as main window), it has a button, when I click it I open tableView controller with custom prototype cell
- (IBAction)searchClick:(id)sender {
CNCarTableController *car = [[CNCarTableController alloc]init];
[self.navigationController pushViewController:car animated:TRUE];
}
But when it opens it has empty rows
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: ( NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TransportCell";
CNTransportCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if(cell == nil){
cell = [[CNTransportCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//cell.textLabel.text = #"bmv";
cell.name = (UILabel*) [cell viewWithTag:100];
[cell.name setText:#"a text"];
cell.number.text = #"number";
cell.car.text = #"car";
return cell;
}
But if will use cell.textLabel.text = #"bmv"; all works ok. But for custom cell way with tags or cell.lbl.text don't work. What is wrong there? Can it be a nuance of navigation controller with view controller?
Custom cell code:
#interface CNTransportCell : UITableViewCell
#property (nonatomic,weak) IBOutlet UILabel *name;
#property (nonatomic,weak) IBOutlet UILabel *number;
#property (nonatomic,weak) IBOutlet UILabel *car;
#end
#implementation CNTransportCell
#synthesize name;
#synthesize number;
#synthesize car;
- (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
you have done small mistake there. Your custom cell class is "CNPlayerCell" & you are allocating new cells from "CNTransportCell" class.
One more thing, have you assigned custom class to your prototype cell in IB? (In identity inspector)
I Know this question is asked again and again here, but did not find any solutions for my problem.I'm creating a Custom UITableViewCell through xib and trying to load it.in my VC called ACSummaryVC
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"SimpleTableCell";
AcSummaryCell *cell = (AcSummaryCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"AcSummaryCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
}
AcSummaryCell is a subclass of UITableViewCelland it has an UILabel IBOutlate called acLabel.when i compile the project i get following error-
`Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ACSummaryVC 0x71465c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key acLabel.'`
i have created connection of acLabel in AcSummayCell class, but i'm getting error from ACSummaryVC? What i'm doing wrong?
Edit:- according to Mani's answer bellow i'm connecting outlate to file owner and it is wrong instead i've to connect outlate to custom cell.but when i try this i did not get my outlate to connect with instead i'm getting like this image -
Now question is How to connect my outlate with custom cell?
here is my AcSummayCell.h
#interface AcSummayCell : UITableViewCell
#property(nonatomic, retain)IBOutlet UILabel* acLabel;
#property(nonatomic, retain)IBOutlet UILabel* namelbl;
#property(nonatomic, retain)IBOutlet UILabel* cBalancelbl;
#property(nonatomic, retain)IBOutlet UILabel* avBalancelbl;
and AcSummayCell.m
#import "AcSummayCell.h"
#implementation AcSummayCell
#synthesize acLabel = _acLabel;
#synthesize namelbl = _namelbl;
#synthesize cBalancelbl = _cBalancelbl;
#synthesize avBalancelbl = _avBalancelbl;
- (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
You have setup a wrong outlet connection. Its because you have connected your label's outlet to your file's owner. Remove that label outlet connection out of file's owner and connect the outlet pointing to your custom cell. It should work.
follow this... for custom tableview cell
UITableViewCell Space between Title and Subtitle -iOS
or this awesome tutorial
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
follow this code properly....
AudioCell.h
#import<UIKit/UIKit.h>
#interface AudioCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UILabel *artistLabel;
...
#end
AudioCell.m
#import "AudioCell.h"
#implementation AudioCell
#synthesize titleLabel = _titleLabel, artistLabel = _artistLabel;
- (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
Now implement this code in your class at the TableView Data Source....
#import "AudioCell.h"
.....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"AudioCell";
AudioCell *cell = (AudioCell *)[self.audioFeedTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = (AudioCell *)[nibArray objectAtIndex:0];
[cell configurePlayerButton];
}
// Configure the cell..
cell.titleLabel.text = [nameArray objectAtIndex:indexPath.row];
cell.artistLabel.text = [commentsArray objectAtIndex:indexPath.row];
....
return cell;
}
Link your custom TableViewCell with the subClass by the same way....
I see your mistakes:
You don't specify Cell class and reuse identifier in your customCell
Create custom cell AcSummaryCell:UITableViewCell first then choose class for tableCell in the nib Cell. Also assign cell identifier in NIB. If correct , your cell must display
AcSummaryCell-SimpleTableCell in Object field.
For hookup to IBOutlet, just choose editor(show assistant editor) -> then choose Automatic-AcSummaryCell.h -> then drag and hookup IBOutlet you want.
I'm a newbie in objective-c and iOS
I created several custom table cells with outlets in it. But I'm not sure if I use the right way.
What I want to do is to get the property of an Outlet which is inside the custom cell, by an IBAction defined in the table view controller.
I have a custom table cell called KolonNumberCell, and a UISlider in it.
I want to define an IBAction called playNumbers in the UITableViewController. The slider seems to work fine in the custom cell. I can get its value.
But I cannot figure out how to get the value of this slider when the playButton outlet is tapped.
I would be so grateful if you could give me some information or show me a way.
Thanks in advance
TableController.h
#import <UIKit/UIKit.h>
#class KolonNumberCell;
#class BankoNumberCell;
#interface TableViewController : UITableViewController{
NSArray *bundleArray;
IBOutlet KolonNumberCell *kolonCell;
IBOutlet BankoNumberCell *bankoCell;
UIButton *playButton;
}
#property (strong) IBOutlet KolonNumberCell *kolonCell;
#property (strong) IBOutlet BankoNumberCell *bankoCell;
#property (nonatomic,retain) NSArray *bundleArray;
#property (nonatomic,retain) IBOutlet UIButton *playButton;
- (void)playNumbers:(id)sender;
#end
part of TableController.m
#import "TableViewController.h"
#interface TableViewController ()
#end
#implementation TableViewController
#synthesize bundleArray,kolonCell,bankoCell,playButton;
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *bundle1 = [[[NSBundle mainBundle] loadNibNamed:#"KolonNumberCell" owner:self options:nil] objectAtIndex:0];
NSArray *bundle2 = [[[NSBundle mainBundle] loadNibNamed:#"BankoNumberCell" owner:self options:nil] objectAtIndex:0];
bundleArray = [[NSArray alloc] initWithObjects:bundle1,bundle2, nil];
}
- (void)playNumbers:(id)sender
{
NSLog(#"button");
// Get the value of the slider in KolonNumberCell... but how?
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
tableView.allowsSelection = NO;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [bundleArray objectAtIndex:indexPath.section];
}
return cell;
}
#end
One of my customtableviewcell implementation file
#import "KolonNumberCell.h"
#implementation KolonNumberCell
#synthesize label,slider;
- (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
self.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"doubleRound.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0]];
}
- (IBAction)sliderChanged:(id)sender
{
[label setText:[NSString stringWithFormat:#"%i",(int)slider.value]];
}
+ (NSString *)reuseIdentifier {
return #"Cell";
}
#end
Your design is odd because your code will become really messy with more Cells.
Matt Gallagher has a nice implementation for Custom TableViews. He is using the TableViewCell as a Controller and puts all the logic related to cell in the cells class. The discussion thread on Stack Overflow:
CustomTableViewController
If you still want to do it your way:
Give your Slider a tag: slider.tag = x //Make sure that the tag is unique
In PlayNumbers:
UISlider *slider = (UISlider *)[[[bundleArray objectAtIndex:0] contentView] viewWithTag:x];
NSLog(#"%i",(int)slider.value);
Edit: thanks for correcting the syntax
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