如何在iOS Swift中重新加载TableView的可见单元格

时间:2019-05-13 06:36:50

标签: ios xcode uitableview

我正在尝试在单元格加载数据后更改字体大小。单击选项卡图标后,我将显示一个Alertcontroller操作表。 单击其中一项操作后,我想更改单元格中标签的字体大小。

enter image description here

enter image description here

我正在使用的代码如下:

 //Two global variables,retaining size of each labels
 var fontSizeWord = 20.0
 var fontSizeMeaning = 12.0

 func changeFont(){
        let optionMenu = UIAlertController(title: nil, message: "Change Font", preferredStyle: .actionSheet)
        optionMenu.view.tintColor = UIColor(red: 179/256, green: 180/256, blue: 255/256, alpha: 1.0)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        let smallfont = UIAlertAction(title: "Small", style: .default) { (UIAlertAction) in
            self.fontSizeMeaning = 12
            self.fontSizeWord = 20
            self.reloadData()
        }
        let mediumfont = UIAlertAction(title: "Medium", style: .default) { (UIAlertAction) in
            self.fontSizeMeaning = 15
            self.fontSizeWord = 23
            self.reloadData()
        }
        let largefont = UIAlertAction(title: "Large", style: .default) { (UIAlertAction) in
            self.fontSizeMeaning = 18
            self.fontSizeWord = 26
            self.reloadData()
        }
        let extraLarge = UIAlertAction(title: "Extra Large", style: .default) { (UIAlertAction) in
            self.fontSizeMeaning = 21
            self.fontSizeWord = 29
            self.reloadData()
        }
        optionMenu.addAction(cancelAction)
        optionMenu.addAction(smallfont)
        optionMenu.addAction(mediumfont)
        optionMenu.addAction(largefont)
        optionMenu.addAction(extraLarge)
        self.present(optionMenu, animated: true, completion: nil)
    }


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:SearchTableViewCell = self.searchTableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! SearchTableViewCell
        cell.wordLbl.font = cell.wordLbl.font.withSize(CGFloat(fontSizeWord))
        cell.meaningLbl.font = cell.meaningLbl.font.withSize(CGFloat(fontSizeMeaning))
        return cell
    }

我的问题是可见的单元格的字体只有在我滚动一点时才改变。我如何解决它以随着uiactionsheet的动作被触发而改变。

4 个答案:

答案 0 :(得分:3)

尝试此代码段

self.searchTableView.reloadRows(at: self.searchTableView.indexPathsForVisibleRows!, with: .automatic)

答案 1 :(得分:2)

听起来像必须从主线程调用reloadData

DispatchQueue.main.async {
    self.tableView.reloadData()
}

如果这不起作用,则您的问题必须在其他地方。将您的项目与我的解决方案进行比较:

class ViewController: UITableViewController {

    enum FontSize: String, CaseIterable {
        case small = "Small"
        case medium = "Medium"
        case large = "Large"

        var pointSize: CGFloat {
            switch self {
            case .small: return 10
            case .medium: return 20
            case .large: return 30
            }
        }
    }

    var currentFontSize: FontSize = .medium {
        didSet {
            if currentFontSize != oldValue, let indexPathsForVisibleRows = tableView.indexPathsForVisibleRows {
                tableView.reloadRows(at: indexPathsForVisibleRows, with: .automatic)
            }
        }
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.font = UIFont.systemFont(ofSize: currentFontSize.pointSize)
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        let alert = UIAlertController(title: "Font Size", message: "Choose your preferred font size.", preferredStyle: .actionSheet)

        let handler: (UIAlertAction) -> Void = { alert in
            guard let title = alert.title, let fontSize = FontSize(rawValue: title) else { return }
            self.currentFontSize = fontSize
        }

        for fontSize in FontSize.allCases {
            alert.addAction(UIAlertAction(title: fontSize.rawValue, style: .default, handler: handler))
        }
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

        present(alert, animated: true)
    }

}

答案 2 :(得分:1)

您必须创建一些方法,这就是我的方法。

func configureVisibleCells(for tableView: UITableView?, animated: Bool) {
    self.tableView(tableView, configureRowsAtIndexPaths: tableView?.indexPathsForVisibleRows, animated: animated)
}

func tableView(_ tableView: UITableView?, configureRowsAtIndexPaths indexPaths: [Any]?, animated: Bool) {
    for indexPath in indexPaths as? [IndexPath] ?? [] {
        let cell: UITableViewCell? = tableView?.cellForRow(at: indexPath)
        if cell != nil {
            self.tableView(tableView, configureCell: cell, forRowAt: indexPath, animated: animated)
        }
    }
}

func tableView(_ tableView: UITableView?, configureCell cell: UITableViewCell?, forRowAt indexPath: IndexPath?, animated: Bool) {
    // Cell configuration
}

tableView(_ tableView: UITableView?, configureCell cell: UITableViewCell?, forRowAt indexPath: IndexPath?, animated: Bool)方法中配置单元格,然后在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell中调用此方法。

,当您需要重新加载可见单元格时,请调用方法tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

希望这对您有所帮助。

答案 3 :(得分:0)

要获取可见行的indexPath,可以使用以下方法:

let indexPathsForVisibleRows = yourTableView.indexPathsForVisibleRows

然后您可以使用以下方法重新加载这些特定的单元格:

yourTableView.reloadRowsAtIndexPaths([indexPathsForVisibleRows], withRowAnimation: UITableViewRowAnimation.automatic)