简体   繁体   中英

UITableViewCell textLabel Width not changing

I am trying to make it so that the text in my textlabel of my UITableViewCell wraps about 30 pixels from the right hand side of the cell. However its going right up to about 5 or 10 pixels from the right.. I have tried editing all of the values I have set in cellForRowAtIndex however the only thing that changes is the height of the cell but not how big the textLabel of the cell is, and I am unable to find the fault and am hoping maybe someone here can see the error.

I have been doing further testing and what I have found is that if I use sectionIndexTitlesForTableView then the UItableViewCell textLabel will reduce to the correct size outlined in the code below.. however if I disable this method then it dose not pay any attention to me trying to set the width in the following code.

This is my code as follows.

//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);
}

The cell's layoutSubviews method will override anything you try to do in cellForRow (or most anywhere else). The best solution would be to subclass UITableViewCell and override layoutSubviews. Make sure you call super, and then customize your text labels frame.

EDIT:

Just include this (modified of course) above your implementation and use

    CustomTableViewCell *cell = [tableView dequeReusableCell...

and

    cell = [[CustomTableViewCell alloc] initWithStyle...

instead. No extra .xibs needed.

    @interface CustomTableViewCell : UITableViewCell
    @end

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM