我正在尝试设置一个待办事项列表,该列表从第二个 View Controller 获取任务并将其添加到第一个 View Controller 的 TableView 中。我一直在关注一个流行的教程,评论的支持是有限的。就此问题仔细查看了其他教程和帖子,但没有一个使用相同的形式,而且对于 swift 来说只是相当新的人无法找到他们拥有的链接。
错误出在第二个 View Controller 上的 UIButton 上,它触发文本移动到第一个 View ,第一个 View 是一个存储文本的表。
错误发生在应用内,是这一行的断点。
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier: "test")
错误信息只有 lldb,在 bt 中看不到任何有用信息
(lldb) bt
* thread #1: tid = 0x23e9e, 0x00000001044ed5e3 ToDo Task`ToDo_Task.FirstViewController.tableView (tableView=0x00007f9721851600, indexPath=0xc000000000000016, self=0x00007f9720f6e7f0)(ObjectiveC.UITableView, cellForRowAtIndexPath : ObjectiveC.NSIndexPath) -> ObjectiveC.UITableViewCell + 35 at FirstViewController.swift:27, queue = 'com.apple.main-thread', stop reason = breakpoint 3.1 4.1
* frame #0: 0x00000001044ed5e3 ToDo Task`ToDo_Task.FirstViewController.tableView (tableView=0x00007f9721851600, indexPath=0xc000000000000016, self=0x00007f9720f6e7f0)(ObjectiveC.UITableView, cellForRowAtIndexPath : ObjectiveC.NSIndexPath) -> ObjectiveC.UITableViewCell + 35 at FirstViewController.swift:27
frame #1: 0x00000001044ee2af ToDo Task`@objc ToDo_Task.FirstViewController.tableView (ToDo_Task.FirstViewController)(ObjectiveC.UITableView, cellForRowAtIndexPath : ObjectiveC.NSIndexPath) -> ObjectiveC.UITableViewCell + 79 at FirstViewController.swift:0
frame #2: 0x00000001050594b3 UIKit`-[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
frame #3: 0x0000000105038fb1 UIKit`-[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
frame #4: 0x000000010504ee3c UIKit`-[UITableView layoutSubviews] + 213
frame #5: 0x0000000104fdb973 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
frame #6: 0x0000000108ed9de8 QuartzCore`-[CALayer layoutSublayers] + 150
frame #7: 0x0000000108ecea0e QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 380
frame #8: 0x0000000108ece87e QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 24
frame #9: 0x0000000108e3c63e QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 242
frame #10: 0x0000000108e3d74a QuartzCore`CA::Transaction::commit() + 390
frame #11: 0x0000000104f5f14d UIKit`_UIApplicationHandleEventQueue + 2035
frame #12: 0x000000010460c551 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #13: 0x000000010460241d CoreFoundation`__CFRunLoopDoSources0 + 269
frame #14: 0x0000000104601a54 CoreFoundation`__CFRunLoopRun + 868
frame #15: 0x0000000104601486 CoreFoundation`CFRunLoopRunSpecific + 470
frame #16: 0x00000001087cd9f0 GraphicsServices`GSEventRunModal + 161
frame #17: 0x0000000104f62420 UIKit`UIApplicationMain + 1282
frame #18: 0x00000001044ebf1e ToDo Task`top_level_code + 78 at AppDelegate.swift:12
frame #19: 0x00000001044ebf5a ToDo Task`main + 42 at AppDelegate.swift:0
frame #20: 0x00000001069f4145 libdyld.dylib`start + 1
(lldb)
错误在这个函数中
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier: "test")
cell.textLabel?.text = taskMgr.tasks[indexPath.row].name
cell.detailTextLabel?.text = taskMgr.tasks[indexPath.row].desc
return cell
}
它使用新的 cocoa 文件中的 taskMgr
var taskMgr :TaskManager = TaskManager()
struct task{
var name = "un-named"
var desc = "un-described"
}
class TaskManager: NSObject {
var tasks = [task]()
func addTask (name: String, desc: String){
tasks.append(task(name: name,desc: desc))
}
}
不确定是否有解决这些问题的简单方法,正如我所说,我是 swift 的新手,不想问任何太简单的问题,但据我所知无法做任何事情来解决它。
最佳答案
使用 tableView.dequeueReusableCellWithIdentifier
而不是在 cellForRowAtIndexPath
中创建 UITableViewCell
的实例
let cell = tableView.dequeueReusableCellWithIdentifier("Test",
forIndexPath: indexPath) as UITableViewCell
如果您使用的是自定义 UITableViewCell
,请在 viewDidLoad
中使用它之前注册您的 UITableViewCell
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(TestCell, forCellReuseIdentifier: "TestCell")
}
关于xcode - UITableViewCell 从其他 View Controller 上的输入传输运行时断点(lldb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28028902/