快速调试:我在 cod 中没有发现错误
Posted
技术标签:
【中文标题】快速调试:我在 cod 中没有发现错误【英文标题】:debug in swift: I not find error in cod 【发布时间】:2016-08-02 08:31:35 【问题描述】:--------- 错误是:2016-07-31 11:16:36.116 t6_TableDemo[900:25064] -[UIView tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例 0x7feaa273c520 2016-07 -31 11:16:36.326 t6_TableDemo[900:25064] * 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[UIView tableView:numberOfRowsInSection:]: 无法识别的选择器发送到 实例 0x7feaa273c520' * 首先抛出调用栈:(
代码是:
//
// 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】:您在项目中遗漏了 2 件事。
1:为您的tableView
创建一个@IBOutlet
。
2:在您的viewDidLoad
方法中符合其dataSource
,您的结果将是:
完整的代码是:
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了解更多信息。
【讨论】:
以上是关于快速调试:我在 cod 中没有发现错误的主要内容,如果未能解决你的问题,请参考以下文章
致命错误:在快速 Json 解析中展开可选值时意外发现 nil