【发布时间】:2017-10-04 16:10:30
【问题描述】:
我想在 UITableViewCell 下添加一张图片和一些文字。这是我想要达到的结果:
但我只能用纯代码实现 UITableViewCell。任何人都知道如何在 UITableViewCell 下方或上方添加图片或一些文本?任何建议,将不胜感激。这是我目前的结果:
这是我的代码:
import UIKit
class AboutViewController : UITableViewController {
private let about = ["About us"]
private let termsOfService = ["Terms of service"]
private let privacyPolicies = ["Privacy policies"]
private let openSourceLicense = ["Open Source Libraries"]
private let sections = ["about", "termsOfService", "privacyPolicies", "openSourceLicense"]
let myTableView: UITableView = {
let mt = UITableView()
return mt
}()
override func viewDidLoad() {
super.viewDidLoad()
// register cell name
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
// set DataSource
myTableView.dataSource = self
// set Delegate
myTableView.delegate = self
}
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.rowHeight
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
print("Value: \(about[indexPath.row])")
} else if indexPath.section == 1 {
print("Value: \(termsOfService[indexPath.row])")
} else if indexPath.section == 2 {
print("Value: \(privacyPolicies[indexPath.row])")
} else if indexPath.section == 3 {
print("Value: \(openSourceLicense[indexPath.row])")
}
}
// return the number of cells each section.
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return about.count
} else if section == 1 {
return termsOfService.count
} else if section == 2 {
return privacyPolicies.count
} else if section == 3 {
return openSourceLicense.count
} else {
return 0
}
}
// return cells
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "MyCell")
if indexPath.section == 0 {
cell.accessoryType = .disclosureIndicator
cell.textLabel?.textAlignment = .left
cell.textLabel?.text = "\(about[indexPath.row])"
} else if indexPath.section == 1 {
cell.accessoryType = .disclosureIndicator
cell.textLabel?.textAlignment = .left
cell.textLabel?.text = "\(termsOfService[indexPath.row])"
} else if indexPath.section == 2 {
cell.accessoryType = .disclosureIndicator
cell.textLabel?.textAlignment = .left
cell.textLabel?.text = "\(privacyPolicies[indexPath.row])"
} else if indexPath.section == 3 {
cell.accessoryType = .disclosureIndicator
cell.textLabel?.textAlignment = .left
cell.textLabel?.text = "\(openSourceLicense[indexPath.row])"
}
return cell
}
}
【问题讨论】:
-
是什么阻止您添加图片和描述?你有什么问题?
-
如果项目多,图片也会滚动吗?如果没有,最简单的方法是在
self.view上添加一个 UIImageView,它的来源是self.tableView的末尾。否则,您是否有关于创建自定义UITableViewCell的问题?在cellForRowAtIndexPath:中管理各种UITableViewCell? -
@rmaddy 我只是不知道如何添加图片和描述...
-
@Larme 对不起,我最后不想自定义 UITableViewCel。我希望它像第一张照片一样。
-
使用页脚或页眉视图可以解决问题。但它是否滚动,这是一个重要的问题。如果没有,那么就做
self.view.addSubview(myUIImageView)
标签: ios swift uitableview uiimageview uitextview