简体   繁体   English

表格查看单元格高度

[英]Table View Cell Height

I have multiple cells in my table view; 我的表视图中有多个单元格; eg Cat Cell, Dog Cell, Chicken Cell. 例如Cat Cell,Dog Cell,Chicken Cell。 In the Cat Cell I have two views: a graphs view and an images view. 在Cat Cell中,我有两个视图:图形视图和图像视图。

If the graphs view is hidden and the images view is visible, then I want to set my cell height to 200, and 350 in the opposite case. 如果隐藏图形视图并且图像视图可见,那么我想将单元格高度设置为200,而在相反的情况下则设置为350。

How would I achieve that? 我怎么做到这一点? I am registering my nib file in the heightForRowAt delegate method of the table view and I have tried different things to no avail. 我正在表视图的heightForRowAt委托方法中注册我的nib文件,我尝试了不同的东西无济于事。 These are the two views in my cell: 这是我单元格中的两个视图:

@property (weak, nonatomic) IBOutlet UIView *imagesView;
@property (weak, nonatomic) IBOutlet UIView *graphView;

...and this is my heightForRowAt method: ...这是我的heightForRowAt方法:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    TrainingImageCell *cell = (TrainingImageCell*)[self.tableView dequeueReusableCellWithIdentifier:@"TrainingImageCell"];
    if (cell.isGraphViewOnTop == YES) {
        return 350;
    }
    else {
        return 200;
    }
    return 200;
}

Instead of initiating a new cell, you need to access the one what is already in the tableView . 您需要访问tableView已有的单元,而不是启动新单元。

Instead of 代替

TrainingImageCell *cell = (TrainingImageCell*)[self.tableView dequeueReusableCellWithIdentifier:@"TrainingImageCell"];

Do

TrainingImageCell *currentCell = (TrainingImageCell*)[tableView cellForRowAtIndexPath:indexPath];

And than take a look on the property. 而不是看看物业。

EDIT: Looking at the lengthy discussion we have, here is a very reliable example for an approach you could use in Swift . 编辑:看看我们的冗长讨论,这里有一个非常可靠的例子,可以在Swift中使用。

import UIKit

class RightAlignedCell: UITableViewCell {
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var subLabel: UILabel!
    // Your boolean property you would like to map
    var randomBool: Bool = false
}

class RightAlignedTableViewController: UIViewController {
    // This is just an example for the dataSource, you should have this somewhere already
    lazy var dataSource: [Bool] = {
        var dataSource: [Bool] = []
        for index in 0..<10 {
            dataSource.append(index % 2 == 0)
        }
        return dataSource
    }() 

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tableView.reloadData()
    }
}

extension RightAlignedTableViewController: UITableViewDelegate {}

extension RightAlignedTableViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        // Take a look in the !!!dataSource!!!, what is value for isHidden
        let isHidden = self.dataSource[indexPath.row]
        // return the right height
        return isHidden ? 60 : 100
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: RightAlignedCell = tableView.dequeueReusableCell(withIdentifier: "RightAlignedCell", for: indexPath) as! RightAlignedCell
        cell.titleLabel.text = "Something"
        cell.subLabel.text = "Nothing"
        cell.randomBool = self.dataSource[indexPath.row]

        return cell
    }
}

Why are registering nib file in heightForRowAt. 为什么要在heightForRowAt中注册nib文件。 You are registering new cell hence cannot access the existing cell. 您正在注册新单元格,因此无法访问现有单元格。 If you want it working in your style instead of registering new cell in height for row at IndexPath you can access existing cell using indexPath as 如果您希望它以您的样式工作而不是在IndexPath中为行注册新单元格,则可以使用indexPath访问现有单元格

TrainingImageCell *currentCell = (TrainingImageCell*)[tableView cellForRowAtIndexPath:indexPath];

and access check property. 和访问检查属性。

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 TrainingImageCell *currentCell = (TrainingImageCell*)[tableView cellForRowAtIndexPath:indexPath];
  if (cell.isGraphViewOnTop == YES) {
  return 350;
  }
return 200;
}

If you dequeueReusableCellWithIdentifier you just get a new cell, it will not have any data to check if isGraphViewOnTop 如果你dequeueReusableCellWithIdentifier你得到一个新的单元格,它将没有任何数据来检查isGraphViewOnTop

My advice will be to return UITableViewAutomaticDimension and set cell views with constants, so when the element is hidden, a cell will have a smaller size. 我的建议是返回UITableViewAutomaticDimension并使用常量设置单元格视图,因此当隐藏元素时,单元格的大小会更小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM