이 코드는 swift4에서 저에게 효과적입니다.
위 화면의 답은 다음과 같습니다.
func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let TrashAction = UIContextualAction(style: .normal, title: "Trash", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
TrashAction.backgroundColor = .red
let FlagAction = UIContextualAction(style: .normal, title: "Flag", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
FlagAction.backgroundColor = .orange
let MoreAction = UIContextualAction(style: .normal, title: "More", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
})
MoreAction.backgroundColor = .gray
return UISwipeActionsConfiguration(actions: [TrashAction,FlagAction,MoreAction])
}
위 화면의 답변 :-
func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let closeAction = UIContextualAction(style: .normal, title: "Mark as Read", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("CloseAction ...")
success(true)
})
closeAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [closeAction])
}
다음과 같이 tableview Delegate 메소드를 작성하십시오.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrPerson.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let personName = arrPerson[indexPath.row]
cell.textLabel?.text = personName.personName
return cell
}
그리고 viewDidLoad에서
override func viewDidLoad() {
super.viewDidLoad()
tblView.delegate = self
tblView.dataSource = self
let person1 = personData(personName: "Jonny", personAge: 30)
let person2 = personData(personName: "Chandan", personAge: 20)
let person3 = personData(personName: "Gopal", personAge: 28)
arrPerson.append(person1)
arrPerson.append(person2)
arrPerson.append(person3)
}