Swift 子类化 UITableViewDataSource EXC

编程入门 行业动态 更新时间:2024-10-26 10:26:40
本文介绍了Swift 子类化 UITableViewDataSource EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试子类化我的 UITableViewDatasource,但是我的应用程序因 EXC_BAD_ACCESS 崩溃.没有解释没有错误消息只是崩溃.我的数据源的排序版本如下所示.

I am trying To subclass My UITableViewDatasource, However My App Crashes with EXC_BAD_ACCESS. No explanations no error messages just crashes. A Sort Version of my DataSource Looks Like This.

import UIKit class DataSource :NSObject, UITableViewDataSource{ var tableView:UITableView let CellIdentifier = "Cell" init(tableView : UITableView) { println("Data Source") self.tableView = tableView super.init() self.tableView.dataSource = self } //:MARK UITableViewDataSource //-----------------------------------------------------------------------------------------// // Number Of Rows In Tableview //-----------------------------------------------------------------------------------------// func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int { return 10 } //-----------------------------------------------------------------------------------------// // Cell For Row At Index Path //-----------------------------------------------------------------------------------------// func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { let cell = self.tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath) as UITableViewCell cell.textLabel.text = "Title of Row: #\(indexPath.row)" return cell } }

如您所见,没有什么特别的,这段代码在 Playground 中运行良好

As You can see There is nothing special, this code works fine in Playground

下面是我对 DataSource 的调用

Below is my call to DataSource

import UIKit class ListEntriesViewController :UITableViewController{ override func viewDidLoad() { println("View Did Load") var data = DataSource(tableView: self.tableView) self.tableView.dataSource = data } }

我错过了什么,为什么此代码在 Playground 中有效,但在我的应用中无效.

What Am I missing, Why this code works works in Playground but not in my App.

谢谢

推荐答案

表视图不保留数据源.在 viewDidLoad 之后立即销毁该对象.您需要将其存储在属性中.

The datasource is not retained by the table view. The object is destroyed immediately after viewDidLoad. You need to store it in a property.

另请注意,UITableViewDataSource 是一个协议,而不是一个类.因此它不能被子类化.

Also note that UITableViewDataSource is a protocol, not a class. Hence it can't be subclassed.

更多推荐

Swift 子类化 UITableViewDataSource EXC

本文发布于:2023-10-07 14:44:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469670.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子类   Swift   EXC   UITableViewDataSource

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!