2017-07-30 66 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 
} 

}