Joson Daniel
Joson Daniel

Reputation: 409

UITableView can not show correct color of button in iOS

I used BVReorderTableView https://github.com/bvogelzang/BVReorderTableView to reorder row in tableview. But i have a problem: When I add UIButton to each row, this button always show clear color.Once selected row, this button just shown.

I tried this code:

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;

    cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UIButton *prioritybtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        [prioritybtn setFrame:CGRectMake(220, 0, 70, 32)];
        [prioritybtn setTag:4000];
        [cell.contentView addSubview:prioritybtn];

    }
     UIButton *priorityButton = (UIButton*)[cell.contentView viewWithTag:4000];
    if ([[items objectAtIndex:indexPath.row] isKindOfClass:[NSString class]] &&
        [[items objectAtIndex:indexPath.row] isEqualToString:@"DUMMY"]) {
        cell.textLabel.text = @"";
        cell.contentView.backgroundColor = [UIColor clearColor];
        prioritybtn.hidden= YES;
    }
    else {

       cell.textLabel.text = [items objectAtIndex:indexPath.row];
        [priorityButton setSelected:NO];
        [priorityButton setTitle:@"Priority" forState:UIControlStateNormal];
        priorityButton.enabled = YES;
    }

    return cell;
}

Also, I want to hide button only at first row, other row is still shown. How can i do that? Thanks

Upvotes: 0

Views: 181

Answers (3)

Muralikrishna
Muralikrishna

Reputation: 1044

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


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;
    UIButton *reletedbtn=nil;
    UILabel *Nameoflb=nil;

    cell = [tableeample dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil)
      {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }
    Nameoflb = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,40)];
    Nameoflb.backgroundColor=[UIColor clearColor];
    Nameoflb.textAlignment=NSTextAlignmentCenter;
    Nameoflb.textColor=[UIColor blackColor];
    [Nameoflb setFont:[UIFont fontWithName:@"Helvetica-Bold" size :14]];
    Nameoflb.text=[dataarray objectAtIndex:indexPath.row];
    [[cell contentView] addSubview:Nameoflb];
    if (indexPath.row!=0)
    {
        reletedbtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        reletedbtn.frame=CGRectMake(Nameoflb.frame.origin.x+Nameoflb.frame.size.width, Nameoflb.frame.origin.y+10,40, 25);
        reletedbtn.tintColor=[UIColor redColor];
        [reletedbtn setTitle:@"butoon" forState:UIControlStateNormal];

        //[reletedbtn addTarget:self action:@selector(statusMasseage:) forControlEvents:UIControlEventTouchUpInside];
        //reletedbtn.tag=1;
        [[cell contentView] addSubview:reletedbtn];

    }


    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

Upvotes: 0

Muralikrishna
Muralikrishna

Reputation: 1044

Try like this:-

 cell.selectionStyle = 
  UITableViewCellSelectionStyleNone;

Upvotes: 0

Nikos M.
Nikos M.

Reputation: 13783

To hide the button at first row:

    if (indexPath.row == 1){
      prioritybtn.hidden = YES;
      prioritybtn.userInteractionEnabled = NO;
    } else {
      prioritybtn.hidden = NO;
      prioritybtn.userInteractionEnabled = YES;
}

Add this before return cell;

Regarding the color, add this where you initialize the button:

 UIButton *prioritybtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    prioritybtn.backgroundColor = [UIColor greenColor]; //put the color you want

Upvotes: 3

Related Questions