5

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

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

例子:

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

图例:“Electrical & Electronic Engi....”作为表格视图中显示的文本,“01”作为右侧的 detailTextLabel,“>”作为导航。

应该是这个样子, http: //oi58.tinypic.com/2j4vg5k.jpg,但是由于有些文字太长,所以出现了:http: //oi58.tinypic.com/erc177.jpg

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

谢谢。

(我是 iOS 编程新手)

4

2 回答 2

3

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

你有两个选择

1)不要使用单元格的默认textLabel,创建新的UILabel并将其添加为tableview单元格的子视图。

- (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 中并在该方法中更改宽度(通过试错法进行)

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

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

或另一种选择(更好的一种)

3)创建自定义表格视图单元格

自定义 tableview 单元格教程

并且对于在 UILabel 的末尾有 ... ,有 UILabel 的属性 truncateTail 。你可以使用它。

于 2014-03-03T04:48:15.193 回答
1

尽管单元格配置有点不同(只有详细标签,没有披露指示符),但这个答案可能会有所帮助:如何截断 UITableView 单元格 TextLabel 中的文本,使其不隐藏 DetailTextLabel?

于 2015-04-07T15:41:40.943 回答