首页 > 解决方案 > 如何在ios swift中重新加载TableView的可见单元格

问题描述

我正在尝试在单元格加载数据后更改字体大小。单击选项卡栏的图标时,我会显示一个警报控制器操作表。单击其中一项操作时,我想更改单元格中标签的字体大小。

在此处输入图像描述

在此处输入图像描述

我正在使用的代码如下:

 //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 的操作被触发时,我该如何修复它以更改。

标签: iosxcodeuitableview

解决方案


试试这个片段

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

推荐阅读