我不明白我做错了什么, 我正在尝试在导航栏上创建一个添加按钮,这样当您单击它时,页面底部会打开一个小表格 View ,用户可以选择要添加的内容(照片、视频等) 我将 tableView 与我的 cocoa touch 类连接起来,您可以在此处看到:
class addTable: UITableView {
let addObjects = ["Add Photo","Add Video","Add Link", "Add Sound Record", "Add Sound Track"]
override func cellForRowAtIndexPath(indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:UITableViewCell = dequeueReusableCellWithIdentifier("addItem", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = addObjects[indexPath.row]
return cell
}
override func numberOfRowsInSection(section: Int) -> Int {
return addObjects.count
}
override func numberOfSections() -> Int {
return 1
}
}
我真的不明白为什么它不起作用,表中的行数甚至不等于 addObjects.count
我真的希望你能理解我的问题,谢谢你们!!
最佳答案
您已经子类化了 UITableView
,但这可能不是必需的。
您希望此类成为标准 TableView 的数据源。
class addTable : NSObject, UITableViewDataSource{
func cellForRowAtIndexPath(tableView:UITableView!, indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("addItem", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = addObjects[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return addObjects.count
}
func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1
}
}
然后,在您的 View Controller 中,您可以将此类的实例设置为您的 tableview 的数据源 -
self.myTableView.dataSource=addTable();
如何分配和初始化实际的 tableview 取决于您 - Storyboard或通过代码,但它可以只是一个直接的 tableview。
关于ios - 快速将 UITableView 添加到 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25466671/