Swift根据数据更改tableviewcell边框颜色

Swift根据数据更改tableviewcell边框颜色,第1张

概述我已根据inStock或outStock写了一个代码来改变单元边框颜色,如果它在inStock它将是红色边框,否则它将是绿色的,但我不适合我,我把它放在willDisplayCell中,这里是我的代码: func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, 我已根据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    }}
解决方法 尝试将代码移动到像这样的cellForRowAt方法

cell.layer.masksToBounds = truecell.layer.cornerRadius = 5cell.layer.borderWIDth = 2cell.layer.shadowOffset = CGSize(wIDth: -1,height: 1)let bordercolor: UIcolor = (stock[indexPath.row] == "inStock") ? .red : .greencell.layer.bordercolor = bordercolor.cgcolor
总结

以上是内存溢出为你收集整理的Swift根据数据更改tableviewcell边框颜色全部内容,希望文章能够帮你解决Swift根据数据更改tableviewcell边框颜色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1007719.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存