2013-04-22 60 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

相關問題