我在UItableViewCell的前端设置了“添加到购物车”操作。我设置了背景色,图像和标题。下面是我的代码。
@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let addToCart = UIContextualAction(style: .normal, title: "Add to Cart") { (action, view, nil) in
print("Added to cart")
}
addToCart.title = "Add to Cart"
addToCart.backgroundColor = #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)
addToCart.image = UIImage(named: "cart")
return UISwipeActionsConfiguration(actions: [addToCart])
}
Run Code Online (Sandbox Code Playgroud)
先前的尝试:我尚未使用定义标题,addToCart.title = "Add to Cart"但是在获得标题之后,我已经对其进行了设置。
我添加的图像尺寸为25 * 25,背景清晰,格式为.png。
我已经为 tableview 单元设置了一个领先的滑动操作,它添加到购物车。我还没有实现任何尾随滑动操作方法,但我仍然为每个单元格执行删除操作。下面是我所有 tableView 方法的代码。我还附上了模拟器中的一张图片。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (indexPath.row == 0) {
return 267
}
else {
return 85
}
}
//tableview methods
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
Run Code Online (Sandbox Code Playgroud)
// 对于第一个单元格
if (indexPath.row == 0){
let cell = storeInfoTable.dequeueReusableCell(withIdentifier: "storeinfocell1", for: indexPath) as! CustomStoreInfoTableOneViewCell
return cell
}
Run Code Online (Sandbox Code Playgroud)
// 对于剩余的单元格
let cell = storeInfoTable.dequeueReusableCell(withIdentifier: "storeItemCell", for: …Run Code Online (Sandbox Code Playgroud)