单元格中的文本在旋转时会被截断! (编辑:单元格也滚动滚动) [英] Text in cell truncates on rotate! (edit: also cells screw up on scroll)

查看:86
本文介绍了单元格中的文本在旋转时会被截断! (编辑:单元格也滚动滚动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我有一个UITableViewCell,它在第一次出现时会自动换行,但是如果旋转到横向再回到纵向,文本将被截断!为什么会这样或该如何解决?

I have a UITableViewCell which word wraps the text when it first appears, but if you rotate to landscape and back to portrait again, the text has been truncated! Why is this or how can I fix it?

其他详细信息:

我的单元格具有UITableViewCellStyleSubtitle样式.为了自动换行文本(UITableViewCellStyleSubtitle的textLabel,而不是detailTextLabel),我计算了文本的高度,并通过

My cell is of style UITableViewCellStyleSubtitle. In order to wordwrap the text (the textLabel of UITableViewCellStyleSubtitle, not the detailTextLabel), I calculate how many lines high the text is, and set this via

 cell.textLabel.numberOfLines

当删除按钮滑入时,我不能简单地将numberOfLines设置为0,因为这看起来很丑.这在我的上一个问题中得到了解释:文本在删除幻灯片

I cant simply set numberOfLines to 0, cos when the delete button slides in, it looks ugly. This is explained in a previous question of mine: Text stretches outside cell boundaries on delete slide

我通过heightForRowAtIndexPath设置了标签的高度,但这可能不是问题,因为如果我将其硬编码为很大的高度,则在方向旋转后文本仍会截断.

I set the height of the label via heightForRowAtIndexPath, but this is probably not the problem, because if i hard code this to a massive height, the text will still truncate after the orientation rotation.

自编写此文档以来,我注意到当我尝试重用单元格时,我的reuseIdentifier字符串不相同.解决此问题可以改善这种情况,有时现在单元格还可以.但是,有时它们仍然会在旋转时截断,而且,如果我再次上下滚动(以便重新加载单元格),它们可以截断或更改其高度!

Since writing this I have noticed that my reuseIdentifier string wasn't the same when i tried to reuse the cell. Fixing this has improved the situation, and sometimes now the cells are fine. However, sometimes they still truncate on rotation, and also, if i scroll down and up again (so the cells are reload), they can truncate or change there height!

代码:

//This is a call back invoked by the interface when drawing the table view. This method will create a cell for each
//row and add text to each cell depending on the string retrieved from the datasource.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
CGFloat textLabelFontSize = 19;

if (cell==nil){
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellType"] autorelease];
    // set the labels to the appropriate text for this row
    cell.textLabel.text = [(Group*)[composeData objectInChosenGroupsListAtIndex:indexPath.row]groupName];
    cell.textLabel.font = [UIFont systemFontOfSize:textLabelFontSize];

    if ([(Group*)[composeData objectInChosenGroupsListAtIndex:indexPath.row]isDynamic]){
         cell.detailTextLabel.text = NSLocalizedString(@"dynamic", @"dynamic");
    }
    else {
        //get and set the group size
        int groupSize = [(Group*)[composeData objectInChosenGroupsListAtIndex:indexPath.row]groupSize];

        if (groupSize == 1)
            cell.detailTextLabel.text = NSLocalizedString(@"1Contact", @"1 contact");
        else
            cell.detailTextLabel.text = [NSString stringWithFormat:NSLocalizedString(@"%dContacts", @"%d contacts"), groupSize];
    }
}

//Calculate height (so we can hard code the number of lines, which is neccesary to avoid ugly stretching when the delete button slides in)
CGFloat width = [[self table] frame].size.width-cell.indentationWidth-50;


int section = indexPath.section;

NSString *title_string = cell.textLabel.text;
NSString *detail_string = cell.detailTextLabel.text;

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

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

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

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;

}

//hard code numberOfLines, to avoid ugly stretching when delete slides in
cell.textLabel.numberOfLines = title_height/[UIFont systemFontOfSize:textLabelFontSize].lineHeight;
//set height for retrieval later if neccesary
cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, height);

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[self tableView: tableView cellForRowAtIndexPath: indexPath];
return cell.frame.size.height;
}

推荐答案

目前,以下代码似乎可以在我的设备上运行,但不能在模拟器上运行.不过,大多数测试都需要这样做.关键修复在于单元的重用,可能还有[table reloadData]

At this moment in time, the following code appears to work on my device, but not on the simulator. Most testing needs doing though. The key fix is in the reuse of the cell, and probably [table reloadData]

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[table reloadData];
return [super shouldAutorotateToInterfaceOrientation:YES];
}

- (void)viewDidAppear:(BOOL)animated{
[table reloadData];
[super viewDidAppear:animated];
}

//This is a call back invoked by the interface when drawing the table view. This method will create a cell for each
//row and add text to each cell depending on the string retrieved from the datasource.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString * identifier = [NSString stringWithFormat:@"SwitchCell %d", indexPath.row]; // The cell row

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
CGFloat textLabelFontSize = 19;

if (cell==nil){
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
    // set the labels to the appropriate text for this row
    cell.textLabel.text = [(Group*)[composeData objectInChosenGroupsListAtIndex:indexPath.row]groupName];
    cell.textLabel.font = [UIFont systemFontOfSize:textLabelFontSize];

    if ([(Group*)[composeData objectInChosenGroupsListAtIndex:indexPath.row]isDynamic]){
         cell.detailTextLabel.text = NSLocalizedString(@"dynamic", @"dynamic");
    }
    else {
        //get and set the group size
        int groupSize = [(Group*)[composeData objectInChosenGroupsListAtIndex:indexPath.row]groupSize];

        if (groupSize == 1)
            cell.detailTextLabel.text = NSLocalizedString(@"1Contact", @"1 contact");
        else
            cell.detailTextLabel.text = [NSString stringWithFormat:NSLocalizedString(@"%dContacts", @"%d contacts"), groupSize];
    }
}


CGFloat width = [[self table] frame].size.width-cell.indentationWidth-50;

int section = indexPath.section;

NSString *title_string = cell.textLabel.text;
NSString *detail_string = cell.detailTextLabel.text;

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

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

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

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;

}

//hard code numberOfLines, to avoid ugly stretching when delete slides in
cell.textLabel.numberOfLines = title_height/[UIFont systemFontOfSize:textLabelFontSize].lineHeight;
//set height for retrieval later if neccesary
cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, height);

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[self tableView: tableView cellForRowAtIndexPath: indexPath];
return cell.frame.size.height;
}

这篇关于单元格中的文本在旋转时会被截断! (编辑:单元格也滚动滚动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆