【问题标题】:Text stretches outside cell boundaries on delete slide删除幻灯片上的文本超出单元格边界
【发布时间】:2011-06-21 14:25:28
【问题描述】:

UITableViewCellStyleSubtitle 的 tableView 单元格会截断文本。 此外,如果使用删除幻灯片,多行的常规 UITableCell 将截断文本。

但是,当删除幻灯片进入时,我的多行 UITableViewCellStyleSubtitle 单元格不会截断文本。相反,它会延伸到单元格的边界之外。我可以解决这个问题吗?这是我的一些代码...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

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

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.lineBreakMode=UILineBreakModeTailTruncation;
    cell.textLabel.numberOfLines = 0;

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

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = (UITableViewCell *)[self tableView: tableView cellForRowAtIndexPath: indexPath];

CGRect frame = [UIScreen mainScreen].bounds;
CGFloat width = frame.size.width;

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:22.0]
                          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;

}

return height;

}

【问题讨论】:

  • 您要部署到哪个版本?模拟器还是设备?

标签: iphone cocoa-touch uitableview


【解决方案1】:

尝试设置标签大小,即最大宽度,如下所示:

cell.textLabel.frame.size.width = someWidth;

cell.detailTextLabel.frame.size.width = someWidth;

【讨论】:

  • 这会在屏幕加载时截断文本,但不会在删除按钮滑入时阻止文本在单元格边框上方和下方拉伸。
  • 你能不能发个截图,我不明白你的意思,也许你也应该改变你的身高,如果我理解正确的话
  • 我现在已经解决了这个问题,谢谢。 (我会在堆栈溢出允许我的几个小时内勾选我的正确答案。)但是,我有一个新的相关问题,我已经为 stackoverflow.com/questions/6451405/… 提出了一个新问题
【解决方案2】:

这很难解决!问题是如果 numberOfLines 设置为 0,它总是会拉伸文本。因此,我需要计算 numberOfLines,并为每个单元格设置它。

cell.textLabel.numberOfLines = textLabelheight/textLabelFontSize;

【讨论】:

    猜你喜欢
    • 2023-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多