Ios Swift-如何从UIActionSheet更新自定义单元格标签

Ios Swift-如何从UIActionSheet更新自定义单元格标签,ios,swift,xcode,delegates,Ios,Swift,Xcode,Delegates,我有一个带有标题标签的自定义集合视图单元格子类。我想将单元格的标题标签更改为操作表中选定操作的标题。我尝试在处理程序中执行此操作,但无法通过处理程序访问单元格标签 以下是操作表的代码 func dailyInjuryIllnessActionSheet() { let alert = UIAlertController(title: "Title", message: "Select an Option", preferredStyle:

我有一个带有标题标签的自定义集合视图单元格子类。我想将单元格的标题标签更改为操作表中选定操作的标题。我尝试在处理程序中执行此操作,但无法通过处理程序访问单元格标签

以下是操作表的代码

func dailyInjuryIllnessActionSheet() {
        let alert = UIAlertController(title: "Title", message: "Select an Option", preferredStyle: .actionSheet)
        //alert.addAction(UIAlertAction(title: "Available for Full Participation", style: .default, handler: nil))
        alert.addAction(UIAlertAction(title: "Available for Full Participation", style: .default, handler: { (action) in
            /// Change the cell text - Get the `UILabel`, and replace the text with `action.title`
            

        }))
        alert.addAction(UIAlertAction(title: "Available for Limited Participation", style: .default, handler: nil))
        alert.addAction(UIAlertAction(title: "Not Available For Participation", style: .default, handler: nil))
        alert.addAction(UIAlertAction(title: "Close Injury / Illness", style: .default, handler: nil))
        alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
        present(alert, animated: true, completion: nil)
    }
下面是单元格的代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DIISDetailCell.reuseIdentifier, for: indexPath) as? DIISDetailCell else { return UICollectionViewCell() }
        if let entries = player?.entries {
            for entry in entries {
                cell.eventName.text = entry.injuryIllnessName
                cell.eventDate.text = entry.statusDate.formatted()
                cell.eventType.text = entry.status
                cell.modifiedBy.text = entry.modifiedBy ?? "Modified By Goes Here"
                
            }
        }
        return cell
    }
我整个星期都在忙这个

我称之为代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        dailyInjuryIllnessActionSheet()
    }

用户从警报中做出选择后,需要更新模型数据(以及随后的单元标签)。警报选择是异步进行的。您可以使用几种常见的方法。一是授权。另一个是传递完成处理程序闭包。第二个更快捷,我将使用它

首先,您的
cellForItemAt
中有一个错误-它将为每个单元格调用,因此您不应该有
for
循环-您需要基于
IndexPath.item
访问所需的元素。这是一个通常使用强制向下广播和强制展开的函数,因为需要注册单元格类,如果没有条目(或没有
player
),则
numberOfItems
函数应返回0-

通过使用选项数组,您可以稍微简化
日常InjuryIllnessActionSheet
代码。为简单起见,我在函数中声明了数组,但它可能应该从其他信息源传入

向该函数添加
completion
处理程序参数,以将所选选项传递回调用者:

func dailyInjuryIllnessActionSheet(completion:((String?)->Void)?=nil) {
    
    let options = ["Available for Full Participation",
                   "Available for Limited Participation",
                   "Not Available For Participation",
                   "Close Injury / Illness"
    ]
    let alert = UIAlertController(title: "Title", message: "Select an Option", preferredStyle: .actionSheet)
    
    for option in options {
        alert.addAction(UIAlertAction(title: option, style: .default, handler: { action in 
            completion?(option)
        }))
    }
    alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: { action in
        completion?(nil)
    }))
    present(alert, animated: true, completion: nil)
}
现在,您可以在调用
dailyInjuryIllnessActionSheet
更新模型并重新加载项目时传递完成处理程序:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    dailyInjuryIllnessActionSheet() { selectedOption in {
        guard let selectedOption = selectedOption else {
            return
        }
        player!.entries[indexPath.item].status = selectedOption
        collectionView.reloadItems(at:[indexPath])
    }
  
}

用户从警报中做出选择后,需要更新模型数据(以及随后的单元标签)。警报选择是异步进行的。您可以使用几种常见的方法。一是授权。另一个是传递完成处理程序闭包。第二个更快捷,我将使用它

首先,您的
cellForItemAt
中有一个错误-它将为每个单元格调用,因此您不应该有
for
循环-您需要基于
IndexPath.item
访问所需的元素。这是一个通常使用强制向下广播和强制展开的函数,因为需要注册单元格类,如果没有条目(或没有
player
),则
numberOfItems
函数应返回0-

通过使用选项数组,您可以稍微简化
日常InjuryIllnessActionSheet
代码。为简单起见,我在函数中声明了数组,但它可能应该从其他信息源传入

向该函数添加
completion
处理程序参数,以将所选选项传递回调用者:

func dailyInjuryIllnessActionSheet(completion:((String?)->Void)?=nil) {
    
    let options = ["Available for Full Participation",
                   "Available for Limited Participation",
                   "Not Available For Participation",
                   "Close Injury / Illness"
    ]
    let alert = UIAlertController(title: "Title", message: "Select an Option", preferredStyle: .actionSheet)
    
    for option in options {
        alert.addAction(UIAlertAction(title: option, style: .default, handler: { action in 
            completion?(option)
        }))
    }
    alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: { action in
        completion?(nil)
    }))
    present(alert, animated: true, completion: nil)
}
现在,您可以在调用
dailyInjuryIllnessActionSheet
更新模型并重新加载项目时传递完成处理程序:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    dailyInjuryIllnessActionSheet() { selectedOption in {
        guard let selectedOption = selectedOption else {
            return
        }
        player!.entries[indexPath.item].status = selectedOption
        collectionView.reloadItems(at:[indexPath])
    }
  
}

你每天在哪里调用
injuryillnesactionsheet
?我在didSelectItemAt@paulw11中调用它请你的问题显示代码。您可以将单元格传递给函数,也可以将完成处理程序传递给函数并更新完成处理程序中的标签我刚刚解决了我的问题。如果你有时间,你能告诉我你的意思吗?UILabel是在cell子类中定义的。您在哪里调用
dailyInjuryIllnesActionSheet
?我在didSelectItemAt@paulw11中调用它请您提问以显示代码。您可以将单元格传递给函数,也可以将完成处理程序传递给函数并更新完成处理程序中的标签我刚刚解决了我的问题。如果你有时间,你能告诉我你的意思吗?UILabel由cell子类定义。