【问题标题】:iOS: How to fit UILabel text in UITableViewCell?iOS:如何在 UITableViewCell 中放置 UILabel 文本?
【发布时间】:2014-09-05 01:03:56
【问题描述】:

这是我的第一个iOS 项目,所以我学到了很多东西,并且必须学习更多。

我了解到,为了在UITableViewCell 上容纳更多项目,我需要对其进行子类化然后使用它。我创建了TransactionCell,并在我的ViewController 中将其用作

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // make last cell visible: http://stackoverflow.com/questions/11928085/uitableview-not-visible-the-last-cell-when-scroll-down
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 120, 0);
    [tableView registerNib:[UINib nibWithNibName:@"TransactionCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];

    TransactionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[TransactionCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];


    }
    TransactionModel *transactionModel = self.transactionsModel.transactions[(NSUInteger) indexPath.row];
    cell.dateAndMonth.text = transactionModel.date;
    cell.name.text = transactionModel.name;
    cell.amount.text = transactionModel.amount;
    cell.categoryImage.image = [self getShortImage:[UIImage imageNamed:transactionModel.category]];
    cell.transactionTypeImage.image = [self getShortImage:[UIImage imageNamed:@"Debit"]];
    cell.imageView.contentMode = UIViewContentModeCenter;
    return cell;
}  

另外,我将单元格的高度设置为

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

我的xib 看起来像

但是,当我运行该项目时,我会看到以下内容

事情不合适!

问题
- 如何使标签完全显示在TransactionCell 上?
- 如何在不切断的情况下将所有元素放在一行中

我确定我需要学习其他东西,但不确定是什么

【问题讨论】:

  • 在 Interface Builder 中更改标签的大小。
  • 试过但没有解决
  • 实现这个的方法太多了,auto layout auto resizing,一个很大的话题
  • 我现在不在我的 Mac 上,但是如果您选择标签,然后单击带有自定义选项的选项卡,应该会有截断选项。像,截断尾巴,截断中间......等等。使用这些设置,直到它像你想要的那样运行。

标签: ios objective-c uitableview


【解决方案1】:

当您在 xib 中选择标签时,转到属性检查器中的自动收缩部分并将选项更改为最小字体比例,我通常将其设置为 0.5 左右。

【讨论】:

    【解决方案2】:

    在单元格内添加三个单独的 UILabel 并将框架设置为三个不同的大小。

    【讨论】:

      【解决方案3】:

      正如@Shadowfiend 所说,您必须使用自动布局和自动调整大小。

      查看此链接:http://www.appcoda.com/self-sizing-cells/

      这是关于自动调整单元格大小的很好的教程,并且描述也很好。

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多