我已根据inStock或outStock写了一个代码来改变单元边框的颜色,如果它在inStock它将是红色边框,否则它将是绿色的,但我不适合我,我把它放在willDisplayCell中,这里是我的代码:
func tableView(_ tableView: UITableView,
willDisplay cell: UITableViewCell,
forRowAt indexPath: IndexPath){
cell.backgroundColor = UIColor.clear
cell.contentView.backgroundColor = UIColor.clear
let whiteRoundedView : UIView = UIView(frame: CGRect(x:10,y: 5,width: self.view.frame.size.width - 20,height: 214))
whiteRoundedView.layer.masksToBounds = false
whiteRoundedView.layer.cornerRadius = 5.0
whiteRoundedView.layer.shadowOffset = CGSize(width: -1,height: 1)
whiteRoundedView.layer.borderWidth = 2
cell.contentView.addSubview(whiteRoundedView)
cell.contentView.sendSubview(toBack: whiteRoundedView)
if stock[indexPath.row] == "inStock" {
whiteRoundedView.layer.borderColor = UIColor.red.cgColor
}
else {
whiteRoundedView.layer.borderColor = UIColor.green.cgColor
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个自定义 tableviewcell 并且当我单击按钮时,单元格内有按钮,下一个视图控制器不显示他的内容,但是如果我单击单元格然后单击按钮,下一个视图控制器显示正确的内容是我的代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! TableViewCell
cell.label.text = numberArray[indexPath.row]
cell.button.tag = indexPath.row
cell.button.addTarget(self,action:#selector(TableViewController.someAction(sender:)),
for:.touchUpInside)
return cell
}
Run Code Online (Sandbox Code Playgroud)
这是我的按钮操作:
func someAction(sender : UIButton){
self.performSegue(withIdentifier: "showDetail", sender: self)
}
Run Code Online (Sandbox Code Playgroud)
这是我的转场功能:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow
{
(segue.destination as! ViewController).number = numberArray[indexPath.row]
}
}
}
Run Code Online (Sandbox Code Playgroud)