2013-04-22 59 views
0

在我的应用程序中,我有自定义的tableView单元格,它具有不同高度和两个按钮的UILabels。我尝试了一些解决方案,但仍然没有运气。能否请你建议我在哪里错在下面的代码....自定义TableView单元格在滚动之前和之后混合iPhone

- (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:现在我的标签是由以下拉杰什答案正常工作。但设置按钮标签正在混合我的按钮在单元格内。

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

请你帮忙。

回答

0

我编辑了代码,pelease检查下面的代码。

而不是[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; 

    } 
+0

设置标签的高度现在在启动的tableView显示以及高矮不一标签,但滚动按钮后获得混乱。标签是稳定只有按钮越来越混乱.... – 2013-04-23 14:05:04

+0

你需要调整按钮以及 – Raj 2013-04-23 14:53:07

+0

gr8。它工作正常。非常感谢Rajesh。 – 2013-04-24 13:06:43

相关问题