Custom UITableViewCell not showing content - ios

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

Related

Reuse custom TableViewCell

I have a viewController that contains a programmatically created tableView.
#property (strong, nonatomic) UITableView *numbersTableView;
//...
self.numbersTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -100, self.view.frame.size.width-20, 50)];
self.numbersTableView.delegate = self;
self.numbersTableView.dataSource = self;
self.numbersTableView.tag = 1;
self.numbersTableView.layer.cornerRadius = 5;
[self.numbersTableView setBounces:false];
[self.numbersTableView setHidden:true];
[self.numbersTableView registerClass:[AZGCountryCell class] forCellReuseIdentifier:#"NumbersTableViewCell"];
[self.view addSubview:self.numbersTableView];
For the prototype cell I want to use a prototype that I created somewhere else in another viewController and I designed It in storyboard.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView.tag == 1) { //tableView == self.numbersTableView
NSString *cellID = #"NumbersTableViewCell";
AZGCountryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[AZGCountryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.countryName.text = self.numbersArray[indexPath.row].number;
cell.flagImageView.image = [UIImage imageNamed:self.numbersArray[indexPath.row].country];
return cell;
}
}
And my custom cell contains one UIImageView and one UILabel but when I run the code the cells are empty and I can see the UIImageView and UILabel are nil!
#import <UIKit/UIKit.h>
#interface AZGCountryCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *flagImageView;
#property (strong, nonatomic) IBOutlet UILabel *countryName;
#property (strong, nonatomic) IBOutlet UILabel *countryCode;
#end
#import "AZGCountryCell.h"
#implementation AZGCountryCell
#synthesize flagImageView;
#synthesize countryName;
- (void)awakeFromNib {
[super awakeFromNib];
self.flagImageView.layer.cornerRadius = self.flagImageView.frame.size.width/2;
self.flagImageView.layer.masksToBounds = YES;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
#end
Then what should I do to have properly filled cells in my numbersTableView?
Unfortunately you can't reuse a table cell you've defined in a table view in a storyboard in your separate class.
In order for you to be able to share your cell between the different views, you'll have to extract the view to a separate nib and register that nib go be used on both tables.
Alternatively you can implement the cell's UI in code in the table view cell subclass.
You getting empty cell because custom tableview cell not registered, use
[self.numbersTableView registerNib:[UINib nibWithNibName:#"AZGCountryCell" bundle:nil] forCellReuseIdentifier:#"NumbersTableViewCell"];
instead of
[self.numbersTableView registerClass:[AZGCountryCell class] forCellReuseIdentifier:#"NumbersTableViewCell"];

Self-sizing UITableViewCell with UITableView inside it - self-sizing doesn't work

I am having trouble with the new concept of self-sizing cells. They work great for simple custom cells, however, I am trying to have a UITableView inside one of my custom UITableViewCells. I thought I had set up everything correctly, the UITableView inside the cell has constraints and everything and the delegates and datasources are connected as well. What's happening is that 'numberOfRowsInSection' in ChecklistTableViewCell gets called and returns 5, but not the corresponding cellForRowAtIndexPath. Therefore, the cell that should include another UITableView is only shown as a smaller cell with no content.
My 'research' via Google has told me that cellForRowAtIndexPath might not get called because the space for the cells is too small. So, I set the rowHeight of all cells to some constant and the UITableView inside the cell is displayed - but I loose the self-sizing functionality.
Therefore, my question, do self-sizing cells not work with more complex components within custom cells or am I missing something basic or important?
First, the code of my UIViewController:
#interface MyViewController ()
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (nonatomic, strong) NSArray *elements;
#end
#implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.estimatedRowHeight = 500.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
#pragma mark - UITableView methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Element *element = self.elements[indexPath.row];
if (something) {
...
} else if (something else) {
ChecklistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifier"];
if (cell == nil) {
cell = [[ChecklistTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"identifier"];
}
cell.checklist = element.checklist;
return cell;
} else {
...
}
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.elements count];
}
#end
Here is my code for the cell that has a UITableView inside of it:
#interface ChecklistTableViewCell : UITableViewCell <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *checklistTableView;
#property (strong, nonatomic) NSArray *checklist;
#end
#import "ChecklistTableViewCell.h"
#implementation ChecklistTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
// Setup table view (self-sizing cells!)
self.checklistTableView.estimatedRowHeight = 50.0;
self.checklistTableView.rowHeight = UITableViewAutomaticDimension;
}
#pragma mark - UITableView methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ChecklistElementTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"someIdentifier"];
if (cell == nil) {
cell = [[ChecklistElementTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"someIdentifier"];
}
cell.checklistElementTitleLabel.text = self.checklist[indexPath.row];
return cell;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.checklist count];
}
#end
And the code for the UIChecklistElementTableViewCell (there's no 'special' code in the .m file):
#interface ChecklistElementTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *checklistElementTitleLabel;
#property (strong, nonatomic) IBOutlet M13Checkbox *checkbox;
#end
In the end, I went with implementing
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
where I return the calculated height for the cell with the table view in it and for all other cells, I return UITableViewAutomaticDimension.
It's not what I had hoped for, but it works. If anyone has another solution, I'm still very interested.
Are you sure that you set correctly you Autolayout's constraints for the cell's elements?
Did you set all the UILabel's line numbers to 0?

Empty custom prototype cell

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)

Accessing the IBOutlets's properties in the custom UITableViewCell with an IBAction

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

custom cell not appear in tableview

I use storyboards. I create new class for my Cell, in storyboard I write in Reuse Identifier "userFullCell". My Cell class .h
#import <UIKit/UIKit.h>
#interface UsefullLinksCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *image;
#property (weak, nonatomic) IBOutlet UILabel *shortTitle;
#end
.m default
#import "UsefullLinksCell.h"
#implementation UsefullLinksCell
- (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
}
property image and shortTitle connected in storyboard with UILabel and UIImageView in Cell
Then in my class with UITableView I import "UseFullLinksCell.h" and in viewDidLoad wrote:
[self.tableView registerClass:[UsefullLinksCell class] forCellReuseIdentifier:#"userFullCell"];
tableView is my outlet:
#property (weak, nonatomic) IBOutlet UITableView *tableView;
and then I try to create cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"userFullCell";
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *info = resourceArray[indexPath.row];
if(info){
cell.image.image = info[#"logo"];
cell.shortTitle.text = info[#"title"];
}
return cell;
}
So, cell initialize, but image and text not assigned, and after going end of method return 0x0000000 for cell. If I create for standart cell code in this method, I mean UITableViewCell *cell, there is all good. Where I could make mistake?
PS Sorry I can not give screenshots from storyboard, because I write from not my computer, if you want, I provide images later
You should register the xib file (with registerNib:forCellReuseIdentifier:) where you made your custom cell, not the class. Also, I don't know if this will make a difference or not, but replace this:
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
With this (notice the forIndexPath: at the end of the method):
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
After Edit:
If added a custom cell to your table view in the storyboard, and gave it the identifier "userFullCell", then you don't need to register anything -- in fact, registering your class makes it not work. So, just comment out that register statement, and see if that works.
You have missed something:
NSArray *objects=[[NSBundle mainBundle]loadNibNamed:#"UsefullLinksCell" owner:self options:nil];
UsefullLinksCell *cell =[objects objectAtIndex:0];
this will give you the object of your custom cell.
I think you have to do the following
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
image=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,50.0,50.0)];
shortTitle=[[UILabel alloc] initWithFrame:CGRectMake(60.0,0.0,200.0,50.0)];
}
return self;
}
Else all should work.. The thing is your components are not initialized anywhere, either it should be loaded from xib or, you have to initialize them manually.
Hope this helps.

Resources