快速的文字游戏,第二部分

在本文的第一部分中,我介绍了一些基本的代码来生成UITableView,其中的重要内容是拖放简短论文的主要内容。 当然,如果没有可编辑的表格视图,它的用途将非常有限,所以我认为我要做第二部分。

UITableView的最终目标应像这样,可以内联编辑,插入和删除条目。

首先,我们需要设置一个自定义UITableCell类,其中包含一个UITextField,代码如下所示。

类myTableViewCell:UITableViewCell,UITextFieldDelegate { 
 让标签:UITextField 
var listItems:myItem? {
didSet {
label.text = listItems!.text
}
}
 所需的init(coder aDecoder:NSCoder){ 
fatalError(“ init(coder :)尚未实现”)
}
 覆盖init(样式:UITableViewCellStyle,复用标识符:字符串?){ 
标签= UITextField(框架:CGRect.null)
label.textColor = UIColor.black
label.font = UIFont.systemFont(ofSize:16)
super.init(样式:样式,reuseIdentifier:reuseIdentifier)
label.delegate =自我
label.contentVerticalAlignment = UIControlContentVerticalAlignment.center
  addSubview(label) 
}
 让leftMarginForLabel:CGFloat = 16.0 
 覆盖func layoutSubviews(){ 
super.layoutSubviews()
  label.frame = CGRect(x:leftMarginForLabel,y:0,width:bounds.size.width — leftMarginForLabel,高度:bounds.size.height) 
  } 
  func textFieldShouldReturn(_ textField:UITextField)-> Bool { 
textField.resignFirstResponder()
返回假
}
  func textFieldShouldEndEditing(_ textField:UITextField)-> Bool { 
如果listItems!= nil {
listItems?.text = textField.text!
}
返回真
}
}

在这里缺少您需要创建第二个类来描述它引用myItem的变量的类。 该代码看起来像这样。

 类myItem:NSObject { 
var文字:字串
  init(text:String){ 
self.text =文字
}
}

除此之外,您还需要查看为UITableView控制器添加的初始代码,并对其进行更新以反映您现在希望它使用自定义单元格的事实。

  myTableView.register(myTableViewCell.self,forCellReuseIdentifier:“ tableCell”) 

完成之后,您还需要更改引用此单元格的代码。

  func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell { 
 让cell = tableView.dequeueReusableCell(withIdentifier:“ tableCell”,for:indexPath)为!  myTableViewCell 
 让item = itemsToLoad [indexPath.row] 
cell.listItems =物品
cell.selectionStyle = UITableViewCellSelectionStyle.none
返回单元
}

我希望能够在表中插入和删除行,并添加了iOS 11特定方法来做到这一点。

  func tableView(_ tableView:UITableView,LeadingSwipeActionsConfigurationForRowAt indexPath:IndexPath)-> UISwipeActionsConfiguration? 
  { 
让editAction = UIContextualAction(style:.normal,标题:“ Insert”,处理程序:{(ac:UIContextualAction,view:UIView,success:(Bool)-> Void)成功(true)
让newE = myItem(text:“ new”)
itemsToLoad.append(newE)
tableView.insertRows(at:[IndexPath(row:itemsToLoad.count-1,section:0)],带有:.automatic)
tableView.endUpdates()
  }) 
editAction.backgroundColor = .blue
返回UISwipeActionsConfiguration(操作:[editAction])
}
  func tableView(_ tableView:UITableView,TrailingSwipeActionsConfigurationForRowAt indexPath:IndexPath)-> UISwipeActionsConfiguration?  { 
如果itemsToLoad.count == 1 {
//中止
返回UISwipeActionsConfiguration(操作:[])
}
 让deleteAction = UIContextualAction(style:.destructive,title:“ Delete”,handler:{(ac:UIContextualAction,view:UIView,success:(Bool)-> Void)成功(true) 
itemsToLoad.remove(位于:indexPath.row)
self.myTableView.reloadData()
})
  deleteAction.backgroundColor = .red 
返回UISwipeActionsConfiguration(操作:[deleteAction])
}

因此,您在现有行上向右滑动即可插入新行,向左滑动即可删除一行。

但是好吧,这有一个缺点……您至少需要添加一行,并且实际上您无法删除所有这些行,否则您将无法再添加任何行,因此,if abort子句会出现。

无论如何,我添加了4个以确保🙂刚开始,请将这行代码添加到viewWillAppear方法中。

  itemsToLoad + = [myItem(text:“ cat”),myItem(text:“ sat”),myItem(text:“ mat”),myItem(text:“ rat”)] 

那里有它。

信用:我需要提及https://blog.apoorvmote.com/edit-uitableviewcell-text-on-tap-in-ios-swift/,在其中找到了内联UITableView代码以实现此目的。 查看他的博客,这里还有更多内容,您应该看看!