UITableViewCell textLabel宽度不变

时间:2012-08-23 23:00:11

标签: iphone ios uitableview

我正在努力使我的UITableViewCell文本标签中的文本从单元格的右侧包裹大约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);
}

1 个答案:

答案 0 :(得分: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