在uitableview中编辑特定单元格

问题描述 投票:1回答:3

enter image description here

我有一个表格视图单元格,我在其中使用操作表来更改应用程序的语言。动作发生后,我想更改显示的语言标题。我该如何访问该单元格?我可以使用下标来调用和编辑该单元格的按钮标题吗?

 let actionSheet = UIAlertController(title:NSLocalizedString("Message", comment: "Message"), message:"", preferredStyle:.actionSheet)
    //
    actionSheet.addAction(UIAlertAction(title:"English", style:.default, handler: { (action) in
        Bundle.setLanguage("en")
                                        self.navigationController?.dismiss(animated:true, completion:nil)
        self.languageSelected.text = "English"
        self.cell.notificationButton.setTitle("English" , for: .normal )
        self.lang = 0
        self.langChange()
 ////////////////////////////////////////////////
   func langChange() {
if lang == 0 {
cell.notificationButton.setTitle("English" , for: .normal )}
if lang == 1 {
cell.notificationButton.setTitle("Hindi" , for: .normal )}
if lang == 2 {
cell.notificationButton.setTitle("Tamil" , for: .normal )}
if lang == 3 {
cell.notificationButton.setTitle("Telugu" , for: .normal )}
}



   ////////////////////////////////////////

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! AppSettingViewCell
    cell.selectionStyle = .none
    cell.leftText.text = menuArray[indexPath.row]
    cell.isUserInteractionEnabled = true

    if lang == 0 {
        cell.notificationButton.setTitle("English" , for: .normal )}
    if lang == 1 {
        cell.notificationButton.setTitle("Hindi" , for: .normal )}
    if lang == 2 {
        cell.notificationButton.setTitle("Tamil" , for: .normal )}
    if lang == 3 {
        cell.notificationButton.setTitle("Telugu" , for: .normal )}
    if indexPath.row == 0 {
        cell.notificationButton = UIButton()
        cell.languageLabel = UILabel()
        cell.addSubview(cell.notificationButton)
        cell.addSubview(cell.languageLabel)
        languageSelected.textColor = appGreenTheme
         cell.languageLabel.text = languageSelected.text
        cell.languageLabel.textColor = appGreenTheme
        cell.languageLabel.isHidden = true
        cell.notificationButton.setTitleColor(appGreenTheme, for: .normal)
        cell.notificationButton.isHidden = false

..我尝试用很多技术改变它,但第一个单元格的标题没有变化。请任何人可以帮忙吗?

ios swift uitableview
3个回答
0
投票

在您的特定情况下,由于您的更改不需要更改单元格布局(通过布局更改,我的意思是更改单元格不涉及高度计算),您可以使用以下

....
self.languageSelected.text = "English"
self.cell.notificationButton.setTitle("English" , for: .normal )
self.lang = 0
self.langChange()

let cell = YOUR_TABLEVIEW_REF.cellForRow(at: IndexPath(row: YOUR_ROW_FOR_LANGUAGE_CELL, section: YOUR_SECTION_FOR_LANGUAGE_CELL)) as! AppSettingViewCell

// REST OF YOUR LOGIC TO CHANGE THE CELL CONTENT BASED ON YOUR IMPLEMENTATION.

cellForRow(at indexPath: IndexPath)将获取当前在给定indexPath中显示的单元格。当您点击单元格并显示操作表时,相当清楚的是,单元格当前正在渲染。打电话给cellForRow(at indexPath: IndexPath)会让你回到那个牢房。

Appledoc


0
投票

看起来你正在保存你的UITableViewCell在封闭类中,并试图通过该引用修改它。我不确定这是否可行,但我知道这不是推荐的改变细胞的方法。

相反,您应该更改cellForRowAt()函数中的语言,就像您已经尝试过的那样。缺少的是,当调用langChange()时,该函数只需要知道你的UITableView并调用reloadData()

func langChange() {
    tableView.reloadData()
}

为什么这样做?

reloadData()强制表重新加载其所有单元格,因为它认为它所基于的数据集已经改变。此时,每次当前显示的单元格再次调用cellForRowAt()。在cellForRowAt()中,您现在将检查选择的语言,并根据语言设置单元格的标题。


0
投票

根据我的理解,您想要更改指定单元格中语言更改后将在操作表中显示的标题,对吗?如果是这样,请尝试使用委托。

示例:在您的单元类中,创建一个函数或@IBAction以在主视图中打开其操作表并将其单元格作为参数传递,当主视图接收其参数(单元格)时,使用tableView.indexPath(for:cell)参数)它将为您提供打开其操作表的单元格的特定indexPath,并通过它更改标题

© www.soinside.com 2019 - 2024. All rights reserved.