CollectionView Custom Cell访问单元格外部的属性

时间:2017-06-24 14:28:55

标签: ios swift swift3 uicollectionview

我正在尝试在单元格之外的集合视图中更改自定义视图单元格的某些属性,但我无法弄清楚我做错了什么:

在cellForItem中我有:

  //post actions
    func editPostAction()  {
        print("Edit post Enabled")

        let cell = PatientQuestionCell()

        cell.questionView.isEditable = true
        cell.questionView.isSelectable = true
        cell.questionView.isScrollEnabled = true
        cell.questionView.becomeFirstResponder()

    }

这里的一切都很完美但是当我触发动作时没有任何反应。这是功能:

$item = new Item;
$data = $request->all();
$item->create($data);

$item = new Item;
$data = $request->all();

if($item->create($data))
{
    return response()->json(["response" => 200, "joke" => $item]);
}
else
{
    return response()->json(["response" => 400, "joke" => $item]);
}

如何使用此功能更改单元格的属性?

1 个答案:

答案 0 :(得分:1)

您可以使用UIButton标记属性,如下所示:

collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)方法中,添加以下代码行:

cell.commentButton.tag = (indexPath.section * 100) + indexPath.item

然后设置按钮的目标:

cell.commentButton.addTarget(self, action: #selector(editPostAction(sender:)), for: .touchUpInside)

现在,添加一个新方法,即您的选择器:

func editPostAction(sender: UIButton) {
    let section = sender.tag / 100
    let item = sender.tag % 100
    let indexPath = IndexPath(item: item, section: section)

    let cell = self.collectionView?.cellForItem(at: indexPath) as! PatientQuestionCell
    cell.questionView.isEditable = true
    cell.questionView.isSelectable = true
    cell.questionView.isScrollEnabled = true
    cell.questionView.becomeFirstResponder()
}