2013-09-24 30 views
0

我有兩個UIButtons和一些表格視圖單元格內的標籤。我有2個部分,第一部分包含一個表格視圖單元格,在該單元格內部有兩個UIButtons,它們調用api以獲取第2部分中單元格的新數據。在某些情況下,創建標籤是因爲字符串很長,所以我必須爲單元格創建一個新的框架。在tableviewcell中的UIButtons和UILabels

我目前遇到的問題是,按鈕文本也得到更新與新的信息,有時字符串很短或有時字符串很長,所以我必須刪除按鈕,並添加一個新的按鈕適合新字符串的新框架。但是如果過去的按鈕字符串比新的按鈕字符串短,該按鈕仍然會出現在背景中。我試圖從超級視圖中刪除它,並檢查它已從超級視圖中刪除(檢查它是否爲空,一旦我刪除它),它是,但它仍然出現在屏幕上。 UILabels也會發生同樣的情況。我也試圖用標籤(viewWithTag:123)從超級視圖中刪除它們。任何人都有任何指點,想法,建議,任何事情都可以幫助我找出/解決這個問題嗎?

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

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
     UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero]; 
     selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1]; 
     cell.selectedBackgroundView = selectedBackgroundView; 

     if(cell==nil) { NSLog(@"Cell is still nil");} 
    } 
    cell.textLabel.text = nil; 
    cell.accessoryView = nil; 
    cell.detailTextLabel.text = nil; 
    cell.userInteractionEnabled = YES; 

    if(indexPath.section == 0) 
    { 

      dosageButton       = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
      amountButton       = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

      [dosageButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; 
      [dosageButton setContentEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)]; 
      [amountButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; 
      [amountButton setContentEdgeInsets:UIEdgeInsetsMake(0, 15.5, 0, 0)]; 


      if(strengths.count == 0) { [dosageButton setTitle:@"Dosage" forState:UIControlStateNormal];} 
      else 
      { 

       [dosageButton setTitle:selectedStrength forState:UIControlStateNormal]; 
       CGSize stringsize = [selectedStrength sizeWithFont:[UIFont systemFontOfSize:14]]; 
       [dosageButton setFrame:CGRectMake(5, 27.5, stringsize.width + 30, 30)]; 
      } 

      if(quantity.count == 0) { [amountButton setTitle:@"Amount" forState:UIControlStateNormal];} 
      else 
      { 

       int value = [selectedQuantity intValue]; 
       if (value == 1) 
       { 
        NSString *amountAndFormat = [NSString stringWithFormat:@"%@ %@", selectedQuantity, selectedFormat]; 
        [amountButton setTitle:amountAndFormat forState:UIControlStateNormal]; 
        CGSize stringsize = [selectedStrength sizeWithFont:[UIFont systemFontOfSize:14]]; 
        CGSize amountSize = [amountAndFormat sizeWithFont:[UIFont systemFontOfSize:14]]; 

        if(amountSize.width+stringsize.width > 250) 
        { 
         dosageButton.frame      = CGRectMake(5, 27.5, 130, 30); 
         amountButton.frame      = CGRectMake(140, 27.5, 160, 30); 
        } 
        else 
        { 
         [amountButton setFrame:CGRectMake(stringsize.width + 40, 27.5, amountSize.width + (amountSize.height * 2.1), 30)]; 
        } 
       } 
       else 
       { 
        NSString *amountAndFormat = [NSString stringWithFormat:@"%@ %@", selectedQuantity, selectedFormatPlural]; 
        [amountButton setTitle:amountAndFormat forState:UIControlStateNormal]; 
        CGSize stringsize = [selectedStrength sizeWithFont:[UIFont systemFontOfSize:14]]; 
        CGSize amountSize = [amountAndFormat sizeWithFont:[UIFont systemFontOfSize:14]]; 
        if(amountSize.width + stringsize.width> 250) 
        { 
         dosageButton.frame      = CGRectMake(5, 27.5, 130, 30); 
         amountButton.frame      = CGRectMake(140, 27.5, 160, 30); 
        } 
        else 
        { 
         [amountButton setFrame:CGRectMake(stringsize.width + 40, 27.5, amountSize.width + (amountSize.height * 2.1), 30)]; 
        } 
       } 

      } 

      [dosageButton addTarget:self action:@selector(showDosages:) forControlEvents:UIControlEventTouchUpInside]; 
      [amountButton addTarget:self action:@selector(showAmount:) forControlEvents:UIControlEventTouchUpInside]; 

      //[cell.contentView addSubview:dosageButton]; 
      //[cell.contentView addSubview:amountButton]; 
      //adding it to view instead of cell because of userInteraction. 
      //if I add it to cell.contentView and userInteractionEnabled = NO 
      //then the user can't press the button 
      //but if the it is enabled then they can click the cell and looks tacky! 

      [self.view addSubview:dosageButton]; 
      [self.view addSubview:amountButton]; 
      cell.userInteractionEnabled = NO; 
    } 

    if(indexPath.section == 1) 
     { 
      if(localName.count == 0) 
      { 
       //Nothing 
      } 

      else 
      { 
      CGRect longStringFrame = CGRectMake(12, 4, 250, 120); 
      //UILabel *longStringLabel = [[UILabel alloc] initWithFrame:longStringFrame]; 
      UILabel *longStringLabel = [[UILabel alloc] initWithFrame:longStringFrame]; 
      longStringLabel.backgroundColor = [UIColor clearColor]; 
      longStringLabel.font = [UIFont systemFontOfSize:12]; 
      longStringLabel.text = [NSString stringWithFormat:@"Something super duper long. Some really really long string that has a lot of information and cannot fit in one line so I have to use numberOfLines = 0 and NSLineBreakByWordWrapping. This string is really really really long.]]; 
      longStringLabel.textColor = [UIColor blackColor]; 
      longStringLabel.numberOfLines = 0; 
      longStringLabel.lineBreakMode = NSLineBreakByWordWrapping; 
      cell.textLabel.text = @""; 
      longStringLabel.tag = 123; 
      [cell.contentView addSubview:longStringLabel]; 
      } 
     } 


} 



- (void) showDosages:(UIButton *)sender 
{ 
    [dosageButton removeFromSuperview]; 
    [amountButton removeFromSuperview]; 
    [[cell.contentView viewWithTag:123] removeFromSuperview]; 
    NSLog(@"%@",[dosageButton superview]); 
    NSLog(@"%@",[amountButton superview]); 


    //Also tried it this way as well. Didn't work 
    //[dosageButton performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO]; 
    //[amountButton performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO]; 

    dispatch_async(someDispatch, ^{ NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat: @"http://somewebsite.com"]]]; [self performSelectorOnMainThread:@selector(fetchData:) withObject:data waitUntilDone:YES]; }); 
} 

showAmount類似於showDosages

+0

請添加代碼以供參考您的按鈕會自動調整..... – NANNAV

+0

PLZ放你的代碼以及你的tableview的圖像 –

+0

@NANNAV編輯了OP,並添加了代碼 – Chris

回答

1

我建議你創建把所有的標籤,你想&按鍵的自定義單元格,舉一個iboutles,讓您可以輕鬆管理。

自定義單元格教程 http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/

一旦你做你的,你可以根據自己的字符串來設置單元格的高度。當你的電池的尺寸增加或減少

+0

與此問題是,我正在使用單元格以顯示cell.textLabel.text以及label.texts以及。這不會在每個單元上創建按鈕嗎?我以前曾使用過自定義單元格,但從未將按鈕放在裏面,所以我不知道每個單元格是否會因爲if(cell == nil){//在這裏創建自定義單元格}而創建按鈕' – Chris

+0

@Chris - 你已經把按鈕放在所有單元格內。當你回收一個單元格時,以前的任何按鈕都會在那裏。如果您只想要某些單元格中的按鈕,請使用單元格標識符功能將它們分爲兩類。 –

+0

@HotLicks我是誰?怎麼會這樣?我以爲我只是將它添加到第一部分,即部分== 1,並在那裏創建它。我如何將它們分成兩類? – Chris

0

這裏是多個小區標識的簡單的例子,希望它可以幫助

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

static NSString *CellIdentifier1 = @"Cell1"; 
static NSString *CellIdentifier2 = @"Cell2"; 

if(flag == 0) { 

    MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 

    if (cell == nil) { 
     cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease]; 
    } 

    cell.textLabel.text= @"Cell1"; 

    return cell; 
} 
else { 
    NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 

    if (cell == nil) { 
     cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2 orientation:currentOrientation] autorelease]; 

    } 

    cell.textLabel.text= @"Cell1"; 

    return cell; 
} 

return nil; 
} 
+0

謝謝!我將它與自定義單元格一起使用,並在單元格內創建按鈕,然後返回它們。 – Chris