Iphone 单元格中的文本在旋转时截断!(编辑:单元格也会在滚动中扭曲)

Iphone 单元格中的文本在旋转时截断!(编辑:单元格也会在滚动中扭曲),iphone,cocoa-touch,uitableview,orientation,Iphone,Cocoa Touch,Uitableview,Orientation,问题: //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

问题:

//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;
}
我有一个UITableViewCell,word会在文本首次出现时将其包装起来,但如果您再次旋转到横向和纵向,则文本已被截断!为什么会这样,或者我如何修复它

额外细节:

//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;
}
我的手机是UITableViewCellStyleSubtitle风格。为了对文本进行文字包装(UITableViewCellStyleSubtitle的textLabel,而不是detailTextLabel),我计算文本的行数,并通过

 cell.textLabel.numberOfLines
我不能简单地将numberOfLines设置为0,因为当删除按钮滑入时,它看起来很难看。这在我前面的一个问题中得到了解释:

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

编辑:

//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;
}
写这篇文章后,我注意到当我尝试重用单元格时,我的reuseIdentifier字符串不一样。解决这个问题已经改善了情况,有时现在细胞已经好了。然而,有时它们仍然会在旋转时截断,而且,如果我再次向下和向上滚动(这样单元格就会重新加载),它们可以截断或更改高度

代码:

//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;
}
这将有助于:

-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

}

-(void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}  
现在实现以下方法:

-(void)checkOrientation
{
UIInterfaceOrientation orientation = self.interfaceOrientation;
if (orientation == UIDeviceOrientationLandscapeLeft||orientation==UIDeviceOrientationLandscapeRight)
{
    [tblView reloadData];  
    // Set numberOfLines = some value you think might adjust the text.
    // Set x coorinate of views if you want to change

}
else
{
    [tblView reloadData];
    // Set numberOfLines = some value you think might adjust the text.
    // Set x coordinates of views to initial x xoordinates.

}

}  
创建接收数据状态:

- (void)receivedRotate:(NSNotification *)notification
{    
[self checkOrientation];
}  
-(void)viewDidLoad
{  
[self checkOrientation];
}
视图中加载

- (void)receivedRotate:(NSNotification *)notification
{    
[self checkOrientation];
}  
-(void)viewDidLoad
{  
[self checkOrientation];
}

此时此刻,以下代码似乎在我的设备上工作,但在模拟器上不工作。不过,大多数测试都需要这样做。关键在于单元的重用,可能还有[表重载数据]

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

如果需要,您可以发送方向更改通知,并根据方向模式更改宽度。我已编辑我的问题,以显示我已将问题修复了一半,但尚未完全修复。我的修正意味着问题更多的是当单元格从视图中消失和重新出现时,它们的重用。但是,滚动问题和方向问题是相同的还是不同的,我还不知道。当你的屏幕第一次出现时,你的文本似乎适合单元格。正当如果是这样的话,你肯定有定位问题。就屏幕定位而言,我认为这个问题现在可能已经解决了。我想我现在的问题是,如果我做一些滚动。事实上,这很可能是一个单独的问题,我需要为(?)研究或提出一个新问题。如果是这样,我会在堆栈溢出允许的情况下自己正确回答这个问题。啊哈!我已经计算出什么时候出了问题。(请记住这一点,现在我已经使用单元重用部分修复了这个问题)。我有3个单元处于纵向模式,这很好。然后我进入横向模式,第三单元就看不见了。如果我将单元格向上滚动到可见位置,然后旋转回纵向模式,文本将被截断。单元格的宽度和高度看起来很好。谢谢,但要么我不知道我需要在checkOrientation里面放什么,要么我们有点误解。请记住,我认为问题不在于单元格或文本标签的物理大小(宽度或高度)。相反,我认为问题在于标签的numberOfLines值。我不确定是否能够访问代码方法中的cell.textLabel.numberOfLines。如果您查看我的代码,这将在CellForRowatineXpath中计算。然而,我认为我的问题是,当我更改回纵向时,似乎设置了横向numberOfLines。只需将numberOfLines=2或您认为在极端情况下会调整文本的最大值进行修正。在“检查方向”中,您可以更改引起干扰的表格或视图的大小。您需要在这两种情况下重新加载数据。别忘了,尼蒂什!我不敢相信这是多么简单!将numberOfLines更改为一个大的最大值非常有效我很快会再测试一次,如果它好的话,我会给你一个巨大的绿色扁虱!(或者,如果你愿意,也可以发布一个新的答案,因为当前的答案是不正确的)!不,我太激动了,太快了!当我滑入“删除”按钮时,这会导致文本从单元格中展开。这是我以前遇到的一个问题,我通过计算正确的行数解决了这个问题。因此,我仍然认为我需要确保纵向和横向计算正确的行数。我在Android和IPhone上开发应用程序。我确实注意到一件事。在Android模拟器上显示的内容与在Android设备上显示的内容完全相似。然而,在IPhone的情况下,情况并非如此。这就是为什么对于IPhone,建议在模拟器和设备上运行应用程序的原因。你不能肯定。