问题:
我有一个UITableViewCell,当第一次出现时,该文字将文字换行,但是如果你再次旋转到横向并再回到肖像,文本就会被截断!为什么这样或如何解决?
额外明细:
我的手机是UITableViewCellStyleSubtitle风格。为了对文本进行自动换行(UITableViewCellStyleSubtitle的textLabel,而不是detailTextLabel),我计算文本的高度,并通过
设置。 cell.textLabel.numberOfLines
我不能简单地将numberOfLines设置为0,当删除按钮滑入时,它看起来很丑。我之前的一个问题解释了这一点:Text stretches outside cell boundaries on delete slide
我通过heightForRowAtIndexPath设置标签的高度,但这可能不是问题,因为如果我将其硬编码到一个很大的高度,文本仍然会在方向旋转后截断。
修改
自写这篇文章以来,我注意到当我尝试重用单元格时,我的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;
}
答案 0 :(得分:0)
这将有效:
-(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.
}
}
创建 recievedRotate :
- (void)receivedRotate:(NSNotification *)notification
{
[self checkOrientation];
}
在 viewDidLoad :
中-(void)viewDidLoad
{
[self checkOrientation];
}
答案 1 :(得分:0)
此时,以下代码似乎可以在我的设备上运行,但不能在模拟器上运行。大多数测试都需要这样做。关键的解决方法是重用单元格,可能是[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;
}