CustomCell 为空或导致 SIGABRT 错误 [W/Code]
CustomCell is empty or results in SIGABRT error [W/Code]
我正在尝试创建一个视频播放器,它在 UITableView 中显示视频的缩略图、标题和说明。当用户单击原始文件时,它将打开一个带有 WebView 的新视图,将在其中播放视频。
目前我仍在尝试设置自定义单元格。我在 class AppDelegate: UIResponder, UIApplicationDelegate {
行的 AppDelegate.swift
文件中遇到错误,错误是:Thread 1: signal SIGABRT
。错误日志在这个post.
的末尾
在 Main.storyboard
中,我添加了一个带有一个原型单元格的 TableView(我使用 delegate
和 dataSource
连接到 ViewController),我在其中添加了一个ImageView 和两个标签。我还创建了一个名为 CustomTableViewCell.swift
的 Swift 文件。我将 View Controller
自定义 Class 设置为 ViewController,将单元格的标识符设置为 customCell
,将样式设置为 "Custom",将 class 设置为 CustomTableViewCell
。
在 ViewController.swift
中,我创建了一个包含视频信息的数组,并设置了 table 函数。
我也尝试了其他方法来使用我在 Stack 上找到的解决方案编写代码,但我也遇到了同样的错误,或者最多我得到一个 table 和 3 个空单元格。代码在下面。有人可以告诉我如何让这个自定义单元格工作吗?
提前致谢!
ViewController.swift
//
// ViewController.swift
// Cycloid
//
// Created by smbss on 10/11/15.
// Copyright © 2015 smbss. All rights reserved.
//
import UIKit
var videos = [Dictionary<String,String>()]
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
if videos.count == 1 {
videos.removeAtIndex(0)
// Check if places != to 1 and not 0 because we created the dictionary places as a global var outside the ViewController and that can not be empty.
}
videos.append(["title":"Video 1","description":"Description 1","thumbnail":"http://img.youtube.com/vi/6oXTFb-52Zc/0.jpg","video":"https://www.youtube.com/watch?v=6oXTFb-52Zc"])
videos.append(["title":"Video 2","description":"Description 2","thumbnail":"http://img.youtube.com/vi/dkwuCeCQVg8/0.jpg","video":"https://www.youtube.com/watch?v=dkwuCeCQVg8"])
videos.append(["title":"Video 3","description":"Description 3","thumbnail":"http://img.youtube.com/vi/6Zf_70xM1Cw/0.jpg","video":"https://www.youtube.com/watch?v=6Zf_70xM1Cw"])
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return videos.count
}
/*
//If I use this there is no SIGABRT error but the cells are empty.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier: "customCell")
return cell
*/
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell
let url = NSURL(string: videos[indexPath.row]["thumbnail"]!)
let data = NSData(contentsOfURL: url!)
cell.img?.image = UIImage(data: data!)
cell.ttl?.text = videos[indexPath.row]["title"]
cell.dsc?.text = videos[indexPath.row]["description"]
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
CustomTableView.swift
//
// CustomTableViewCell.swift
// Cycloid
//
// import Foundation
import UIKit
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var img: UIImageView!
@IBOutlet weak var ttl: UILabel!
@IBOutlet weak var dsc: UILabel!
// I also tried deleting the code below.
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
错误日志:
2015-11-10 18:00:23.069 Cycloid[5922:398570] Unknown class CustomTableViewCell in Interface Builder file.
2015-11-10 18:00:23.074 Cycloid[5922:398570] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x7f96da6a0030> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key dsc.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010d55ef45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010f282deb objc_exception_throw + 48
2 CoreFoundation 0x000000010d55eb89 -[NSException raise] + 9
3 Foundation 0x000000010d927a6b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
4 UIKit 0x000000010de2f96f -[UIView(CALayerDelegate) setValue:forKey:] + 173
5 UIKit 0x000000010e133a71 -[UIRuntimeOutletConnection connect] + 109
6 CoreFoundation 0x000000010d49fa80 -[NSArray makeObjectsPerformSelector:] + 224
7 UIKit 0x000000010e132454 -[UINib instantiateWithOwner:options:] + 1864
8 UIKit 0x000000010debe177 -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 388
9 UIKit 0x000000010debe549 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 71
10 Cycloid 0x000000010d37a2dd _TFC7Cycloid14ViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 125
11 Cycloid 0x000000010d37ac7f _TToFC7Cycloid14ViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79
12 UIKit 0x000000010ded0e2a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 782
13 UIKit 0x000000010ded0f3f -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
14 UIKit 0x000000010dea6307 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3187
15 UIKit 0x000000010ded9d1c -[UITableView _performWithCachedTraitCollection:] + 92
16 UIKit 0x000000010dec1884 -[UITableView layoutSubviews] + 223
17 UIKit 0x000000010de2fe40 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
18 QuartzCore 0x00000001122c559a -[CALayer layoutSublayers] + 146
19 QuartzCore 0x00000001122b9e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
20 QuartzCore 0x00000001122b9cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
21 QuartzCore 0x00000001122ae475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
22 QuartzCore 0x00000001122dbc0a _ZN2CA11Transaction6commitEv + 486
23 QuartzCore 0x00000001122dc37c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
24 CoreFoundation 0x000000010d48a947 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
25 CoreFoundation 0x000000010d48a8b7 __CFRunLoopDoObservers + 391
26 CoreFoundation 0x000000010d47fe2c CFRunLoopRunSpecific + 524
27 UIKit 0x000000010dd764f5 -[UIApplication _run] + 402
28 UIKit 0x000000010dd7b30d UIApplicationMain + 171
29 Cycloid 0x000000010d37be0d main + 109
30 libdyld.dylib 0x000000010fd8a92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
转到你的故事板,select 原型单元格,删除你的自定义 class(所以这又是一个常规的 UITableViewCell
),构建一次项目,然后设置自定义 class 返回 CustomTableViewCell
并重试。
我正在尝试创建一个视频播放器,它在 UITableView 中显示视频的缩略图、标题和说明。当用户单击原始文件时,它将打开一个带有 WebView 的新视图,将在其中播放视频。
目前我仍在尝试设置自定义单元格。我在 class AppDelegate: UIResponder, UIApplicationDelegate {
行的 AppDelegate.swift
文件中遇到错误,错误是:Thread 1: signal SIGABRT
。错误日志在这个post.
在 Main.storyboard
中,我添加了一个带有一个原型单元格的 TableView(我使用 delegate
和 dataSource
连接到 ViewController),我在其中添加了一个ImageView 和两个标签。我还创建了一个名为 CustomTableViewCell.swift
的 Swift 文件。我将 View Controller
自定义 Class 设置为 ViewController,将单元格的标识符设置为 customCell
,将样式设置为 "Custom",将 class 设置为 CustomTableViewCell
。
在 ViewController.swift
中,我创建了一个包含视频信息的数组,并设置了 table 函数。
我也尝试了其他方法来使用我在 Stack 上找到的解决方案编写代码,但我也遇到了同样的错误,或者最多我得到一个 table 和 3 个空单元格。代码在下面。有人可以告诉我如何让这个自定义单元格工作吗?
提前致谢!
ViewController.swift
//
// ViewController.swift
// Cycloid
//
// Created by smbss on 10/11/15.
// Copyright © 2015 smbss. All rights reserved.
//
import UIKit
var videos = [Dictionary<String,String>()]
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
if videos.count == 1 {
videos.removeAtIndex(0)
// Check if places != to 1 and not 0 because we created the dictionary places as a global var outside the ViewController and that can not be empty.
}
videos.append(["title":"Video 1","description":"Description 1","thumbnail":"http://img.youtube.com/vi/6oXTFb-52Zc/0.jpg","video":"https://www.youtube.com/watch?v=6oXTFb-52Zc"])
videos.append(["title":"Video 2","description":"Description 2","thumbnail":"http://img.youtube.com/vi/dkwuCeCQVg8/0.jpg","video":"https://www.youtube.com/watch?v=dkwuCeCQVg8"])
videos.append(["title":"Video 3","description":"Description 3","thumbnail":"http://img.youtube.com/vi/6Zf_70xM1Cw/0.jpg","video":"https://www.youtube.com/watch?v=6Zf_70xM1Cw"])
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return videos.count
}
/*
//If I use this there is no SIGABRT error but the cells are empty.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier: "customCell")
return cell
*/
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell
let url = NSURL(string: videos[indexPath.row]["thumbnail"]!)
let data = NSData(contentsOfURL: url!)
cell.img?.image = UIImage(data: data!)
cell.ttl?.text = videos[indexPath.row]["title"]
cell.dsc?.text = videos[indexPath.row]["description"]
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
CustomTableView.swift
//
// CustomTableViewCell.swift
// Cycloid
//
// import Foundation
import UIKit
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var img: UIImageView!
@IBOutlet weak var ttl: UILabel!
@IBOutlet weak var dsc: UILabel!
// I also tried deleting the code below.
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
错误日志:
2015-11-10 18:00:23.069 Cycloid[5922:398570] Unknown class CustomTableViewCell in Interface Builder file.
2015-11-10 18:00:23.074 Cycloid[5922:398570] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x7f96da6a0030> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key dsc.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010d55ef45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010f282deb objc_exception_throw + 48
2 CoreFoundation 0x000000010d55eb89 -[NSException raise] + 9
3 Foundation 0x000000010d927a6b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
4 UIKit 0x000000010de2f96f -[UIView(CALayerDelegate) setValue:forKey:] + 173
5 UIKit 0x000000010e133a71 -[UIRuntimeOutletConnection connect] + 109
6 CoreFoundation 0x000000010d49fa80 -[NSArray makeObjectsPerformSelector:] + 224
7 UIKit 0x000000010e132454 -[UINib instantiateWithOwner:options:] + 1864
8 UIKit 0x000000010debe177 -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 388
9 UIKit 0x000000010debe549 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 71
10 Cycloid 0x000000010d37a2dd _TFC7Cycloid14ViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 125
11 Cycloid 0x000000010d37ac7f _TToFC7Cycloid14ViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79
12 UIKit 0x000000010ded0e2a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 782
13 UIKit 0x000000010ded0f3f -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
14 UIKit 0x000000010dea6307 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3187
15 UIKit 0x000000010ded9d1c -[UITableView _performWithCachedTraitCollection:] + 92
16 UIKit 0x000000010dec1884 -[UITableView layoutSubviews] + 223
17 UIKit 0x000000010de2fe40 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
18 QuartzCore 0x00000001122c559a -[CALayer layoutSublayers] + 146
19 QuartzCore 0x00000001122b9e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
20 QuartzCore 0x00000001122b9cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
21 QuartzCore 0x00000001122ae475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
22 QuartzCore 0x00000001122dbc0a _ZN2CA11Transaction6commitEv + 486
23 QuartzCore 0x00000001122dc37c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
24 CoreFoundation 0x000000010d48a947 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
25 CoreFoundation 0x000000010d48a8b7 __CFRunLoopDoObservers + 391
26 CoreFoundation 0x000000010d47fe2c CFRunLoopRunSpecific + 524
27 UIKit 0x000000010dd764f5 -[UIApplication _run] + 402
28 UIKit 0x000000010dd7b30d UIApplicationMain + 171
29 Cycloid 0x000000010d37be0d main + 109
30 libdyld.dylib 0x000000010fd8a92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
转到你的故事板,select 原型单元格,删除你的自定义 class(所以这又是一个常规的 UITableViewCell
),构建一次项目,然后设置自定义 class 返回 CustomTableViewCell
并重试。