So I’m creating a UIButton programmatically based on whether a value on one of my objects is > 0. However when I edit that value and reload the table it wont remove the button. The value is definitely > 0 and not nil as its being displayed in a label.
Ive tried adding the button to every cell and then setting its hidden property, which is giving me the same behavior as the code below. If I stop the app and re-run the app it displays how it should.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
CGRect newIconRect = CGRectMake(280, 5, 33, 33);
UIButton *warningButton = [[UIButton alloc] initWithFrame:newIconRect];
warningButton.tag = 66;
[cell.contentView addSubview:warningButton];
}
UIButton *warningButton = (UIButton *)[cell.contentView viewWithTag:66];
[warningButton setImage:[UIImage imageNamed:@"exclamation.png"] forState:UIControlStateNormal];
[warningButton addTarget:self action:@selector(warningButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
if (ueo.daysLeft >= 0)
{
daysLeftLabel.text = [[NSString alloc]initWithFormat:@"%i recurringDays to go", ueo.daysLeft];
warningButton.hidden = YES;
}
else
{
daysLeftLabel.text = [[NSString alloc]initWithFormat:@"%i recurringDays have passed", ueo.daysLeft];
warningButton.hidden = NO;
}
}
You are creating duplicate buttons in the cell if you are doing it as shown in the question. So you will not be able to remove the buttons since you might have already lost the reference to button objects. For reach reload or table view scroll, the above code will create new buttons and add on top of the cell.
You can either customize the
UITableViewCelland add this button in theinitmethod of that cell class. After that you can show/hide usingAn easy but not recommended approach is to do this part in
if (cell == nil),For eg:-