【问题标题】:Adding UIButton to detailTextLabel/right side of UITableView将 UIButton 添加到 UITableView 的 detailTextLabel/右侧
【发布时间】:2018-01-09 03:43:55
【问题描述】:

我对 Swift 很陌生,我想知道在 TableViewController 中的 10 个单元格行中添加 10 个按钮的最简单和最简单的方法是什么。

P.S:如果这 10 个按钮执行不同而不是重复,那就太好了。

import UIKit
import FirebaseDatabase

var ref: DatabaseReference?
var databaseHandle: DatabaseHandle?
var postData = [String]()
class TableViewController5: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        ref = Database.database().reference() //set the firebase reference
        // Retrieve the post and listen for changes
        databaseHandle = ref?.child("Posts3").observe(.value, with: { (snapshot) in
            postData.removeAll()

            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let key = snap.key

                postData.append(key)
            }
            self.tableView.reloadData()
        })
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return postData.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.font = UIFont.init(name: "Helvetica", size: 23)
        cell.textLabel?.text = postData[indexPath.row]
        return cell
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
            return 80
    }
}

【问题讨论】:

  • 按钮有什么作用?为什么是按钮?为什么不让用户点击一行?
  • didSelect 方法 - 将获取点击的行。然后在该方法中,您可以进行适当的视图控制器
  • 很抱歉忘记进一步解释,因为这是一个投票应用程序。用户需要点击 10 个展位上的投票按钮,然后它会转到另一个页面
  • 但是每个用户的页面都是一样的吗?
  • 您不需要按钮。当用户点击该行时,您可以执行 segue。

标签: ios swift uitableview uibutton


【解决方案1】:

据我所知,您应该使用原型单元格并在该单元格中添加一个 IBAction,然后在该单元格中您应该执行 segue 并传递您需要自定义加载的页面的任何数据。

在您的tableView(_:cellForRowAt:) 方法中,您可以为按钮设置标签。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? prototypeCell

    cell.voteButton.tag = indexPath.row

    return cell
}

然后在你的单元格的类中,在我之前提到的 IBAction 中,检查标签并设置数据以相应地传递。

然后在您的prepareForSegue:sender: 方法中,只需将您想要传递的数据传递给下一个视图控制器,一切都应该可以正常工作。

【讨论】:

    【解决方案2】:

    看看对你有没有帮助

    //Common cell Variable 
    var cell = UITableViewCell()
    
    //UIButton commonly Declared
    var acceptRequest = UIButton()
    var declineRequest = UIButton()
    

    我的 TableView 单元包含两个按钮,如图所示接受和拒绝

    在我的 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 我为按钮添加了处理程序

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            //Cell for dequeuing 
            cell = self.RequestListTableView.dequeueReusableCell(withIdentifier: "Cell")!
            //Used Tags here
            //You can Make use of TableViewCell class here and Access Buttons from there
            acceptRequest = cell.viewWithTag(4) as! UIButton
    
            //Adding action Handlers for UIbuttons
            self.acceptRequest.addTarget(self, action: #selector(RequestListView.AcceptRequest(sender:)), for: .touchUpInside)
    
            return cell
    }
    

    我的按钮处理程序

    func AcceptRequest (sender: UIButton) {
        //Dequeue cell with identifier can be ignored 
        cell = self.RequestListTableView.dequeueReusableCell(withIdentifier: "Cell")!
    
        //Get button position from TableView : Required
        let buttonPosition : CGPoint = sender.convert(CGPoint.zero, to: self.RequestListTableView)
    
        //Get index Path of selected Cell from TableView 
        //Returns Table Cell index same as didSelect 
        let indexPath : IndexPath = self.RequestListTableView.indexPathForRow(at: buttonPosition)!
    
        //Can be used if you need to update a view 
        cell = self.RequestListTableView.cellForRow(at: indexPath)!
    
       //Now time to access using index Path 
         cell.textLabel.text = array[indexPath.row] //Example
    
    }
    

    这个程序甚至可以用于单个按钮或多个按钮

    如果您在一个单元格中有多个按钮,这将对您有所帮助

    【讨论】:

      猜你喜欢
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多