【问题标题】:How to create buttons that appear in UITableView如何创建出现在 UITableView 中的按钮
【发布时间】:2016-06-26 18:13:14
【问题描述】:

我正在使用 UITableView 控制器制作菜单,并且我需要创建出现在表格的第一个单元格或位置以及表格视图的最末端的按钮。表格视图在运行时也包含 10 个单元格(这是单元格的最大数量)。每个按钮将呈现另一个视图控制器。

我最近尝试使用 viewforSectionInHeader 但没有运气。

如何在 UITableView 中创建两个按钮,一个在第一个位置,一个在最后?最后一个按钮也应该是加号之类的图标。

【问题讨论】:

  • 你可以使用视图控制器。添加一个带有两个按钮的视图。下面放 tableview。或给予设计

标签: xcode swift uitableview uibutton


【解决方案1】:

我认为这就是你想要的viewDidLoad

let topButton = UIButton()
let bottomButton = UIButton()

self.table.tableHeaderView = topButton
self.table.tableFooterView = bottomButton

只需使用表格的tableHeaderViewtableFooterView 即可。

【讨论】:

  • 我想我更喜欢你的回复...点个赞 ;)
  • @penatheboss 喜欢 ;)
  • 谢谢大家!很高兴能提供帮助。
【解决方案2】:

如果最大单元格数为 10 设置

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {  
    return 1
}

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

    return 12
}
 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = _tableView.dequeueReusableCellWithIdentifier("yourCell", forIndexPath: indexPath) as! yourCell

   if indexPath.row ==0 || 11{

  //show button First row or  last
  // add target to button for click also set tag for the button to identify which button clicked 

   }
    else  {
  //hide button
  }
 return cell
}

根据您的设计创建一个带有按钮和标签的表格视图单元格。 根据您隐藏按钮或其他控件的行。 添加插座。

【讨论】:

  • 基于什么条件?
【解决方案3】:

将两个单元格上的按钮添加到原型单元格中,但将它们隐藏起来。在您的 cellForRowAtIndexPath 函数中检查 indexPath.row 值。如果它是第一行或最后一行,则将隐藏值设置为 false。如果它不是第一行或最后一行,则不要更改隐藏值。

您需要知道隐藏了哪些行。因此,如果您有 10 行,则 indexPath.row 的第 9 和第 0 值需要启用按钮的可见性

【讨论】:

  • 隐藏按钮是可行的,但是制作一个不同的单元格要好得多。一个单元格会有按钮,而另一个则没有。
猜你喜欢
  • 2013-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
相关资源
最近更新 更多