Custom cell in UITableView make images overlap on scroll - ios

I have created custom table view cell that have 2 images on it (initially set to be hidden).
When I render each cell I check some status and set images visible/hidden property.
When I open that table it looks fine when I scroll to the bottom and back to the top first 2-3 cells have both image displayed.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"myCustomCell";{
OrderCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
[tableView registerNib:[UINib nibWithNibName:#"myCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
...
cell.Title.text = #"some value";
...
if(...){
cell.image1.hidden = YES;
cell.image2.hidden = NO;
}
else{
cell.image1.hidden = NO;
cell.image2.hidden = YES;
}
...
Why this could happen?
Is the problem maybe with CellIdentifier.
UPDATE
First try:
OrderCustomCell *cell;
if(cell == nil){
[tableView registerNib:[UINib nibWithNibName:#"myCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:nil];
}
Second try:
OrderCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
[tableView registerNib:[UINib nibWithNibName:#"myCustomCell" bundle:nil] forCellReuseIdentifier:nil];
cell = [tableView dequeueReusableCellWithIdentifier:nil];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"myCustomCell";
OrderCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if(cell == nil){
[tableView registerNib:[UINib nibWithNibName:#"myCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
...
cell.Title.text = #"some value";
...
if(...){
cell.image1.hidden = YES;
cell.image2.hidden = NO;
}
else{
cell.image1.hidden = NO;
cell.image2.hidden = YES;
}
...

Use this as an identifier.
NSString *identifier =[NSString stringWithFormat:#"%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
I hope this will resolve your problem. I had same issue, fixed now.

You can use, it works for me.
OrderCustomCell * orderCustomCell = (OrderCustomCell *)[tableView dequeueReusableCellWithIdentifier:nil];
if (orderCustomCell==nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"OrderCustomCell" owner:self options:nil];
orderCustomCell = [nib objectAtIndex:0];
}

Related

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

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;
}

UISearchDisplayController with custom cell

I have a Custom Cell designed in storyboard, it is inside a UITableViewController which is working fine with the custom cell.
Now, I'm trying to use the same cell on a UITableViewController with a UISearchDisplayController and it is not working.
This is my method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
}
cell.titleLabel.text = object[#"title"];
cell.subtitleLabel.text = object[#"subtitle"];
return cell;
}
It just return white, regular cells, and if I use the default cell.textLabel.text it shows my objects.
Try this, set Tag on storybord
http://i.stack.imgur.com/eVXPE.jpg
image from AppCoda
And change your method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
}
//Change this - START
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
UILabel *subtitleLabel = (UILabel *)[cell viewWithTag:101];
titleLabel.text = object[#"title"];
subtitleLabel.text = object[#"subtitle"];
//Change this - END
return cell;
}
If you have made the custom cell inside a tableview on the storyboard I don't know if you can use it in another tableview either remake the custom cell in the new tableView on the story board or make a seperate custom .xib just for the cell and load them in you cellForRow methods with something like..
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
[tableView registerNib:[UINib nibWithNibName:#"MyCustomCellNib" bundle:[NSBundle mainBundle] forCellReuseIdentifier:CellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
Or even better, you could just make a small change in your existing code, and hopefully it will work:
From:
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
To:
CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Or in Swift
From:
var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell
To:
var cell = self.tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell
Credits

Missing accessory in custom UITableViewCell

Before I created custom cell for UITableView I used this to render rows:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
Then I have created CustomCell with two UILabels in it.
And replace code from above with:
static NSString *CellIdentifier = #"customCell";
OrderCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
[tableView registerNib:[UINib nibWithNibName:#"MyCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
But now accessory is not visible.
It shows only at first row and it doesn't look like before.
Is there anything else that I need to do to show accessory when creating custom cell?
Modify your code as below. Set the accesssory Type outside of condition..
static NSString *CellIdentifier = #"customCell";
OrderCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
[tableView registerNib:[UINib nibWithNibName:#"MyCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
You have set the accessoryType inside the condition if(cell==nil) which calls first time..
Hope it fix the issue..
If you're using storyboard, then you have to register your custom nib in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:#"YourClass" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"YourCellIdentifier"];
}
If you're not using storyboard, you have to register it in your cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"YourCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:#"YourCustomClass" owner:self options:nil] lastObject];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
////
return cell;
}
You have to register your nib in viewDidLoad instead of cellForRowAtIndexPath
- (void)viewDidLoad
{
[super viewDidLoad];
static NSString *CellIdentifier = #"customCell";
[tableView registerNib:[UINib nibWithNibName:#"MyCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
}

Extended TableViewCell blank

I'm trying to customize a UITableViewCell, but for some reason it's showing up as blank. Is there something obvious I'm doing wrong? I've pulled the menuLabel from the storyboard as an outlet, so it's tied correctly, and the cell in the storyboard is linked to the class "MenuCell".
In my tableview controller
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MenuCell";
[self.tableView registerClass:[MenuCell class] forCellReuseIdentifier:CellIdentifier];
MenuCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
NSLog(#"creating a new cell");
cell = [[MenuCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"MenuCell"];
}
cell.menuLabel.text = #"Hello";
return cell;
}
You can use this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MenuCell";
MenuCell *cell = (MenuCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
UINib* customCellNib = [UINib nibWithNibName:#"MenuCell" bundle:nil];
// Register this nib file with cell identifier.
[tableView registerNib: customCellNib forCellReuseIdentifier:CellIdentifier];
cell = (MenuCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
// Whatever you want
retrun cell;
}
Hope this will help. happy coding :P
Use this one ....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"MenuCell";
MenuCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[MenuCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell = (MenuCell *)[[[NSBundle mainBundle] loadNibNamed:#"MenuCell" owner:self options:nil] objectAtIndex:0];
}
cell.menuLabel.text = #"Hello";
return cell;
}
I Hope it will be helpful.

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