2

UITableViewCell の textlabel のテキストがセルの右側から約 30 ピクセルでラップされるようにしようとしています。ただし、右から約 5 または 10 ピクセルまで右に移動します。cellForRowAtIndex で設定したすべての値を編集しようとしましたが、変更されるのはセルの高さだけで、セルの textLabel の大きさではありません。であり、私は障害を見つけることができず、おそらくここの誰かがエラーを見ることができることを願っています.

私はさらにテストを行ってきましたが、使用するsectionIndexTitlesForTableViewと、UItableViewCell textLabel は以下のコードで概説されている正しいサイズに縮小されることがわかりました..ただし、このメソッドを無効にすると、次のコードで幅を設定します。

これは次のような私のコードです。

//These Constants are used for dynamic cell height
#define FONT_SIZE 18.0f
#define CELL_CONTENT_WIDTH 290.0f
#define CELL_CONTENT_MARGIN 10.0f



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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // Configure the cell using custom cell

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

        [cell.textLabel setLineBreakMode:UILineBreakModeWordWrap];
        [cell.textLabel setMinimumFontSize:FONT_SIZE];
        [cell.textLabel setNumberOfLines:0];
        [cell.textLabel setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]];
        [cell.textLabel setTag:1];

        [[cell contentView] addSubview:cell.textLabel];
    }

    //Customize cell here
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection

    //Replaces previously selected cells accessory view (tick)
    if ([indexPath isEqual:oldCheckedIndexPath]){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    //Display cells with data that has been sorted from startSortingTheArray
    NSMutableArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]];

    NSString *key = [keys objectAtIndex:indexPath.row];

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];


    //Applise current key value to cell text label
    [cell.textLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 25.0f))];

    [cell.textLabel setText:key];

    return cell;
}



//Cell size for word wrapping So the user can see all of the details of a submodel etc.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    //Display cells with data that has been sorted from startSortingTheArray
    NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]];
    NSString *key = [keys objectAtIndex:indexPath.row];

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    CGFloat height = MAX(size.height, 25.0f);

    return height + (CELL_CONTENT_MARGIN * 2);
}
4

1 に答える 1

10

セルの layoutSubviews メソッドは、cellForRow (または他のほとんどの場所) で実行しようとするすべてをオーバーライドします。最善の解決策は、UITableViewCell をサブクラス化し、layoutSubviews をオーバーライドすることです。必ず super を呼び出してから、テキスト ラベル フレームをカスタマイズしてください。

編集:

実装の上にこれを(もちろん変更して)含めるだけで使用できます

    CustomTableViewCell *cell = [tableView dequeReusableCell...

    cell = [[CustomTableViewCell alloc] initWithStyle...

代わりは。余分な .xibs は必要ありません。

    @interface CustomTableViewCell : UITableViewCell
    @end

    @implementation CustomTableViewCell
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        cell.textLabel.frame = theFrameYouWant;
    }
    @end
于 2012-08-23T23:59:43.680 に答える