0

I have a big problem with my UITableViewCell. I use storyboard and I have a custom cell into my UITableVIew. I add a Identifier to this, and my class code is:

static NSString *celldentifier = @"myCellId";

CustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:celldentifier];

    if (celldentifier == nil) {
        celldentifier = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celldentifier];
    }
...

Edit:

...
myCell.myButton.hidden = YES;
...

When I load other information in cell and I need hide the button, I do a reload table:

[self.myTable reloadData];

And in my viewController I reload this tableView showing or hidding some components in my cell like a UIButton. But when I do scroll, this buttons added disappear..

What is wrong in my code? How could I solve this problem?

user3745888
  • 6,143
  • 15
  • 48
  • 97
  • 1
    show the complete code. You need to track when you want to hide/show the components in cell. – Teja Nandamuri Aug 16 '17 at 18:05
  • The actual reason of the issue is below the three dots. But you should use this syntax `CustomCell *myCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:celldentifier forIndexPath: indexPath];` and delete the `celldentifier` check which will never be `nil`. – vadian Aug 16 '17 at 18:06
  • 2
    And the code you have doesn't really make sense. `celldentifier = [[CustomCell alloc]`? – Phillip Mills Aug 16 '17 at 18:06
  • I have added the rest of code, is only the assign values to UIButton or hide/show this, and reload table from my reload function whith the new information in NSArray... – user3745888 Aug 16 '17 at 18:15
  • 2
    Your approach is completely wrong. You have to keep the `hidden` state for each cell in the model, to hide/show a button change the value in the model and reload the cell or the entire view. Further if you are going to change the state of an UI element in `cellForRow` in an `if` clause you have to add an `else` clause to set a default value. – vadian Aug 16 '17 at 18:21
  • Thank you @vadian this was my stupid error... I was forgotten show the button then reload and always was hidde.. Thank you! – user3745888 Aug 16 '17 at 18:34

4 Answers4

1

the problem is that cells are getting reused.. if you have an if statement to make a change.. make sure to have put the else so you revert it

if(isReady){
    myCell.myButton.hidden = YES;
}
else {
    myCell.myButton.hidden = NO;
}
0

myCell.myButton.hidden = YES;

Its bad idea to use it out of cellForRowAtIndexPath method.

You should create model for cell and keep there all properties for cell.

if (myCell == nil) {
    myCell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celldentifier];
    //I think it should be but don't shure
    //[tableView registerClass:<#(nullable Class)#> forCellReuseIdentifier:<#(nonnull NSString *)#>];
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Arkhyp Koshel
  • 175
  • 11
  • 1
    What are those comments in the your code? How does the code in this answer help solve the problem? – rmaddy Aug 16 '17 at 19:29
0
static NSString *celldentifier = @"myCellId";
CustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:celldentifier];
if (!myCell) {
    myCell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celldentifier];
}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
iticle
  • 131
  • 1
  • 4
0

I had the same problem and you should add myCell.myButton.hidden = NO; this code before myCell.myButton.hidden = YES;

Like this:

static NSString *simpleTableIdentifier = @"myCellId";

CustomCell * myCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (myCell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    myCell = [nib objectAtIndex:0];
}

myCell.myButton.hidden = NO;

if(isReady){
    myCell.myButton.hidden = YES;
}
erim kurt
  • 91
  • 6