gpt4 book ai didi

swift - 快速调试 : I not find error in cod

转载 作者:行者123 更新时间:2023-11-30 13:11:08 24 4
gpt4 key购买 nike

---------- ERROR IS: 2016-07-31 11:16:36.116 t6_TableDemo[900:25064] -[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7feaa273c520 2016-07-31 11:16:36.326 t6_TableDemo[900:25064] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7feaa273c520' * First throw call stack: (

<小时/>

代码是:

{
//

// ViewController.swift

// t6_TableDemo

//

// Created by dvd shd on 7/29/16.

// Copyright © 2016 zohur. All rights reserved.

//


import UIKit


class ViewController: UIViewController,UITableViewDataSource {


let devcourses=[("ios App","simon All" ),("ios 8","Biotifull"),("Win 7"," it is good"), ("Windows 8","is a bad"), ("Linux 7","is better"), ("ios 9","verry very Good"),("Xcode 7","it is better")]



let webcourses=[("rghamsar","akhavan" ),("azmadar","bakhshali"),("rezapedar","shokrollahzadeh"), ("mohammadhoseinfarzand","shokrollahzadeh"), ("zhkhahar","shokrollahzadeh"), ("pakhahar","shokrollahzadeh"),("khodam","it is better")]






func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return 2

}



func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {





if section == 0 {

return devcourses.count

} else

{

return webcourses.count

}



}



func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)as UITableViewCell









if indexPath.section == 0 {

let (courseTitle,courseAuthor)=devcourses[indexPath.row]

cell.textLabel?.text=courseTitle

cell.detailTextLabel?.text = courseAuthor





}

else {

let (courseTitle,courseAuthor)=webcourses[indexPath.row]

cell.textLabel?.text=courseAuthor

cell.detailTextLabel?.text=courseTitle

}

// Retrieve in image

var myImage = UIImage(named: "CellIcon")

cell.imageView?.image=myImage



return cell

}





func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

if section == 0 {

return "Developer Courses"

} else

{

return "Web Courses"

}

}







override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

}


override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}



}



}

最佳答案

您在项目中错过了两件事。

1:为您的tableView创建一个@IBOutlet

2:在您的 viewDidLoad 方法中符合其 dataSource ,您的结果将是:

enter image description here

完整的代码为:

import UIKit

class ViewController: UIViewController,UITableViewDataSource {


let devcourses=[("ios App","simon All" ),("ios 8","Biotifull"),("Win 7"," it is good"), ("Windows 8","is a bad"), ("Linux 7","is better"), ("ios 9","verry very Good"),("Xcode 7","it is better")]

let webcourses=[("rghamsar","akhavan" ),("azmadar","bakhshali"),("rezapedar","shokrollahzadeh"), ("mohammadhoseinfarzand","shokrollahzadeh"), ("zhkhahar","shokrollahzadeh"), ("pakhahar","shokrollahzadeh"),("khodam","it is better")]

@IBOutlet weak var tblView: UITableView!

override func viewDidLoad() {

super.viewDidLoad()

tblView.dataSource = self
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return 2

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if section == 0 {

return devcourses.count

} else {

return webcourses.count

}
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)as UITableViewCell

if indexPath.section == 0 {

let (courseTitle,courseAuthor)=devcourses[indexPath.row]

cell.textLabel?.text=courseTitle

cell.detailTextLabel?.text = courseAuthor

}

else {

let (courseTitle,courseAuthor)=webcourses[indexPath.row]

cell.textLabel?.text=courseAuthor

cell.detailTextLabel?.text=courseTitle

}

// Retrieve in image

let myImage = UIImage(named: "CellIcon")

cell.imageView?.image=myImage

return cell

}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

if section == 0 {

return "Developer Courses"

} else {

return "Web Courses"

}
}
}

检查Demo Project了解更多信息。

关于swift - 快速调试 : I not find error in cod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38715454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com