2016-12-29 53 views
0

私はUITableViewを持っていて、内部には5つのtextFieldがあります。 私はUITextFieldDelegateを割り当てて、textFieldに境界線を作成します。私はcellForRowAtIndexPathから関数createBorderLineを呼び出していますが、それはエラーを投げています(致命的なエラー:予期せず、オプション値をアンラッピングしている間にnilが見つかりました)。以下はcellForRowAtIndexPathからtextFieldの境界線を取得するにはどうすればよいですか?

は私のコードです:

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

     let identifier = "EditProductCell" 

     var editProductCell = tableView.dequeueReusableCell(withIdentifier: identifier) as? EditProductCell 
     if(editProductCell == nil) 
     { 
      let nib:Array = Bundle.main.loadNibNamed("EditProductCell", owner: self, options: nil)! 
      editProductCell = nib[0] as? EditProductCell 

      //Call Create Border Line function. 
      self.createBorderLine() 
     } 
} 

そして、ここでは私のcreateBorderLine機能である:

func createBorderLine() 
{ 
    let index : NSIndexPath = NSIndexPath(row: 0, section: 0) 
    let tCell : EditProductCell = self.tableView.cellForRow(at: index as IndexPath) as! EditProductCell 

    tCell.InvoiceDate.delegate = self 
    tCell.InvoiceNumber.delegate = self 
    tCell.modelNumber.delegate = self 
    tCell.productName.delegate = self 
    tCell.serialNumber.delegate = self 
    tCell.viewWarrentyDate.isHidden = true 


    setBottomBorder(textField: tCell.InvoiceDate, width: 0.8,color : UIColor.lightGray) 
    setBottomBorder(textField: tCell.InvoiceNumber, width: 0.8,color : UIColor.lightGray) 
    setBottomBorder(textField: tCell.modelNumber, width: 0.8,color : UIColor.lightGray) 
    setBottomBorder(textField: tCell.productName, width: 0.4,color : UIColor.lightGray) 
    setBottomBorder(textField: tCell.serialNumber, width: 0.4,color : UIColor.lightGray) 
} 

私は何ができますか?なぜそれはエラーを出すのですか?

+0

なぜ、 'let index:NSIndexPath = NSIndexPath(行:0、セクション:0)'を作成していますか?あなたはちょうどcreateBorderLineにセルオブジェクトを渡すことができます –

+0

OK 実際に私は少しTableViewConceptsで混乱しています。私が知っている基本は です。とにかくありがとうございます。 – kishor0011

答えて

2

なぜ、createBorderLineで毎回0行と0行のインデックスパスを作成していますか?単にcreateBorderLine

self.createBorderLine(editProductCell) 

とする代わりに、あなたがEditProductCellクラスでcreateBorderLineを置くべきコントローラクラスでcreateBorderLineを作成する

func createBorderLine(tCell: EditProductCell) 
{ 

tCell.InvoiceDate.delegate = self 
tCell.InvoiceNumber.delegate = self 
tCell.modelNumber.delegate = self 
tCell.productName.delegate = self 
tCell.serialNumber.delegate = self 
tCell.viewWarrentyDate.isHidden = true 


setBottomBorder(textField: tCell.InvoiceDate, width: 0.8,color : UIColor.lightGray) 
setBottomBorder(textField: tCell.InvoiceNumber, width: 0.8,color : UIColor.lightGray) 
setBottomBorder(textField: tCell.modelNumber, width: 0.8,color : UIColor.lightGray) 
setBottomBorder(textField: tCell.productName, width: 0.4,color : UIColor.lightGray) 
setBottomBorder(textField: tCell.serialNumber, width: 0.4,color : UIColor.lightGray) 

} 

createBorderLine関数内でのセル参照を渡します。 EditProductCellオブジェクトrefを直接呼び出すことができます。

+0

はい私はこれとその作業を試みました。 他にも方法はありますか? – kishor0011

+0

EditProductCellクラスにcreateBorderLineを配置することをお勧めします。 cellForRowAt indexPathメソッドのEditProductCell refからcreateBorderLineに直接アクセスすることができます。 – Sahil

+0

あなたは正しいです。私は同じ考えをしていた。 しかし、私はtextFieldをタップするときに境界線の色を変更しています。 ありがとう – kishor0011

関連する問題