i have a table view. And im adding two buttons to each cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
newBtn = [[UIButton alloc]init];
newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[newBtn setFrame:CGRectMake(250,10,25,55)];
[newBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside];
[newBtn setTitle:@"+" forState:UIControlStateNormal];
[newBtn setEnabled:YES];
newBtn.hidden = YES;
[cell addSubview:newBtn];
subBtn = [[UIButton alloc]init];
subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[subBtn setFrame:CGRectMake(280,10,25,55)];
[subBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside];
[subBtn setTitle:@"-" forState:UIControlStateNormal];
[subBtn setEnabled:YES];
subBtn.hidden = YES;
[cell addSubview:subBtn];
}
return cell;
}
And i want to have the buttons hidden at first, then when the table is in “edit” mode, i want these buttons to appear. and when the table leaves “edit” mode, the buttons disappear.
I can get one of the cells buttons to do this.
- (IBAction)editButton:(id)sender
{
if (self.editing)
{
[self setEditing:NO animated:YES];
[self.myTableView setEditing:NO animated:YES];
EditButton.title = @"Edit";
subBtn.hidden = YES;
newBtn.hidden = YES;
}
else
{
[self setEditing:YES animated:YES];
[self.myTableView setEditing:YES animated:YES];
EditButton.title = @"Done";
subBtn.hidden = NO;
newBtn.hidden = NO;
}
}
But the problem is: when i do this, only the very LAST cell gets the buttons. They appear and disappear exactly when i want, but only the last cell! No other cells get any buttons, could someone please help me! Thanks so much!
The way you are doing this
subBtnandnewBtnpoint to the buttons of the last cell. A better approach would be to subclassUITableViewCelland make the buttons instance variables. Then overwrite- (void)setEditing:(BOOL)editing animated:(BOOL)animatedand hide/show the buttons there.