Я обнаружил ошибку в swift
при попытке создать tableview
, составленный из пользовательского cells
, в зависимости от набора условий.Ошибка при создании tableview с несколькими пользовательскими ячейками
Вот мой код:
var tableData: [String] = []
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
// number of rows in table view
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
// create a cell for each table view row
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let phonenocell:MyCustomCell = self.tableView.dequeueReusableCellWithIdentifier("phonecell", forIndexPath: indexPath) as! MyCustomCell
let pincell:SocialCell = self.tableView.dequeueReusableCellWithIdentifier("socialcell", forIndexPath: indexPath) as! SocialCell
let fbcell:FacebookCell = self.tableView.dequeueReusableCellWithIdentifier("facebookcell", forIndexPath: indexPath) as! FacebookCell
let snapcell:SnapchatCell = self.tableView.dequeueReusableCellWithIdentifier("snapchatcell", forIndexPath: indexPath) as! SnapchatCell
let twitcell:TwitterCell = self.tableView.dequeueReusableCellWithIdentifier("twittercell", forIndexPath: indexPath) as! TwitterCell
let instacell:InstagramCell = self.tableView.dequeueReusableCellWithIdentifier("instagramcell", forIndexPath: indexPath) as! InstagramCell
if tableData.contains("Number") {
return phonenocell
}
if tableData.contains("Social") {
return pincell
}
if tableData.contains("Facebook") {
return fbcell
}
if tableData.contains("Snapchat") {
return snapcell
}
if tableData.contains("Twitter") {
return twitcell
}
if tableData.contains("Instagram") {
return instacell
}
}
При попытке построить и запустить я получаю построить не удалось с помощью следующей ошибки:
"Missing Return in a function expected to return 'UITableViewCell'
Я был снова и снова мой код, но я честно не вижу, где я ошибаюсь ...
Любая помощь была бы принята с благодарностью!
Что вы вернетесь, когда последний 'если '-' if tableData.contains ("Instagram") 'fail? – OOPer
При каждом вызове 'cellForRowAtIndexPath' вы создаете 6 ячеек, 5 для мусора и только один для использования. Это очень неэффективно и дорого. – vadian
Можете ли вы показать, как выглядит ваш tableData..and @vadian is right – Dravidian