ios - 快速将 UITableView 添加到 View Controller

标签 ios xcode uitableview swift

我不明白我做错了什么, 我正在尝试在导航栏上创建一个添加按钮,这样当您单击它时,页面底部会打开一个小表格 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/

相关文章:

php - iTunes 最近添加的 XML 未显示更新结果

ios - iOS 上 UIImage 的运动模糊效果

swift - 编辑- UITableView 单元格按钮操作委托(delegate)- Swift 4

ios - 如何使用 texfield 扩展来限制用户输入特殊字符?

ios - 第一部分标题固定在顶部的 UITableView

iphone - UITableViewscrollToRowAtIndexPath

objective-c - iOS:如何正确访问 UITableViewCell 的 contentView 上的自定义标签?

iOS:在 AV 视频上绘图,然后将绘图保存在视频文件中

ios - 不要在初始化方法和 dealloc 中使用访问器方法

iphone - 我怎样才能让 NSTimer 在用户使用应用程序的整个过程中一直运行?