我已经为该单元格设置了一个文本,但是,它显示的文本太长,这会影响正确的详细文本被覆盖或不显示。
我无法更改它,因为我需要下一个视图控制器中的名称。是否可以使其仅显示文本,后跟“....”?
EXAMPLE:
电气电子工程.. 01 >
传奇:
“Electrical & Electronic Engi....”作为表格视图中显示的文本,“01”作为右侧的详细文本标签,“>”作为导航。
它应该是这样的,http://oi58.tinypic.com/2j4vg5k.jpg http://oi58.tinypic.com/2j4vg5k.jpg,但由于有些文字太长,出现以下内容:http://oi58.tinypic.com/erc177.jpg http://oi58.tinypic.com/erc177.jpg
textLabel 和detailTextLabel 似乎不适合或显示在整行中。我希望正确的detailTextLabel仍然存在,并且textLabel以“....”结尾
Thanks.
(我是 iOS 编程新手)
为此,您必须限制 UITableViewCell 的默认 textLabel 的宽度或向单元格添加新的 UILabel。
你有两个选择
1)不要使用单元格的默认文本标签,创建新的 UILabel 并将其添加为表视图单元格的子视图。
- (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)创建自定义表格视图单元格
自定义表格视图单元格教程 http://www.appcoda.com/customize-table-view-cells-for-uitableview/
为了在 UILabel 的末尾有 ... ,有 UILabel 的属性 truncateTail 。你可以用它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)