How to add a custom UITableViewCell to a xib file objective-c - ios

I want to load my custom xib for tableviewcell in view controller.I can't load my xib in tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = _customCell;
_customCell = nil;
}
cell.topLabel.text = #"I am on top";
cell.bottomLabel.text = #"and I'm on the bottom";
return cell;
}
I cant't load my custom cell from xib into tableview.
Please help me.

There are two options:
OPTION 1
First you can Register the Custom Cell in viewDidLoad
UINib *nib = [UINib nibWithNibName:#"CustomCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:#"cell"];
Then in cellForRowAtIndexPath of TableView DataSource methods:
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.lblName.text = #"Tejas";
OPTION 2
If you are not registering the cell in viewDidLoad, you have to do the following things in cellForRowAtIndexPath of TableView DataSource methods:
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"cell"];
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:self options:nil];
if(cell == nil)
cell = nibs[0];
cell.lblName.text = #"Tejas";
return cell;

You can do like this in Tableview cellforrowAtIndexPath methods,
static NSString *CellIdentifier = #"CellData";
NSArray *arrData = [[NSBundle mainBundle]loadNibNamed:#"cellTaskDetail" owner:nil options:nil];
cellTaskDetail *cell = [[cellTaskDetail alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell = [arrData objectAtIndex:0];
return cell;
Its work fine...do it

Register the cell(XIB Cell) in the UITableView using the following method:
[self.tableView registerNib:[UINib nibWithName:nibName bundle:nil] forCellReuseIdentifier:#"CustomCell"];
Then you can dequeue the cell using the identifier you used for registering the cell.

NSString *simpleTableIdentifier = [NSString stringWithFormat:#"%d_%d",indexPath.section,indexPath.row];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nibArray objectAtIndex:0];
}
cell.topLabel.text = #"I am on top";
cell.bottomLabel.text = #"and I'm on the bottom";
return cell;

Using loadNibNamed simple solution
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"CellIdentifier";
YourCustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{ cell = [[[NSBundle mainBundle] loadNibNamed:#"YourCustomeCell" owner:self options:nil] objectAtIndex:0];
}
// do code with cell here
return cell;
}

Related

Show data in Custom tableview Cell

I am new to developing. i have some experience but i'm not great at it (yet). I'm working on this tableview app which shows data. I want to show that data in a custom cell. But i don't know how to do that.
This is the code i use for my tableview:
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"HistoryViewTableCell"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"HistoryViewTableCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
NSDictionary * tempDictionary = [tableData objectAtIndex:indexPath.row];
cell.textLabel.text = [tempDictionary objectForKey:#"PickupAddress"];
return cell;
}
I need to show this line of code in my custom cell.
cell.textLabel.text = [tempDictionary objectForKey:#"PickupAddress"];
I already made a class and a xib file for the custom cell
This may solve the problem:
HistoryTableViewCell *cell = (HistoryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"HistoryViewTableCell"];
your written kind of code will not work. if you are using custom XIB than add a UILable in the xib. and than assign them tag number.
now from ViewController do the following:
write this after #implementation
enum {
CELL_TITLE = 2,
CELL_SUBTITLE_1 = 3,
};
Load the Custom XIB File in cellforrowatindexpath method.
// Get table View Cell
static NSString *CellIdentifier = #"customCell";
UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = (UITableViewCell *) [nib objectAtIndex:0];
}
Now Add text on Lable with the below methods;
UILabel *titleLable = (UILabel *) [cell.contentView viewWithTag:CELL_TITLE];
titleLable.text = #"This is your text";

Customise single cell in UITableview

I would like to change the format of the first cell in my UITableView (masterViewController), so that the image stretches across thewidth of the whole cell and the text label is shown above the image. Also would like to add a second text label to this cell only?
Any help on how to do this would be great, I have looked without success for this particular problem and I am fairly new to UITableViews.
Make one custom cell as per your requirement and load it for indexpath.row== 0 and simple UITableViewCell for all other cells. hope it will helps you
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CustomCell";
CustomCell *customcell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
static NSString * identifier = #"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(indexPath.row == k)
{
if(customcell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
scrlcell = [nib objectAtIndex:0];
}
}
else{
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
}
if(indexPath.row == k)
return customcell;
else
return cell;
}
You should build a custom cell (you can build it in Interface builder) and then use it on your indexPath.row == 0. Like this:
static NSString *CellIdentifier = #"CellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
NSArray *topLevel = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
for (id currentObject in topLevel){
if ([currentObject isKindOfClass:[CustomCell class]])
{
cell = currentObject;
break;
}
}
}
if(indexPath.row == 0){
// Use your cell here
}else{
// Build a new cell
}
return cell;

UITableViewCells not getting reused when scrolling

I'm building an app where a screen should show a tableview where in the tableview first two row are two different custom cells and the rest are different custom cells. So I have did this in my cellForRowAtIndexPath:
My Code:
if (indexPath.row == 0 && !isSearchEnabled) {
FirstRowViewCell *cell;
static NSString *CellIdentifier = #"FirstCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// load a new cell from the nib file
[[NSBundle mainBundle] loadNibNamed:#"ContactCell" owner:self options:nil];
cell = self.firstCell;
//cell.delegate = self;
//cell.indexPath = indexPath;
self.firstCell = nil;
}
return cell;
} else if (indexPath.row == 1 && !isSearchEnabled) {
SecondRowViewCell *cell;
static NSString *CellIdentifier = #"SecondCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// load a new cell from the nib file
[[NSBundle mainBundle] loadNibNamed:#"ContactCell" owner:self options:nil];
cell = self.secondCell;
//cell.delegate = self;
//cell.indexPath = indexPath;
self.secondCell = nil;
}
return cell;
}else {
static NSString *CellIdentifier = #"ContactCell";
ContactTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"ContactCell" owner:self options:nil];
// load a new cell from the nib file
cell = self.contactCell;
cell.delegate = self;
cell.indexPath = indexPath;
self.contactCell = nil;
}
return cell;}
but its lagging when scrolling the tableview. When I checked with the Time Profiler in instruments its showing that cellForRowAtIndexPath especially the
[[NSBundle mainBundle] loadNibNamed:#"ContactCell" owner:self options:nil];
is taking much time to load which causes lag. My question is, do the cell getting reused or not, because the above line getting executed for each and every cell even we are scrolling to the top. Please help me to get through the lag problem. Thanks!
try this may be it will helps
static NSString *simpleTableIdentifier = #"ContactCell";
ContactCell *cell = (ContactCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ContactCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
You are creating cells that do not have the reuseIdentifier set. reuseIdentifier is a readonly property of UITableViewCell. What you should do instead is register the nib with the tableview, perhaps in the viewDidLoad or viewWillAppear method of your UIViewController.
UINib *cellNib = [UINib nibWithNibName:#"ContactCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
Once you do this, dequeueReusableCellWithIdentifier: will always return a valid cell, whether it is instantiating it or reusing it for you.
You can refer to this Apple documentation: UITableView, UITableViewCell, and UINib.

Custom cells issue in iOS 5

I've a custom UITableViewCell subclass and I've read that this is supposed to be the correct way of loading custom cells for iOS 5:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tv dequeueReusableCellWithIdentifier:#"customCell"];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"customCell"];
// Configure cell
cell.nameLabel.text = self.customClass.name;
}
return cell;
}
But the label text is not shown when I run the app. However, I also tried this way:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"customCell"];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell *)view;
}
}
// Configure cell
((CustomCell *)cell).nameLabel.text = self.customClass.name;
}
return cell;
}
This way the label is displayed, but any reuseIdentifier is set in loadNibName: method. What the best way of loading custom cells should be? I need to support iOS 5+. Could the first approach not be working because I'd to configure cell's labels and styles in initWithStyle: method and not in the table view's method?
Thanks!
The error in your code is that cell.nameLabel.text is only set in the if (cell == nil) { ... } case and not generally.
The easiest ways to load custom cells are
Use registerNib:forCellReuseIdentifier: if the cell is defined in a nib file, or
use registerClass:forCellReuseIdentifier: if the cell is created programmatically, or
use a Storyboard and define "CustomCell" as "Prototype Cell" for the table view.
For example (in viewDidLoad):
[self.tableView registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellReuseIdentifier:#"customCell"];
Then dequeueReusableCellWithIdentifier
will always return a cell, so that cellForRowAtIndexPath simplifies to
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tv dequeueReusableCellWithIdentifier:#"customCell"];
cell.nameLabel.text = self.customClass.name;
return cell;
}
Just use this code and try.
static NSString *cellIdentifier=#"cell";
YourCustomCell *cell = (YourCustomCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSString *customeCellName = [NSString stringWithFormat:#"%#",[YourCustomCell class]];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (YourCustomCell *) currentObject;
break;
}
}
}
cell.nameLabel.text = self.customClass.name; // Make sure self.customclass.name have value.
return cell;
Use this code (NewCustomCell is your Custom UITableViewCell class name)...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NewCustomCell *cell = (NewCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"NewCustomCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
return cell;
}
Happy coding... :)

Reuse Custom Cell using NIB

Hi I'm new to IOS developement and I created a UITableView which uses custom cells that are created in a nib. Below is my code from my ViewController that is loading the cells however if I scroll up and down 3 times the app crashes because I don't think I'm reusing the cells correctly. I googled around but much of the code/solutions I found seemed to be outdated. My code is below any help is greatly appreciated!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:#"cellIdentifier"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
cell.TITLE.text = [NSString stringWithFormat:#"\"%#\"", [TITLE objectAtIndex:indexPath.row]];
cell.desc.text = [desc objectAtIndex:indexPath.row];
cell.votes.text = [votes objectAtIndex:indexPath.row];
return cell;
}
change the row
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:#"cellIdentifier"];
to be
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
Go to your CustomCell .xib file in IB, look for the identifier field and set it to CustomCell
You can register the nib for tableView, like :
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
[tableView registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
//...
return cell;
}

Resources