2017-02-23 9 views
0

어떻게 사용자 정의 셀 클래스 안에있는 함수에서 활성 셀의 indexpath.row를 알아낼 수 있습니까?사용자 정의 셀 클래스의 Indexpath.row

protocol ItemTableViewCellDelegate: NSObjectProtocol { 
    func textFieldDidEndEditing(text: String, cell: ItemTableViewCell) 
} 

class ItemTableViewCell: UITableViewCell, UITextFieldDelegate { 

    @IBOutlet weak var itemTitle: UITextField! 
    @IBOutlet weak var date: UILabel! 

    var delegate: ItemTableViewCellDelegate? 


    override func awakeFromNib() { 
     itemTitle.delegate = self 
    } 

    func textFieldDidEndEditing(_ textField: UITextField) { 
     if let text = textField.text { 
      delegate?.textFieldDidEndEditing(text: text, cell: self) 

     } 
     //BTW: everything below this comment runs every time I want it to, so no problem about that 
     items.insert(itemTitle.text!, at: //(Here I want the indexpath)) 
    } 


} 

그래서, 텍스트 필드의 변화에 ​​따라 내 배열을 업데이트 할 :

나는이 사용합니다. 그러기 위해서는 인덱스 경로를 알아 내야합니다. 나는 그것을 넣으려고했는데 : items.insert(itemTitle.text!, at: indexPath.row)

그러나 그렇게하지 못했습니다.

셀 클래스에서 수행 할 수없는 경우 주 클래스 내부에서 어떻게 수행 할 수 있는지 아이디어에 대해 공개합니다.

내보기의 스크린 샷 :

enter image description here

+0

'items' 배열은 어디에 정의되어 있습니까? –

+2

셀에 데이터 모델을 업데이트 할 텍스트가 변경되었음을 알릴 수있는 대리인이 있어야합니다. – dan

+0

델리게이트 대신 클로저를 사용할 수도 있습니다. – vikingosegundo

답변

1

이 ItemTableViewCell 클래스

var indexPathForCell: IndexPath? 

에 그리고 당신의 부모 뷰 컨트롤러 클래스 cellForRowAtIndexPath에 IndexPath 변수를 추가 : -

let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier", for: indexPath) as! ItemTableViewCell 

cell.indexPathForCell = indexPath 

return cell 
+0

이것은 대부분의 경우 작동하지만 때로는 셀에 설정되지 않고 – Junaid