Ios UITableViewCell功能与设计相匹配
使用Ios UITableViewCell功能与设计相匹配,ios,uitableview,Ios,Uitableview,使用UITableViewCell是否可以在不创建自定义UITableViewCell的情况下获得以下内容 我已经试用了自定义单元格 var tableView = UITableView() override func viewDidLoad() { super.viewDidLoad() tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.plain) tableVi
UITableViewCell
是否可以在不创建自定义UITableViewCell
的情况下获得以下内容
我已经试用了
自定义单元格
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.white
tableView.tableFooterView = UIView()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "myIdentifier")
tableView.frame = CGRect(x:0 , y:64, width:self.view.bounds.width, height:self.view.bounds.height)//Optional for table size
self.view.addSubview(tableView)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let myCell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "myIdentifier")
myCell.textLabel?.textColor = UIColor(red: 0, green: 137/255, blue: 197/255, alpha: 1.0)
myCell.textLabel?.numberOfLines = 2
myCell.textLabel?.font = UIFont.boldSystemFont(ofSize: 12)
myCell.textLabel?.halfTextColorChange(fullText: "Claim Submission\n6/10/2016", changeText: "6/10/2016")
myCell.detailTextLabel?.textColor = UIColor(red: 0, green: 137/255, blue: 197/255, alpha: 1.0)
myCell.detailTextLabel?.numberOfLines = 2
myCell.detailTextLabel?.font = UIFont.boldSystemFont(ofSize: 11) // (ofSize: 11)
myCell.detailTextLabel?.halfTextColorChange(fullText: "$0.01\nBal:$999.99", changeText: "Bal:$999.99")
return myCell
}
extension UILabel {
func halfTextColorChange (fullText : String , changeText : String ) {
let strNumber: NSString = fullText as NSString
let range = (strNumber).range(of: changeText)
let attribute = NSMutableAttributedString.init(string: fullText)
attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGray , range: range)
attribute.addAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 9)], range: range)
self.attributedText = attribute
}
}
输出
我强烈建议您使用布局在xib中设计所有内容。然后,您可以轻松实现相当复杂的设计思想,例如(示例):70%的单元具有顶部和底部,最后30%垂直居中,布局正确。整个细胞总是占据可用的空间,等等。如果你做得好,你就再也不会有这种头痛,你将不再关心它在什么设备上运行,等等。你可能会问,为什么是xib?因为一旦设计完成,iphone和ipad都可以使用相同的代码
这是关于用户界面的,现在到了课堂上。在所描述的情况下,您的自定义单元格类将只包含数据填充代码和一些逻辑(如果需要)。对于这种类型的单元格,您需要创建自己的自定义Celone word:Nope。