【问题标题】:IOS UiTableView Cells how can I make it's height adjustableIOS UiTableView Cells我怎样才能让它的高度可调
【发布时间】:2016-11-27 19:08:06
【问题描述】:

我正在使用 TableView 并有一个 TableViewCell 这是我第一次成功创建一个。我通过 Json 获取数据并填充 TableView;我的问题是所有 TableView 单元格都具有相同的高度,并且由于我返回的数据大小不同,它会导致一些看起来很糟糕的表格单元格。例如,看看下面的图片,如果有更多数据(如下面的第一个单元格显示),我如何制作它以便 TableView 单元格扩展,或者如果它像下面的第二个单元格这样的数据较少,则表格视图单元格会收缩,因此没有太多的空白显示。

这就是 StoryBoard 的外观,有时可以包含大量数据的元素是如下所示的 Post 按钮标签。我基本上是在尝试做两件事:如果 Post-Data 很长,则展开 TableViewCell;如果 Post-data 很小,则使 TableViewCell 更小。

我可以访问所有元素

    class HomePageTVC: UITableViewCell {



    @IBOutlet weak var profile_id: UILabel!
    @IBOutlet weak var comment: UIButton!
    @IBOutlet weak var vote: UIButton!
    @IBOutlet weak var time: UILabel!
    @IBOutlet weak var post: UIButton!
    @IBOutlet weak var fullname: UIButton!
    @IBOutlet weak var location: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        profile_id.isHidden = true
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
      //  post.isUserInteractionEnabled = true

        // Configure the view for the selected state
    }

}

【问题讨论】:

    标签: ios swift uitableview uistoryboard


    【解决方案1】:

    第一步: 如图所示应用约束,

    下图中显示的约束的文本表示是:)

    垂直约束: 五:|-[8]-[全名]-[8]-[时间]-[8]-[TextView]-[8]-|

    任何组件都没有高度限制。 在应用这些约束时,xCode 会建议您将 Content Compression Resistance Priority 修改为 749 这样做!

    第 2 步:取消选中 textView 可滚动属性

    第三步:设置行数为0

    第 4 步:在您的 tableView 控制器的 ViewDidLoad() 中写入

    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 100
    

    第 5 步:不要写 heightForRowAtIndexPath 委托 :)

    就是这样:)

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      除了 Sandeep 的回答之外,您还应该在显示单元格高度之前计算它们的高度,以防止出现一些滞后的滚动体验。

      @interface SomeTableViewController ()
      
      @property (strong, nonatomic) NSMutableDictionary *cellHeightsDictionary;
      
      @end
      
      @implementation SomeTableViewController
      
      - (NSMutableDictionary *)cellHeightsDictionary { //setter
          if (!_cellHeightsDictionary) {
              _cellHeightsDictionary = [[NSMutableDictionary alloc]init];
          }
          return _cellHeightsDictionary;
      }
      
      - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
          NSIndexPath *key = indexPath;
          NSNumber *height = @(cell.frame.size.height);
          //store the pre-calculated cell height and index path to the dictionary
          [self.cellHeightsDictionary setObject:height forKey:key];
      }
      
      - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
          NSNumber *height = [self.cellHeightsDictionary objectForKey:indexPath];
      
          if (height) { //load the height from dictionary for this index path
              return height.floatValue;
          }
      
          return UITableViewAutomaticDimension;
      }
      
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-27
        • 2017-07-01
        • 2014-09-07
        • 2018-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多