我有一个UITableView,并且在UITableViewCell里面有5个textFields。我必须分配UITextFieldDelegate并想在textField上创建边框线。我正在从cellForRowAtIndexPath调用我的函数createBorderLine,但是它引发了一个错误(严重错误:在展开一个Optional值时意外发现nil)。
下面是我的代码:
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)
}
我能做什么?为什么会出现错误?
为什么每次在createBorderLine中都为第0行和第0部分创建索引路径。只需在createBorderLine中传递单元格引用
self.createBorderLine(editProductCell)
并在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
在Controller Class中创建,而应该createBorderLine
在EditProductCell
class中放置。并直接通过EditProductCell对象ref调用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句