【问题标题】:Display UIButton in UITableView , when Cell is selected选择 Cell 时在 UITableView 中显示 UIButton
【发布时间】:2015-10-23 01:14:48
【问题描述】:

我想在我的UITableViewCell 中添加一个UIButton。我的单元格在被选中时会扩展高度。只有在扩展区域中调用didSelectRow 时才能显示该按钮...我有 4 个我想用不同的问题、表单和按钮填充的单元格。我应该对每个单元格进行子分类吗?

这是我目前的代码:

let SelectedCellHeight: CGFloat = 500.0
let UnselectedCellHeight: CGFloat = 44.0
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let logItemCell = tableView.dequeueReusableCellWithIdentifier("LogCell", forIndexPath: indexPath) as! UITableViewCell




}

// Used to expand cell when selected
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    if let selectedCellIndexPath = selectedCellIndexPath {
        if selectedCellIndexPath == indexPath {
            return SelectedCellHeight
        }
    }
    return UnselectedCellHeight
}


func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

    if let selectedCellIndexPath = selectedCellIndexPath {
        if selectedCellIndexPath == indexPath {
            self.selectedCellIndexPath = nil
        } else {
            self.selectedCellIndexPath = indexPath
        }
    } else {
        selectedCellIndexPath = indexPath
    }
    tableView.beginUpdates()
    tableView.endUpdates()

}

【问题讨论】:

    标签: ios iphone swift uitableview uibutton


    【解决方案1】:

    是的,您应该制作 4 个具有不同标识符和类的原型单元格,并根据 indexPath 在“cellForRowAtIndexPath”中返回您想要的那个。

    【讨论】:

    • 我应该在我创建的 UITableViewCell 类中实例化按钮吗?
    【解决方案2】:

    您应该对所需的每个单元格进行子类化。当didSelectRow被调用并且单元格被扩展时button.hidden = false应该被调用

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多