2012-01-13 115 views
5

我在UITableViewCell中有三個自定義的UILabel。像這樣的單元設計,在iPhone中動態更改UITableviewCell高度?

Date(12.1.2012) Time(12.00pm) 
Cost: $20.00 
Details 

我已經在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;中指定了行高100。但是,現在細節有時會從服務器返回空(空)。那時我需要從單元格中移除細節標籤並將特定的單元格(行)高度更改爲70.我該怎麼做?我在Google中最好地搜索了我的級別。但是,我仍然只是困惑這樣做。你能幫我麼?謝謝。我從這個鏈接中得到了一些想法 - http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/。他們只使用一個標籤來調整行高。請幫幫我。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 90; 
} 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        static NSString *CellIdentifier = @"Cell"; 

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


            UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)]; 
            dateLabel.tag = 100; 
            dateLabel.backgroundColor = [UIColor clearColor]; 
            dateLabel.font = [UIFont fontWithName:@"Helvetica" size:16]; 
            dateLabel.font = [UIFont boldSystemFontOfSize:16]; 
            dateLabel.highlightedTextColor = [UIColor whiteColor]; 
            [cell.contentView addSubview: dateLabel]; 

      UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 200, 25)]; 
            timeLabel.tag = 101; 
            timeLabel.backgroundColor = [UIColor clearColor]; 
            timeLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
            timeLabel.highlightedTextColor = [UIColor whiteColor]; 
            [cell.contentView addSubview: timeLabel]; 

      UILabel *costLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 25)]; 
            costLabel.tag = 102; 
            costLabel.backgroundColor = [UIColor clearColor]; 
            costLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
            costLabel.font = [UIFont boldSystemFontOfSize:14]; 
            costLabel.highlightedTextColor = [UIColor whiteColor]; 
            [cell.contentView addSubview: costLabel]; 

            UILabel *eventLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 300, 25)]; 
            eventLabel.tag = 103; 
            eventLabel.backgroundColor = [UIColor clearColor]; 
            eventLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
            eventLabel.font = [UIFont boldSystemFontOfSize:14]; 
            eventLabel.highlightedTextColor = [UIColor whiteColor]; 
            [cell.contentView addSubview: eventLabel]; 
     } 

        UILabel *dateLabel = (UILabel *) [cell.contentView viewWithTag:100]; 
        dateLabel.text = [DateArray objectAtIndex:indexPath.row]; 

     UILabel * timeLabel = (UILabel *) [cell.contentView viewWithTag:101]; 
        timeLabel.text = [timeArray objectAtIndex:indexPath.row]; 

     UILabel * costLabel = (UILabel *) [cell.contentView viewWithTag:102]; 
        costLabel.text = [costArray objectAtIndex:indexPath.row]; 

        UILabel *eventLabel = (UILabel *) [cell.contentView viewWithTag:103]; 
     NSString *description = [eventArray objectAtIndex:indexPath.row]; 
     if([description isEqualToString:@""]) 
     { 
     eventLabel.text = @""; // Here i don't want to show this label and resize(reduce) the row height to 60; 
     } 
     else 
     { 
        eventLabel.text = description; 
     } 
        return cell; 
    } 

我該怎麼做?謝謝。

回答

8

在返回高度時,檢查詳細描述是否爲空。如果是回到60

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *description = [eventArray objectAtIndex:indexPath.row]; 
if([description isEqualToString:@""]) 
    return 60 
else 
{ 
    return 90; 
} 
} 

另外,你需要if語句@"Details"移除的標籤在同在cellForRowAtIndexPath:。預先標記,從子視圖中標識並從單元格中移除它。

+0

Mr.MadhavanRP感謝您的回答。我已經在我的代碼中嘗試了您的答案。我檢查了cellForRowAtIndexPath中的條件:也。第一次它工作正常。當我滾動tableview時,單元格大小已更改,說明標籤已更改位置。這意味着說明標籤已刪除所有行。你能幫我麼。謝謝 – Gopinath 2012-01-13 07:17:37

+0

@Gopinath爲什麼不嘗試添加細節標籤作爲單元格的子視圖,只有當你有一些描述。您可能需要創建一個自定義類來解決滾動問題。參考這個問題的答案http://stackoverflow.com/questions/8352530/changing-the-position-of-custom-uibutton-in-custom-uitableviewcell – MadhavanRP 2012-01-13 08:48:50

0

您可以通過使用CGSize這樣的檢查返回字符串的大小 -

CGSize size = [detailStr sizeWithFont:[UIFont boldSystemFontOfSize:12.0f] constrainedToSize:CGSizeMake(190, 40) lineBreakMode:UILineBreakModeWordWrap]; 

你可以根據你的病情改變寬度,高度和字體。在計算大小之前,請檢查detailStr是否不等於零。

+0

Mr.Sasdnib謝謝你的回答。我編輯了我的問題。請檢查這個。我只是困惑你的答案,你可以幫我。謝謝。 – Gopinath 2012-01-13 05:51:37