0

In my application I have custom tableView cell which having UILabels with varying heights and two buttons. I have tried number of solution but still no luck. Could you please suggest where I am wrong in following code....

- (void)viewDidLoad
{
[super viewDidLoad];

self.myChklist.rowHeight = 10;
contentView.hidden = YES;
buildingQuest = [[NSMutableArray alloc]initWithObjects:@"Motor vehicles are not parked in the space",@"The complex spatial separation is not flammable by cutting of the substances released in the space",@"Is the provided interval for the Complex spatial Separation yet compiled? 1>Minimum distance between buildings is the height of higher building, at least 5m. 2>Minimum distance between buildings and warehouses of combustible materials at least 20 m", @"Are the openings (e.g. edges of the corrugated plates) completely covered in the terminal area with non-combustible material?",@"Information on both sides exists, that in the closing no materials may be suppressed, the presence of persons is prohibited in the closed area and that the fire doors must remain open only as long as necessary for operational reasons.",@"Motor vehicles are not parked in the space",nil ];

myChklist.delegate = self;
myChklist.dataSource = self;
[self.myChklist reloadData];

}

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

contentView.hidden = YES;
if( tableView == myChklist)
{

     static NSUInteger const kQuestLabelTag = 1;
    static NSUInteger const kYesButtonTag = 2;
    static NSUInteger const kNoButtonTag = 3;

   UILabel *QuestLabel = nil;
    UIButton *YesButton;
    UIButton *NoButton;
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


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

        cell.selectionStyle = UITableViewCellSelectionStyleGray;           //cell bg
        self.myChklist.backgroundColor = [UIColor clearColor];

        QuestLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 300, 0)];
        QuestLabel.tag = kQuestLabelTag;
        QuestLabel.numberOfLines = 9;//ceilf([[buildingQuest objectAtIndex:indexPath.row] sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping].height);// add this  line

        QuestLabel.backgroundColor = [UIColor clearColor];
    QuestLabel.font = [UIFont systemFontOfSize:16];


        NSString *titleString = [buildingQuest objectAtIndex:indexPath.row] ;
        CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

        UIButton *YesButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];//[[UIButton alloc]
        [YesButton setFrame:CGRectMake(10, titleSize.height+5, 60, 30)];
        [YesButton setTitle:@"Yes" forState:UIControlStateNormal];


        YesButton.tag = kYesButtonTag;

        [YesButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
        YesButton.titleLabel.textColor = [UIColor blackColor];
        [YesButton addTarget:self action:@selector(yesAction) forControlEvents:UIControlEventTouchUpInside];
         [cell.contentView addSubview:YesButton];

        UIButton *NoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [NoButton setTitle:@"No" forState:UIControlStateNormal];
        [NoButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
        NoButton.titleLabel.textColor = [UIColor blackColor];

        [NoButton setFrame:CGRectMake(95, titleSize.height+5, 60, 30)];
        NoButton.tag = kNoButtonTag;
        [NoButton setTag:indexPath.row];
        [NoButton addTarget:self action:@selector(NoAction:) forControlEvents:UIControlEventTouchUpInside];

        [cell.contentView addSubview:QuestLabel];

        [cell.contentView addSubview:NoButton];


    }
    else

   {

    QuestLabel = (UILabel *)[cell.contentView viewWithTag:kQuestLabelTag];
    YesButton = (UIButton *)[cell.contentView viewWithTag:kYesButtonTag];
    NoButton = (UIButton *)[cell.contentView viewWithTag:kNoButtonTag];

    }


    QuestLabel.text = [buildingQuest objectAtIndex:indexPath.row];

    questionString = [NSString stringWithFormat:@"%@",QuestLabel.text];

    QuestLabel.numberOfLines = ceilf([[buildingQuest objectAtIndex:indexPath.row] sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping].height);
    NSString *titleString = [buildingQuest objectAtIndex:indexPath.row] ;
    CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

    [QuestLabel sizeToFit];
    return cell;

}

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *titleString = [buildingQuest objectAtIndex:indexPath.row] ;
CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

return (titleSize.height)+70;
}

Update 1: Now my label is working properly by following Rajesh answer. But setting button tag is mixing my buttons within cell.

[NoButton setTag:indexPath.row]; //removing this line from tableview cell the button adjust properly, but on  button action it not provide me the index.row label.

-(void)NoAction:(id)sender       //button action
{

  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
  UITableViewCell *cell = [myChklist cellForRowAtIndexPath:indexPath];
   // NSIndexPath *selectedIndexPath = [self.myChklist indexPathForSelectedRow];
  questionString = [buildingQuest objectAtIndex:indexPath.row];

  checklistController *chk = [[checklistController alloc]initWithNibName:@"checklistController" bundle:nil];
  [self presentViewController:chk animated:YES completion:nil];

 chk.passedQuestString = questionString;
 }

Could you please help.

Navnath Memane
  • 265
  • 1
  • 8
  • 25

1 Answers1

0

I have edited the code , pelease check with below code.

Instead of [QuestLabel sizeToFit];I set the label height dynamically

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


        static NSUInteger const kQuestLabelTag = 1;
        static NSUInteger const kYesButtonTag = 2;
        static NSUInteger const kNoButtonTag = 3;

        UILabel *QuestLabel = nil;
        UIButton *YesButton;
        UIButton *NoButton;
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


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

            cell.selectionStyle = UITableViewCellSelectionStyleGray;           //cell bg
            self.tableView1.backgroundColor = [UIColor clearColor];

            QuestLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 300, 0)];
            QuestLabel.tag = kQuestLabelTag;
            QuestLabel.numberOfLines = 9;//ceilf([[buildingQuest objectAtIndex:indexPath.row] sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping].height);// add this  line

            QuestLabel.backgroundColor = [UIColor clearColor];
            QuestLabel.font = [UIFont systemFontOfSize:16];


            NSString *titleString = [buildingQuest objectAtIndex:indexPath.row] ;
            CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

            UIButton *YesButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];//[[UIButton alloc]
            [YesButton setFrame:CGRectMake(10, titleSize.height+5, 60, 30)];
            [YesButton setTitle:@"Yes" forState:UIControlStateNormal];


            YesButton.tag = kYesButtonTag;

            [YesButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
            YesButton.titleLabel.textColor = [UIColor blackColor];
            [YesButton addTarget:self action:@selector(yesAction) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:YesButton];

            UIButton *NoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [NoButton setTitle:@"No" forState:UIControlStateNormal];
            [NoButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
            NoButton.titleLabel.textColor = [UIColor blackColor];

            [NoButton setFrame:CGRectMake(95, titleSize.height+5, 60, 30)];
            NoButton.tag = kNoButtonTag;
            [NoButton setTag:indexPath.row];
            [NoButton addTarget:self action:@selector(NoAction:) forControlEvents:UIControlEventTouchUpInside];

            [cell.contentView addSubview:QuestLabel];

            [cell.contentView addSubview:NoButton];


        }
        else

        {

            QuestLabel = (UILabel *)[cell.contentView viewWithTag:kQuestLabelTag];
            YesButton = (UIButton *)[cell.contentView viewWithTag:kYesButtonTag];
            NoButton = (UIButton *)[cell.contentView viewWithTag:kNoButtonTag];

        }


        QuestLabel.text = [buildingQuest objectAtIndex:indexPath.row];

        QuestLabel.numberOfLines = 0;

        NSString *titleString = [buildingQuest objectAtIndex:indexPath.row] ;
        CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
        CGRect rect = QuestLabel.frame;
        rect.size.height = titleSize.height+10;
        QuestLabel.frame = rect;

        return cell;

    }
Raj
  • 5,895
  • 4
  • 27
  • 48
  • Now on launching the tableView display well with varying height labels but after scrolling buttons get jumbled. Labels are steady only buttons getting jumbled now.... – Navnath Memane Apr 23 '13 at 14:05
  • You need to adjust the button as well – Raj Apr 23 '13 at 14:53
  • Hi again. I have already set NoButton.tag = kNoButtonTag. When I am setting [Nobutton setTag:indexPath.row]; for some action the button getting mixed up again. What should I do here? – Navnath Memane Apr 25 '13 at 12:21
  • you need the button to be placed just under the label ? – Raj Apr 26 '13 at 05:56
  • Yes. first label then two buttons (Yes)(No) below that label. – Navnath Memane Apr 26 '13 at 06:28
  • Solved:) Thanks for your follow up. I have follow this link....http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview – Navnath Memane Apr 29 '13 at 10:42
  • 1
    I am sorry I didnt get time to update the answer , any way happy to hear that you got the solution – Raj Apr 30 '13 at 07:39