我试图做到这一点,以使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);
}

最佳答案

单元格的layoutSubviews方法将覆盖您在cellForRow(或大多数其他位置)中尝试执行的所有操作。最好的解决方案是子类化UITableViewCell并重写layoutSubviews。确保您调用 super ,然后自定义您的文本标签框架。

编辑:

只需在您的实现和使用之上包括(当然进行修改)

    CustomTableViewCell *cell = [tableView dequeReusableCell...


    cell = [[CustomTableViewCell alloc] initWithStyle...

代替。无需额外的.xib。
    @interface CustomTableViewCell : UITableViewCell
    @end

    @implementation CustomTableViewCell
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        cell.textLabel.frame = theFrameYouWant;
    }
    @end

关于iphone - UITableViewCell textLabel宽度不变,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12101170/

10-11 14:38