iAsmi iAsmi - 4 months ago 13
iOS Question

how to hide button on particular cell of tablview?

I want to hide button on some cell of

UITableView
, but I am not able to find what was I did wrong , some cell of my
UITableView
have both text and button and some cell have only text with single line or multiple line. I want to show button on some cell of
UITableView
. I did this code :

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellidentifier= @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath];

UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(5, 2, 100, 30);
[button setTitle:@"World" forState:UIControlStateNormal];
[button addTarget:self action:@selector(openNewPage:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor= [UIColor clearColor];
button.tag = indexPath.row;
[cell addSubview:button];

if(indexPath.row ==0)
{


cell.textLabel.attributedText = [dataArray objectAtIndex:0];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
if (indexPath.row == 1)
{


[cell.imageView sd_setImageWithURL:[NSURL URLWithString:[dataArray objectAtIndex:1]] placeholderImage:nil];
cell.imageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
}
if(indexPath.row ==2)
{


cell.textLabel.attributedText = [dataArray objectAtIndex:2];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
if(indexPath.row ==3)
{

cell.textLabel.attributedText = [dataArray objectAtIndex:3];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
if(indexPath.row ==4)
{

cell.textLabel.attributedText = [dataArray objectAtIndex:4];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;


}
if( indexPath.row == 5)
{

cell.textLabel.attributedText =[dataArray objectAtIndex:5];
cell.textLabel.textAlignment = NSTextAlignmentRight;
}


if(indexPath.row == 6)

{
//[button setHidden:YES];
cell.textLabel.attributedText = [dataArray objectAtIndex:6];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;

}
if ( indexPath.row == 6 || indexPath.row == 5)
{
[button setHidden:NO];
}
else {
[button setHidden:YES];
}
return cell;
}


But this code will add button on 6th and 5th cell also on 2nd and 3rd cell also , I want button only 6th and 5th cell.

Answer

You shouldn't add a button as [cell addSubView: button] inside cellForRowAtIndexPath. dequeReusableCellForIndexPath asks the tableView for a cell. The tableView creates one if it doesn't have any or passes an old one. If you receive and old one, then I may already have a button. If you add another one, you will end up with a stack of buttons.

You can achieve what you want in two ways: You can create your cell in the storyboard, for example, add the button and create an outlet in a subclass of UITableViewCell. Then you can hide or unhide the button according to what cell is.

The other way, if buttons are too different, you can create two cells in the storyboard with different identifiers, for example "cellWithButton" and "cellWithoutButton".

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     UITableViewCell *cell;

    if (indexPath.row < 5) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellWithoutButton"];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellWithButton"];
    }

    // Do the rest of your configuration

    return cell;
}
Comments