选择时扩展和收缩的变体单元格高度

时间:2016-09-06 13:23:54

标签: ios swift uitableview swift2

我有TableViewController类,可以加载三种不同的客户cell类型。在tableView中加载了哪种单元格类型取决于在其他地方选择的variable。每个单元格类型的高度不同,有时相同类型的高度可能会有所不同,具体取决于单元格textimage字段内的信息量。

当您单击它们展开的每个单元格时,每个单元格都具有展开和未选定的高度。

这是我的代码:

 // MARK: - Table View Settings
// Cell Settings
let selectedCellHeight: CGFloat = 592
let unselectedCellHeight: CGFloat = 420
var selectedCellIndexPath: NSIndexPath?

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 4
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    switch cellType {
    case CellName.Verbs.rawValue:
        let cell = tableView.dequeueReusableCellWithIdentifier(cellType, forIndexPath: indexPath) as! VerbCell
        tableView.separatorColor = UIColor.clearColor()
        return cell
    case CellName.Applicants.rawValue:
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCellWithIdentifier(CellName.Verbs.rawValue, forIndexPath: indexPath) as! VerbCell
            tableView.separatorColor = UIColor.clearColor()
            return cell
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier(cellType, forIndexPath: indexPath) as! ApplicantCell
            tableView.separatorColor = UIColor.clearColor()
            scrollToSecondtRow()
            return cell
        }
    default:
        let cell = tableView.dequeueReusableCellWithIdentifier(cellType, forIndexPath: indexPath)
        return cell
    }
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    switch cellType {
    case CellName.Verbs.rawValue, CellName.Applicants.rawValue:
        if selectedCellIndexPath == indexPath {
            return selectedCellHeight
        }
        return unselectedCellHeight
    default:
        return 120
    }
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    switch cellType {
    case CellName.Verbs.rawValue, CellName.Applicants.rawValue:
        // Adjust Height Of Verb Cells
        if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath {
            selectedCellIndexPath = nil
        } else {
            selectedCellIndexPath = indexPath
        }
        tableView.beginUpdates()
        tableView.endUpdates()
        if selectedCellIndexPath != nil {
            tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .None, animated: true)
        }
    default:
        break
    }
}

// MARK: - Functions
func scrollToSecondtRow() {
    let indexPath = NSIndexPath(forRow: 1, inSection: 0)
    self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: false)
}

我遇到的问题是根据细胞高度的内容自动调整每个细胞的扩展视图,细胞高度可以在400到900个点之间,具体取决于里面的内容。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您应该实施UITableView委托方法tableView:heightForRowAtIndexPath:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

另外,实施tableView:estimatedHeightForRowAtIndexPath:以便为UITableView提供估计的单元格大小。

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
      return 120;
}

或者,如果您的UITableView对所有单元格使用相同的估计大小,则可以使用:

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

确保单元格中项目的约束是可调整的 - 并且 - 仅当您的应用程序支持iOS 7之前的版本时 - 在tableView:heightForRowAtIndexPath:内返回固定大小。 这是一个简单的教程,可以帮助您更好地理解它们。 https://www.raywenderlich.com/129059/self-sizing-table-view-cells

答案 1 :(得分:0)

我创建了一个名为resizable cell的自定义单元格,它只是一个带标签的单元格。标签设置为自动布局所有边缘为零。

我所做的是未选择的单元格将具有一定数量的行(在这种情况下为2),并且在选择时将其更改为零,这允许标签增长以适合文本内容,这反过来将使单元格变得适合标签。这可能与您正在做的完全不同,但这应该指向正确的方向。

测试了它并且它正在工作。

您的视图控制器:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

let data: [String] = ["Some really long string, Some really long string, Some really long string, Some really long string, Some really long string, Some really long string, Some really long string,Some really long string",
                      "some short string",
                      "some medium string, some medium string, some medium string, some medium string, some medium string",
                      "extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string, extremely long string"]

let cellId = String(ResizeableCell.self)
// if you will have multiple selected cells at once then create an array of selected indices
var selectedIndexPath: NSIndexPath?

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.tableView.registerNib(UINib(nibName: cellId, bundle: nil), forCellReuseIdentifier: cellId)
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 100
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


//datasource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier(cellId) as! ResizeableCell
    if indexPath == selectedIndexPath {
        cell.resizeLabel.numberOfLines = 0
        cell.resizeLabel.lineBreakMode = .ByWordWrapping
    } else {
        cell.resizeLabel.numberOfLines = 2
        cell.resizeLabel.lineBreakMode = .ByTruncatingTail
    }
    cell.resizeLabel.text = data[indexPath.row]

    cell.backgroundColor = UIColor.orangeColor()

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if selectedIndexPath != indexPath {
        // grow
        selectedIndexPath = indexPath
    } else {
        // shrink
        selectedIndexPath = nil
    }
    tableView.beginUpdates()
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    tableView.endUpdates()
}
}