2012-01-13 7 views
5

私はUITableViewCellに3つのカスタムUILabelを持っています。このようなセルデザインは、に行の高さ100を指定しました。しかし、今では、サーバから空白(Null)を返すことがあります。その時私はセルから詳細ラベルを削除し、特定のセル(行)の高さを70に変更する必要があります。これを行うにはどうすればいいですか? Googleで自分のレベルを最高に探しました。しかし、私はまだこれをやって混乱しています。手伝ってくれませんか?ありがとう。私はこのリンクからいくつか考えました - http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/。行の高さを変更するには、ラベルを1つしか使用しませんでした。私を助けてください。iPhoneでUITableviewCellの高さを動的に変更できますか?

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

ある場合は、cellForRowAtIndexPath:にif文と同じで@"Details"でラベルを削除する必要があります。事前にタグ付けし、サブビューから識別してセルから削除します。

+0

Mr.MadhavanRPあなたの答えに感謝します。あなたの答えを自分のコードで試しました。私はcellForRowAtIndexPathの条件をチェックしました:また。初めて正常に動作しています。私は、テーブルビューをスクロールすると、セルのサイズが変更され、説明ラベルが場所を変更しました。これは、説明ラベルにすべての行の削除があることを意味します。手伝ってくれませんか。ありがとう – Gopinath

+0

@Gopinathなぜあなたはいくつかの説明がある場合にのみ、セルのサブビューとして詳細ラベルを追加しようとしないでください。スクロールの問題を解決するためにカスタムクラスを作成する必要があるかもしれません。この質問の回答を参照してください。http://stackoverflow.com/questions/8352530/changing-the-position-of-custom-uibutton-in-custom-uitableviewcell – MadhavanRP

0

あなたはこのようCGSizeを使用して返される文字列のサイズを確認することができます - あなたはあなたの条件に応じて、幅、高さ、およびフォントを変更することができます

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

。また、sizeを計算する前にdetailStrがnilと等しくないかどうかを確認してください。

+0

Mr.Sasdnibはあなたの答えに感謝します。私は私の質問を編集しました。これを確認してください。私はちょうどあなたが私を助けてくださいできる答えを混乱させる。ありがとう。 – Gopinath

関連する問題