UITableViewCell with xib - ios

I'm developing an app for iOS which use the xib files.
Usually I use Storyboard and I don't know how to set up a UITableViewCell with xib files. When I make a xib file with a UITableView, I see a table with some row, now I need to edit this row to write what I stored in an array.
How can I use the xib file to design a UITableViewCell?
I need to do a very simple table: I would to use the Basic preset for the cell to display a title.
I know that I've to connect the delegate and DataSource to the file owner for table view and I put in the file owner the UITableViewDelegate and UITableViewDataSource.
Now how I can edit the content of the cell?
I found on the web some guide that tells to create a xib file with a UITableViewCell and I did it, but I don't know how to work with this

Firstly, you need to create the class for the customCell which inherits from UITableViewCell.
Now, add the properties you want to have in your customCell. In this example I added cellImage and cellLabel.
#property (nonatomic, strong) IBOutlet UILabel *cellLabel;
#property (nonatomic, strong) IBOutlet UIImageView *cellImageView;
After that you need to link the UILabel and the UIImageView from the CustomCell to the Nib.
You need to add:
- (void)viewDidLoad
{
....
[self.tableView registerNib:[UINib nibWithNibName:#"xibName" bundle:nil] forCellReuseIdentifier:CellIdentifier];
.....
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCellReuse";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
[cell.cellImageView setImage:[UIImage imageNamed:#"whatever"]];
[cell.cellLabel setText = #"whatever"];
return cell;
}

Related

this class is not key value coding-compliant for the key [Tableview error]

i am working on a application which has a table view inside a UIView.i have only one cell to display in the tableView. but it has to be a customised cell. i have added some lables and image into the cell. Before i added them as IBOutlets my app was running without an error. but immediately after i added IBOutlet, app crashes and showing me an error in error log as ,
this class is not key value coding-compliant for the key <myKey>
so far what i have done is , i added a UItableViewCell class to my customized cell. and also i added delegate and data source to my UIViewController class. and inside the cellForRowatIndexPath i tried this
static NSString *CellIdentifier = #"creditCardCell";
CreditCardCell *cell = [self.cardCell dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CreditCardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if i remove the Outlet from my UITableViewCell class, then app is working without any error. please help me someone. tell me what should i do for this.
Don't use IBOutlet for the cell. Make the prototype cell in tableView and
Add this in .h file and connect it with storyBoard/xib
#property (strong, nonatomic) IBOutlet UITableView *tableView;
Do this in cellForRowAtIndexPath in .m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"creditCardCell";
CreditCardCell *cell = (CreditCardCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[CreditCardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Good Luck!

Custom Prototype Cells - Storyboard Compilation Error

I am creating custom prototype cells in my program, but I am unable to get the program to compile.
Here's what it looks like (i've kept it fairly standard right now, just to get it working):
I have also created a new custom class homeTable, here's what homeTable.h looks like:
#interface homeTable : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *itemName;
#property (weak, nonatomic) IBOutlet UILabel *itemType;
#end
I've set the tableViewCell to have a the custom class homeTable.
And here is what the code in my masterViewController looks like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"KeyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *key = [ownedItems objectAtIndex:indexPath.row];
NSString *name = [key objectForKey:#"name"];
NSString *type = [key objectForKey:#"type"];
cell.itemName.text = name;
cell.itemType.text = type;
return cell;
}
When I try running the program it throws up a Interface Builder Storyboard Compilation failed error.
I've been stuck on this since yesterday, What am I doing wrong?
I may have miss a bigger issue but are here a few things that you could have done wrong.
First make sure your custom class and the cell identifier are correctly set-up in your storyboard.
Also right click on the tableViewController in the left panel inside the storyboard to take a look at your IB connections. You may have linked something you deleted later. You may see links with a ! telling you somethings isn't right.
Then you could change things in :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
First with storyboard no need for :
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Since cell will never be nil. Storyboard takes care of it, put a breakpoint there you'll see.
Also you are trying to access your custom properties
#property (weak, nonatomic) IBOutlet UILabel *itemName;
#property (weak, nonatomic) IBOutlet UILabel *itemType;
Without specifying the matching class, I would recommend you use your custom tableViewCell class as follow :
homeTable *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
By the way the name of your class should respect naming conventions. (But that will not help with your issue, just good practices).
If you still have a problem, I would recommend you post more details about what's going on in your storyboard.

UISearchBar with UITableView containing custom cells => blank cells

I have a UITableView containing custom cells. All works fine. But after, I decided to add a searchBar in order to... Search !
So, I added a "Search Bar and Search Display Controller" from the "Object Library" in my xib file, under the UITableView.
I created a specific class for the custom cell :
"CustomCell.h"
#class CustomCell;
#interface CustomCell : UITableViewCell
{
}
#property (weak, nonatomic) IBOutlet UILabel *lblDate;
#property (weak, nonatomic) IBOutlet UILabel *lblAuteur;
#end
"CustomCell.m"
no interesting stuff
"CustomCell.xib"
The "Event" class :
#interface Event : NSObject
{
}
#property (strong, nonatomic) NSString *desc;
#property (strong, nonatomic) NSString *dateCreation;
#end
And the view containing the UITableView and the UISearchBar :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
// Determinate the current event
Event *currentEvent;
if (tableView == self.searchDisplayController.searchResultsTableView)
currentEvent = [filteredArray objectAtIndex:indexPath.row];
else
currentEvent = [notFilteredArray objectAtIndex:indexPath.row];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.lblAuteur.text = [currentEvenement desc];
cell.lblDate.text = [currentEvenement dateCreation];
return cell;
}
Ok, now we can come to my problem. After loading the tableView, the custom cells are displaying well. But not if I use the SearchBar :
If there is an event with the desc attribute equal to "foo", and if I enter "bar" in the SearchBar, I obtain the "No results" message. It's normal.
If there is an event with the desc attribute equal to "foo", and if I enter "foo" in the SearchBar, the cells are displaying, but without their content ! I'm just seeing the cells' borders, and the lblDate and the lblAuteur are equal to nil.
Why have I this behaviour ? Thanks a lot for your help... I precise that the filteredArray and the notFilteredArray are correctly filled (I checked that many times). It means that the search mechanism is working well.
After much "searching" (pun) I've found the problem.
When you reference the tableview with the prototype cell you can't rely on the tableview that is passed in because sometimes that tableview is the search tableview without the prototype cell.
so instead of...
HomeTabCell *cell = (HomeTabCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
replace the "tableview" above with a direct connection to the table view with the prototype cell
HomeTabCell *cell = (HomeTabCell *)[self.HomeTableView dequeueReusableCellWithIdentifier:CellIdentifier];
Hope this helps!
Check these settings, if there not
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
try this,
Sorry for people searching to resolve this issue...
I could not fix it, and the prime contractor do not want this function anymore, so I gave up.

Style UITableViewCell inside UIScrollView/UIView

Overview
I've added a UITableView inside a UIScrollView using the storyboard.
My simple goal is to style the UITableViewCell's inside this table view.
Code
Currently, I have added a UITableView property to the view controller and added the outlet in the storyboard.
#interface NWDetailViewController : UIViewController<UITableViewDelegate,
UITableViewDataSource> {
IBOutlet UIScrollView *scrollView;
...
IBOutlet UITableView *tableView;
IBOutlet NSLayoutConstraint *tableViewHeightConstraint;
}
Created a custom UITableViewCell (NWLocationTableViewCell), added a property and connected the outlet in the storyboard:
#interface NWLocationTableViewCell : UITableViewCell {
IBOutlet UILabel *titleLabel;
}
And this is how i'm trying to populate the cell in my Controller:
-(UITableViewCell *)tableView:(UITableView *)thisTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"LocationCell";
// Same with both tableView and thisTableView
NWLocationTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[NWLocationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
[cell.titleLabel setText:#"test"];
// or
UILabel *label = (UILabel *)[cell.contentView viewWithTag:2112];
[label setText:#"test"];
NSLog(#"%#, %#", cell.contentView.subviews, cell.class);
return cell;
}
But both methods yield nothing, hence I can't style it. Adding any static objects (UIButton for example) also don't show up.
The logs show: "(), NWLocationTableViewCell"
Its a naming problem.
in your method header you pass thisTableView as the tableView to use, but then you call [tableView dequeueReusableCellWithIdentifier]; to get a cell. It should generate an error, but probably there is another variable by that name, or a property? Anyways, if thats your verbatim code, theres your error.

My custom UITableViewCell table cell shows up as a blank list of items

Basically I have a custom UITableViewCell which I made in my nib file. Within that I have some labels and whatnot that I want to be able to edit programatically. I called my nib file "post" and I made its file owner "post" and I set its class to "post" under file owner as well. I linked all my labels within my nib to this class:
post.h:
#import <Foundation/Foundation.h>
#interface post : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *title;
#property (nonatomic, weak) IBOutlet UILabel *authorComments;
#property (nonatomic, weak) IBOutlet UILabel *time;
#end
post.m:
#import "post.h"
#implementation post
#synthesize title = _title;
#synthesize authorComments = _authorComments;
#synthesize time = _time;
#end
An image of my nib file:
So now everything in my nib file is linked with my post class except for the actual cell itself because I was told I had to link that with a "view" but I'm not sure how (I tried linking it to backgroundView but this did not fix my problem). I've also tried giving the actual cell object the class post but it does not fix my problem.
Then in my controller I have the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// this is the identifier that I gave the table cell within my nib
static NSString *simpleTableIdentifier = #"post";
// grabs me a cell to use
post *cell = (post *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[post alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// sets the text on one of the labels within my cell to something else
cell.title.text = #"this is a test";
// returns my customized cell
return cell;
}
However when I run my app I just get this:
I know that the problem I'm having has to do with my custom cell, because I had it working previously, when I set the file owner on my nib to my controller, instead of attempting what I'm doing now which is trying to make a subclass of UITableViewCell. All I want to do is to be able to create a custom cell with multiple different labels on it, and then be able to edit the text on the cell programatically.
If it helps, here are all my source files and my nib file:
https://dl.dropboxusercontent.com/u/7822837/source.zip
You have to registerNib:forCellReuseIdentifier: in your UITableView, something like this:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:#"post" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"post"];
}
Swift:
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerNib(UINib(nibName: "post", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "post")
}
You have to do two things
In xib file
DisConnect all outlets of file owner and connect the outlets to your custom cell object
In code
Load the cell from nib
if (cell == nil) {
//cell = [[post alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell = [[[NSBundle mainBundle]loadNibNamed:#"post" owner:self options:nil]objectAtIndex:0];
}
Give the referencing Outlet to your ViewController, You didn't given a referencing outlet to your ViewController.

Resources