0

在我的应用程序中,我有自定义的 tableView 单元格,其中 UILabels 具有不同的高度和两个按钮。我已经尝试了多种解决方案,但仍然没有运气。你能否建议我在下面的代码中哪里错了......

- (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;
}

更新1:现在我的标签按照 Rajesh 的回答正常工作。但是设置按钮标签是在单元格中混合我的按钮。

[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;
 }

能否请你帮忙。

4

1 回答 1

0

我已经编辑了代码,请检查下面的代码。

而不是[QuestLabel sizeToFit];我动态设置标签高度

- (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;

    }
于 2013-04-23T10:24:44.117 回答