用数据填充tableviews,swift2
问题描述:
我是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]
}
但是这些方法都没有得到过调用。因此,第一个问题是我错过了什么?第二个问题:我启用编辑的方式是否允许删除这些表视图中的条目?
答
您需要将表视图的委托和数据源属性设置为您的视图控制器。