web-dev-qa-db-ja.com

UITableViewCellで整列するラベル

私が使う UITableView を使用して作成されたセルを使用 UITableViewCellStyleSubtitle。すべてのセルの高さは、

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

デリゲートメソッド。

あなたが写真で見ることができる問題

((注:画像が更新されました

http://img.skitch.com/20090715-g7srxgee2d7fhi5rab2wufrdgm.png

TextLabelとdetailTextLabelのセルの上部への配置を設定するにはどうすればよいですか? (UITableViewCellをサブクラス化し、layoutSubviewsをオーバーライドすることによってそれを行うつもりはありません)

ありがとう

20
Igor

これはセルをサブクラス化しない別のソリューションであるため、さまざまなテーブルスタイルで確実に機能します。 (他の解決策は確認していません。)タイトルと詳細の文字列はUITableViewControllerヘッダーで宣言されており、すでに定義されています。それらはそれほど長くはなく、確かに高さ4000以内です!

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    CGRect frame = [UIScreen mainScreen].bounds;
    CGFloat width = frame.size.width;

    int section = indexPath.section;
    int row = indexPath.row;

    NSString *title_string = [title_strings_array objectAtIndex:row];
    NSString *detail_string = [detail_strings_array objectAtIndex:row];

    CGSize title_size = {0, 0};
    CGSize detail_size = {0, 0};

    if (title_string && [title_string isEqualToString:@""] == NO ) {
        title_size = [title_string sizeWithFont:[UIFont systemFontOfSize:22.0]
                              constrainedToSize:CGSizeMake(width, 4000)
                                  lineBreakMode:UILineBreakModeWordWrap];
    }

    if (detail_string && [title_string isEqualToString:@""] == NO ) {
        detail_size = [detail_string sizeWithFont:[UIFont systemFontOfSize:18.0]
                                constrainedToSize:CGSizeMake(width, 4000)
                                    lineBreakMode:UILineBreakModeWordWrap];
    }

    CGFloat title_height = title_size.height;
    CGFloat detail_height = detail_size.height;

    CGFloat content_size = title_height + detail_height;

    CGFloat height;

    switch ( section ) {

        case 0:
            height = content_size;
            break;

        //Just in case  
        default:
            height = 44.0;
            break;

    }

    return height;

}
8
SK9

さて、これは午後の費用がかかりましたが、私はそれを理解したと思います。私の知る限り、これはUITableViewCellがtextLabelとdetailTextLabelをレイアウトする方法のバグのようです。行の高さを設定すると、2つのラベルに同じ高さが割り当てられるように見えます。つまり、detailTextLabelにさらに多くのスペースが必要な場合でも、上記の動作を正確に得ることができます。これが問題を解決するために私がした2つのことです。それを修正するためにUITableViewCellをサブクラス化する必要がありましたが、それは最小限のコードです。

まず、各行の高さを正しく計算していることを確認してください。このメソッドをテーブルビューデリゲートに配置します。フォントメソッドを独自のものに置き換えます。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString *cellDetailText = [[self itemForIndexPath: indexPath] detailDescription];
   NSString *cellText = [[self itemForIndexPath: indexPath] description];
   // The width subtracted from the tableView frame depends on:
   // 40.0 for detail accessory
   // Width of icon image
   // Editing width
   // I don't think you can count on the cell being properly laid out here, so you've
   // got to hard code it based on the state of the table.
   CGSize constraintSize = CGSizeMake(tableView.frame.size.width - 40.0 - 50.0, CGFLOAT_MAX);
   CGSize labelSize = [cellText sizeWithFont: [self cellTextLabelFont] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
   CGSize detailSize = [cellDetailText sizeWithFont: [self cellDetailTextLabelFont] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

   CGFloat result = MAX(44.0, labelSize.height + detailSize.height + 12.0); 
   return result;
}

次に、UITableViewCellをサブクラス化し、layoutSubviewsをオーバーライドします。

#import "UITableViewCellFixed.h"


@implementation UITableViewCellFixed
- (void) layoutSubviews {
   [super layoutSubviews];
   self.textLabel.frame = CGRectMake(self.textLabel.frame.Origin.x, 
                                      4.0, 
                                      self.textLabel.frame.size.width, 
                                      self.textLabel.frame.size.height);
   self.detailTextLabel.frame = CGRectMake(self.detailTextLabel.frame.Origin.x, 
                                           8.0 + self.textLabel.frame.size.height, 
                                           self.detailTextLabel.frame.size.width, 
                                           self.detailTextLabel.frame.size.height);
}
@end
36
Chris Garrett

ただし、セルのサイズを設定する場合は、NSStringのさまざまなサイズ設定方法を使用して行う必要があります。このようにして、セルを作成し、空白を回避するための高さを正確に決定できます。

1
Jeff Kelley