2016-01-06 37 views
0

我是Swift和iOS開發新手,目前正在開發一個顯示聯繫人詳細信息的應用程序。現在這個應用程序有一個ViewController,顯示聯繫人詳細信息。他認爲,屬於那個ViewController我有兩個表視圖,應該顯示額外的信息。 爲了能夠解決我所做的視圖控制器的用數據填充tableviews,swift2

UITableViewDataSource, UITableViewDelegate 

的委託,並添加下面的方法的意見表:

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] 
} 

但是這些方法都沒有得到過調用。因此,第一個問題是我錯過了什麼?第二個問題:我啓用編輯的方式是否允許刪除這些表視圖中的條目?

回答

1

您需要將表視圖的委託和數據源屬性設置爲您的視圖控制器。