Swift UITableView数据源
我收到2个错误 第一个我很困惑,因为所有的东西都在{}里面,所以我不确定我会错在哪里 类型“StoriesViewController”不符合协议 “UITableViewDataSource”单击修复添加: func tableView_utableview:UITableView,cellForRowAt indexath: IndexPath->UITableViewCell{ } 第二个错误 “dequeueReusableCellWithIdentifier”的使用不明确 我将主情节提要中的标识符storyCell分配给表格单元格 完整代码:Swift UITableView数据源,swift,xcode,uitableview,tablecell,Swift,Xcode,Uitableview,Tablecell,我收到2个错误 第一个我很困惑,因为所有的东西都在{}里面,所以我不确定我会错在哪里 类型“StoriesViewController”不符合协议 “UITableViewDataSource”单击修复添加: func tableView_utableview:UITableView,cellForRowAt indexath: IndexPath->UITableViewCell{ } 第二个错误 “dequeueReusableCellWithIdentifier”的使用不明确 我将主情节提
// creating a custom color
extension UIColor {
static var darkmodeGray = UIColor.init(red: 41/255, green: 42/255, blue: 47/255, alpha: 1)
}
class StoriesViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
<#code#>
}
@IBOutlet weak var tableView: UITableView!
let storySelection = [("WeekOne"),("WeekTwo"),("WeekThree"),("WeekFour"),("WeekFive"),("WeekSix")]
let storySelectionImages = [UIImage(named: "WeekOne"), UIImage(named: "WeekTwo"), UIImage(named: "WeekThree"), UIImage(named: "WeekFour"), UIImage(named: "WeekFive"), UIImage(named: "WeekSix")]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor .darkmodeGray
}
//Part One
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
//Part Two
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return storySelection.count
}
//Part Three
private func tableView(tableView :UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCellWithIdentifier("storyCell", forIndexPath: IndexPath) as! TableViewCell
cell.CoverArt.image=self.storySelectionImages[indexPath .row]
return cell
}
}
你的密码是Swift 2!代码 更新方法签名的常用方法是注释掉它,重新键入它并使用代码完成 或者读一下报纸,这总是个好主意 这三种方法是numberOfSections,如果只有一个节,则可以省略
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return storySelection.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "storyCell", for: indexPath) as! TableViewCell
cell.CoverArt.image = self.storySelectionImages[indexPath.row]
return cell
}
我建议重写你的方法,并利用Xcode的代码补全来确保你正确地输入它们。你的建议帮助修复了我收到的第二个错误。在使用代码完成时,我仍然对第一部分感到困惑。它需要什么代码?func tableView_utableview:UITableView,cellForRowAt indexath:indexPath->UITableView单元格{}我重写了这些方法//第二部分func tableView\uTableView:UITableView,numberOfRowsInSection部分:Int->Int{return storySelection.count}//第三部分func tableviewtableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath->UITableViewCell{let cell=tableView.dequeueReusableCellwithIdentifier:storyCell,for:indexPath as indexPath as!TableViewCell return cell}我仍然收到第一个错误。请在类的开头删除cellForRowAt方法,并在第三部分中完全使用我建议的代码。您的签名仍然错误。