iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS Swift,使用标记在tableview CellForRow之外更新UITableView自定义单元格标签大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
安装程序( Swift 1.2 / iOS 8.4):

我在UIViewController中有UITableView自定义单元格(标识符= Cell).在自定义TableView单元格中有两个按钮(递增/递减计数)和一个标签(显示计数).@H_197_7@

目标:@H_197_7@

按下增加计数或减少计数按钮时更新标签.@H_197_7@

目前我能够获得按钮Tag并调用CellForRowATindexPath之外的函数.按下按钮可增加和减少计数.但我无法在标签显示计数更新.@H_197_7@

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

    let cell:FoodTypeTableViewCell = self.tableView!.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! FoodTypeTableViewCell

    cell.addBtn.tag = indexPath.row // Button 1
    cell.addBtn.addTarget(self,action: "addBtn:",forControlEvents: .TouchUpInsidE)

    cell.subBtn.tag = indexPath.row // Button 2
    cell.subBtn.addTarget(self,action: "subBtn:",forControlEvents: .TouchUpInsidE)

    cell.countLabel.text = // How can I update this label
    return cell
}

func addBtn(sender: AnyObject) -> Int {
    let button: UIButton = sender as! UIButton
    count = 1 + count
    println(count)
    return count
}

func subBtn(sender: AnyObject) -> Int {
    let button: UIButton = sender as! UIButton
    if count == 0 {
        println("Count zero")
    } else {
        count = count - 1
    }
    println(count)
    return count
}

在这里和那里看到过这个问题,但是在Swift中找不到明确的答案.如果您能帮助清楚地回答它,以便其他人不能复制,但清楚地了解正在发生的事情,我将非常感激.@H_197_7@

谢谢.@H_197_7@

解决方法

这是一个不需要标签解决方案.我不会完全按照您的意愿重新创建单元格,但这涵盖了您要问的部分.

使用Swift 2,因为我没有Xcode 6.x了.@H_197_7@

让我们从UITableViewCell子类开始.这只是一个标签的哑容器,上面有两个按钮.单元格实际上不执行任何特定的按钮操作,它只是将调用传递给配置方法中提供的闭包.这是MVC的一部分.视图不与模型交互,只与控制器交互.控制器提供闭包.@H_197_7@

import UIKit

typealias ButtonHandler = (Cell) -> Void

class Cell: UITableViewCell {

    @IBOutlet private var label: UILabel!
    @IBOutlet private var addButton: UIButton!
    @IBOutlet private var subtractButton: UIButton!

    var incrementHandler: ButtonHandler?
    var decrementHandler: ButtonHandler?

    func configureWithValue(value: UInt,incrementHandler: ButtonHandler?,decrementHandler: ButtonHandler?) {
        label.text = String(value)
        self.incrementHandler = incrementHandler
        self.decrementHandler = decrementHandler
    }


    @IBACtion func increment(sender: UIButton) {
        incrementHandler?(self)
    }


    @IBACtion func decrement(sender: UIButton) {
        decrementHandler?(self)
    }
}

现在控制器也很简单@H_197_7@

import UIKit

class ViewController: UITableViewController {

    var data: [UInt] = Array(count: 20,repeatedValue: 0)

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

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

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

    override func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! Cell

        cell.configureWithValue(data[indexPath.row],incrementHandler: incrementHandler(),decrementHandler: decrementHandler())

        return cell
    }

    private func incrementHandler() -> ButtonHandler {
        return { [uNowned self] cell in
            guard let row = self.tableView.indexPathForCell(cell)?.row else { return }
            self.data[row] = self.data[row] + UInt(1)

            self.reloadCellAtRow(row)
        }
    }

    private func decrementHandler() -> ButtonHandler {
        return { [uNowned self] cell in
            guard
                let row = self.tableView.indexPathForCell(cell)?.row
                where self.data[row] > 0
                else { return }
            self.data[row] = self.data[row] - UInt(1)

            self.reloadCellAtRow(row)
        }
    }

    private func reloadCellAtRow(row: int) {
        let indexPath = NSIndexPath(forRow: row,inSection: 0)

        tableView.beginupdates()
        tableView.reloadRowsATindexPaths([indexPath],withRowAnimation: .AutomatiC)
        tableView.endupdates()
    }

}

当单元格出列时,它会使用要在标签显示的值配置单元格,并提供处理按钮操作的闭包.这些控制器与模型交互,以递增和递减显示的值.更改模型后,它会在tableview中重新加载更改的单元格.@H_197_7@

闭包方法采用单个参数,即对单元格的引用,从中可以找到单元格的行.这比使用标签更加去耦合,这对于了解tableview中单元格的索引是非常脆弱的解决方案.@H_197_7@

您可以从https://bitbucket.org/abizern/so-32931731/get/ce31699d92a5.zip下载完整的工作示例(需要Xcode7)@H_197_7@

大佬总结

以上是大佬教程为你收集整理的iOS Swift,使用标记在tableview CellForRow之外更新UITableView自定义单元格标签全部内容,希望文章能够帮你解决iOS Swift,使用标记在tableview CellForRow之外更新UITableView自定义单元格标签所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。