iOS 7 - 自定义tableViewCell内容中的文本被删除

时间:2014-03-10 04:56:25

标签: ios uitableview ios7

过去几个小时我一直试图解决这个问题,但无济于事。我有一个自定义tableViewCell类,我用于tableViewCells。我在自定义类中有3个标签和2个textField。这是我的SearchResultTableViewCell.h文件:

#import <UIKit/UIKit.h>

@interface SearchResultTableViewCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel *price;
@property (strong, nonatomic) IBOutlet UILabel *itemName;
@property (strong, nonatomic) IBOutlet UILabel *discount;
@property (strong, nonatomic) IBOutlet UITextField *quantity;
@property (strong, nonatomic) IBOutlet UITextField *discountInput;

@end

文本标签从数据库条目填充。我现在遇到的问题是当itemName太长时,它会在显示屏上被切断。我已经尝试将故事板中Label的Lines属性设置为3行,但它不起作用。有没有人知道如何在标签中实现多行?谢谢。

2 个答案:

答案 0 :(得分:0)

如果将numberOfLines设置为0(并将标签设置为文本Wrap),标签将自动换行并根据需要使用尽可能多的行。

cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; cell.textLabel.lineBreakMode = UILineBreakModeTextWrap;

如果您正在编辑IB中的UILabel,您可以通过按alt-return进入换行来输入多行文本 - 返回将完成编辑。

答案 1 :(得分:0)

CustomTableViewCell.m

    self.itemName.numberOfLines = 0; // change to 0 to support multiple lines
    self.itemName.lineBreakMode = NSLineBreakByWordWrapping;
    CGRect frame = self.itemName.frame; // cache the frame of your item name label
    CGFloat itemWidth = frame.size.width; // keep the width to a variable
    CGSize constraint = CGSizeMake(itemWidth, 2000); // define your constraint, unlimited height, and the width will be the original width
    CGSize size = [self.itemName.text sizeWithFont:self.itemName.font constrainedToSize:constraint lineBreakMode:self.itemName.lineBreakMode]; // get the size that fit your content
    size.width = itemWidth; // use back the original width
    frame.size = size; // update the size to the frame cached just now
    self.itemName.frame = frame; // update the item name label frame