简体   繁体   English

在表视图中填充数据,swift2

[英]Fill tableviews in view with data, swift2

I am new to Swift and iOS development and am currently working on an app, that displays contact details. 我是Swift和iOS开发的新手,目前正在开发一个显示联系方式的应用程序。 Now this app has a ViewController, that displays the contact details. 现在,此应用程序具有一个ViewController,可显示联系人详细信息。 Int he view, that belongs to that ViewController I have two table views, that should display additional information. 在属于那个ViewController的视图中,我有两个表视图,应该显示其他信息。 To be able to address those table views I made the view controller a delegate of 为了能够处理这些表视图,我将视图控制器委托为

UITableViewDataSource, UITableViewDelegate

and added the following methods: 并添加了以下方法:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print(tableView)
    print(self.hobbiesTableView)
    if(tableView == self.hobbiesTableView){
        return card.hobbies.count
    } else {
        return card.friends.count
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("hobbycell", forIndexPath: indexPath) as UITableViewCell

    let row = indexPath.row

    if(tableView == self.hobbiesTableView){
        cell.textLabel?.text = card.hobbies[row]
    } else {
        cell.textLabel?.text = card.friends[row].vorname
    }

    return cell
}

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true;
}

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let editAction = UITableViewRowAction(style: .Normal, title: "Edit") { (rowAction:UITableViewRowAction, indexPath:NSIndexPath) -> Void in
        //TODO: edit the row at indexPath here
    }
    editAction.backgroundColor = UIColor.blueColor()


    let deleteAction = UITableViewRowAction(style: .Normal, title: "Delete") { (rowAction:UITableViewRowAction, indexPath:NSIndexPath) -> Void in
        //TODO: Delete the row at indexPath here
    }
    deleteAction.backgroundColor = UIColor.redColor()

    return [editAction,deleteAction]
}

However none of these methods get ever called. 但是这些方法都没有被调用过。 First question thus is what am I missing? 因此,第一个问题是我想念什么? Second question: Would the way I enabled editing allow for the removal of entries in those table views? 第二个问题:启用编辑的方式是否允许删除那些表视图中的条目?

您需要将表视图的委托和dataSource属性设置为视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM