tableview 中的文本标签太长会影响正确的细节 (detailTextLabel) 被覆盖或不显示 [英] A text label in the tableview is too long which affects the right detail (detailTextLabel) to be covered or not shown

查看:29
本文介绍了tableview 中的文本标签太长会影响正确的细节 (detailTextLabel) 被覆盖或不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为该单元格设置了一个文本,但是,它显示的文本太长,影响了正确的细节文本被覆盖或不显示.

I have set a text for that cell but however, the text that it is shown is too long which affects the right detail text to be covered or not shown.

我无法更改它,因为我需要在下一个视图控制器中使用该名称.是否可以让它只显示文本,后跟...."?

I can't change it as I need the name in the next viewcontroller. Is it possible to enable it to just display the text, followed by "...."?

示例:

电气&电子工程.... 01 >

Electrical & Electronic Engi.... 01 >

图例:Electrical & Electronic Engi...."作为显示在tableview中的文本,01"作为右侧的detailTextLabel,>"作为导航.

LEGEND: "Electrical & Electronic Engi...." as text displayed in the tableview, "01" as the detailTextLabel on the right and the ">" as the navigation.

它应该是这样的,http://oi58.tinypic.com/2j4vg5k.jpg,但由于某些文字太长,出现如下:http://oi58.tinypic.com/erc177.jpg

This is how it should look like, http://oi58.tinypic.com/2j4vg5k.jpg, but due to some text is too long, this is what appears: http://oi58.tinypic.com/erc177.jpg

textLabel 和 detailTextLabel 似乎不适合或显示在整行中.我希望正确的 detailTextLabel 仍然存在,textLabel 以...."结尾

The textLabel and detailTextLabel doesn't seem to fit or show in the whole row. I would like the right detailTextLabel to be still there will the textLabel to end with a "...."

谢谢.

(我是 iOS 编程新手)

(I'm new to iOS Programming)

推荐答案

为此,您必须限制 UITableViewCell 的默认 textLabel 的宽度或向单元格添加新的 UILabel.

For this you will have to restrict the width of default textLabel of UITableViewCell or add new UILabel to cell.

你有两个选择

1)不要使用单元格的默认文本标签,创建新的UILabel并将其添加为tableview单元格的子视图.

1)Dont use default textLabel of cell, create new UILabel and add it as a subview of tableview cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // Configure the cell using custom cell

    //restrict width here while creating label (change 40 to what you want)
    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,40,20)];

   tempLabel.text=@"The text you want to assign";
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];



        [[cell contentView] addSubview:tempLabel];
    }

    return cell;
}

2) 或者第二种方法是更改​​默认 textLabel 的宽度,为此您必须创建继承 UITableViewCell 的新子类,并在子类覆盖方法 (void)layoutSubView 和该方法中更改宽度(通过尝试和错误方法)

2)Or second way is to change width of default textLabel , for this you will have to create new subclass inheriting UITableViewCell, and in the subclass override method (void)layoutSubView and in that method change width(do it by trial and error method)

使用以下 .h 和 .m 文件创建新类

create new class with following .h and .m file

////CustomCell .h file

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell

@end

////CustomCell .m file

#import "CustomCell.h"

@implementation CustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];

    CGRect tempFrame=self.textLabel.frame;

     //whatever you want to set
     tempFrame.width=30;
     self.textLabel.frame=tempFrame;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

或者一种不同的选择(更好的一种)

Or one different option(better one)

3) 创建自定义 tableview 单元格

3) Create custom tableview cell

自定义tableview单元格教程

并且因为在 UILabel 的末尾有 ...,所以有 UILabel 的属性 truncateTail.你可以使用它.

And for having ... at the end of UILabel, there is property truncateTail of UILabel. you can use that.

这篇关于tableview 中的文本标签太长会影响正确的细节 (detailTextLabel) 被覆盖或不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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