我正在尝试实现UITableViewController
。
我为自定义单元格类和一个UITableViewController
写了一些代码,对于自定义单元格有3个问题:
1)如何在titleView和descriptionView中修改字符串“ title”和“ description”的属性,例如大小,字体,颜色等?
2)当两个图像均位于描述字段上方时,如何设置图像位于左侧,图像标题位于右侧? 这是the current output
的屏幕截图3)在我的titleView中,当我运行该应用程序时出现问题,这意味着任何用户都可以修改标题,该问题不会出现在description字段中。谁能找出原因? 我还将在下面添加此问题的屏幕截图: Anyone can modify the title while using the app
这是UITableViewController
的代码:
struct CellData {
var title: String?
var description: String?
var image: UIImage?
}
class TableViewController: UITableViewController {
var data = [CellData]()
override func viewDidLoad() {
super.viewDidLoad()
data = [CellData.init(title: "Dips", description: "Dips is one of the best exercises when it comes to building chest, triceps and even side delts. It's also good in the long run due to the fact that it implies your core a lot.", image: #imageLiteral(resourceName: "Dips"))]
self.tableView.register(CustomCell.self, forCellReuseIdentifier: "custom")
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "custom") as! CustomCell
cell.mainImage = data[indexPath.row].image
cell.myDescription = data[indexPath.row].description
cell.title = data[indexPath.row].title
cell.layoutSubviews() // you don't have to scroll up to scale them properly
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
}
以下是自定义单元格的代码:
class CustomCell: UITableViewCell {
var title: String?
var myDescription: String?
var mainImage: UIImage?
var titleView: UITextView = {
var mainTitleView = UITextView()
mainTitleView.translatesAutoresizingMaskIntoConstraints = false
return mainTitleView
}()
var myDescriptionView: UITextView = {
var textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false // set this to false if you plan to add future constraints
textView.isScrollEnabled = false
return textView
}()
var mainImageView : UIImageView = {
var imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false // same applies here
imageView.contentMode = .scaleAspectFit
return imageView
}()
// you just type init
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(titleView)
self.addSubview(myDescriptionView)
self.addSubview(mainImageView)
titleView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
titleView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
titleView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
titleView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
myDescriptionView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
myDescriptionView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
myDescriptionView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
mainImageView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
mainImageView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
mainImageView.bottomAnchor.constraint(equalTo: self.myDescriptionView.topAnchor).isActive = true
mainImageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
mainImageView.heightAnchor.constraint(equalToConstant: 120).isActive = true
}
// just type layout and Pick
override func layoutSubviews() {
super.layoutSubviews()
if let title = title {
titleView.text = title
}
if let description = myDescription {
myDescriptionView.text = description
}
if let image = mainImage {
mainImageView.image = image
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder: ) has not been implemented")
}
}