2017-07-30 64 views
0

我想创建一个自定义表格视图,但偶然发现每个代码。截至目前,我在下面有这个。这很混乱,可能是错误的,但有人可以帮忙吗?自定义TableView错误

另外,我不断收到一个错误代码

unexpectedly found nil while unwrapping optional value

import UIKit 

class tableOutlineViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var tableView: UITableView! 


    var names = ["Max", "Bill", "Reagan", "Mikayla", "Jessie", "Sierra", "Jeff", "Erik", "Landon"] 
    var numbers = ["35", "33", "29", "27", "25", "23", "19", "15", "11"] 
    var photo = [UIImage(named: "Person1"), UIImage(named: "Person2"), UIImage(named: "Groceries"), UIImage(named: "Person3"), UIImage(named: "Person4"), UIImage(named: "Person5"), UIImage(named: "Person6"), UIImage(named: "Person7")] 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return names.count 
    } 

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = self.tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell 

     cell.images.image = photo[indexPath.row] 
     cell.name.text = names[indexPath.row] 
     cell.number.text = numbers[indexPath.row] 


     return cell 
    } 

} 
+0

你可以添加你CustomCell类的代码在这里 –

回答

0

的错误是在下面的行,因为它是无法强制展开CustomCell

let cell = self.tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell 

正如你可能使用Nib对于您的自定义单元格,您应该首先在viewDidLoad中注册。

let nib = UINib(nibName: YOUR_CUSTOM_CELL_NIB_NAME, bundle: nil) 
tableView.register(nib, forCellReuseIdentifier: "CustomCell") 
0

好像你忘了在自定义单元格的Xib文件的属性检查器中定义标识符(CustomCell)。所以,当你做武力解包时,你会得到零。同时避免强行拆包以避免碰撞。重构了代码: -

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
guard let cell = self.tableView.dequeueReusableCell(withIdentifier: "CustomCell") as? CustomCell else {return UITableViewCell()} 
    cell.images.image = photo[indexPath.row] 
    cell.name.text = names[indexPath.row] 
    cell.number.text = numbers[indexPath.row] 
    return cell 
} 
0

冷杉所有请不要填充您的tableview与多个数组。这些阵列很容易失去同步并可能导致崩溃。理想情况下,您应该创建一个名称,年龄和照片作为其元素的课程struct。然后,你应该有一个结构类的数组来填充你的表视图。

这里以及你的名字数组中,你有9个元素,而你的图像数组有8个元素。这会导致您的应用程序崩溃,因为indexpath方法中的行的单元无法找到第9个图像。

cell.images.image = photo[indexPath.row] 
0

unexpectedly found nil while unwrapping optional value

在线

let cell = self.tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell 

因为,UITableViewCell需求Identifier。在UITableView上选择UITableViewCell,然后转到Utilities -> Attributes Inspecter -> Table View Cell -> identifier,并将标识符设置为CustomCell,如下面的屏幕截图所示。

enter image description here

错误2:

你会得到,

fatal error: Index out of range

上线

cell.imageView?.image = photo[indexPath.row] 

因为,names.count != photo.count // 9 != 8

确保已添加datasource & delegate

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.tableView.dataSource = self 
    self.tableView.delegate = self 

} 
0

所以我把一些在咨询y'alls的,当我运行下面的代码,我得到的零个错误,但在模拟器,它会显示一个空的tableView我放在上面的tableView的图像是另一个图像从另一个视图控制器。我不知道它是代码还是其他的东西。

let nib = UINib(nibName: "CustomCell", bundle: nil) 
    tableView.register(nib, forCellReuseIdentifier: "Cell") 
} 

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return names.count 
} 

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    guard let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell") as? CustomCell else {return UITableViewCell()} 
    cell.images.image = photo[indexPath.row] 
    cell.name.text = names[indexPath.row] 
    cell.numbers.text = numbers[indexPath.row] 
    return cell 

和我CustomCell

进口的UIKit

类CustomCell:{UITableViewCell的

@IBOutlet weak var images: UIImageView! 
@IBOutlet weak var name: UILabel! 
@IBOutlet weak var numbers: UILabel! 



override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

}