繁体   English   中英

表格查看单元格高度

[英]Table View Cell Height

我的表视图中有多个单元格; 例如Cat Cell,Dog Cell,Chicken Cell。 在Cat Cell中,我有两个视图:图形视图和图像视图。

如果隐藏图形视图并且图像视图可见,那么我想将单元格高度设置为200,而在相反的情况下则设置为350。

我怎么做到这一点? 我正在表视图的heightForRowAt委托方法中注册我的nib文件,我尝试了不同的东西无济于事。 这是我单元格中的两个视图:

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

...这是我的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;
}

您需要访问tableView已有的单元,而不是启动新单元。

代替

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

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

而不是看看物业。

编辑:看看我们的冗长讨论,这里有一个非常可靠的例子,可以在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
    }
}

为什么要在heightForRowAt中注册nib文件。 您正在注册新单元格,因此无法访问现有单元格。 如果您希望它以您的样式工作而不是在IndexPath中为行注册新单元格,则可以使用indexPath访问现有单元格

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

和访问检查属性。

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

如果你dequeueReusableCellWithIdentifier你得到一个新的单元格,它将没有任何数据来检查isGraphViewOnTop

我的建议是返回UITableViewAutomaticDimension并使用常量设置单元格视图,因此当隐藏元素时,单元格的大小会更小。

暂无
暂无

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

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