0
UITableViewCellの下に図とテキストを追加したいと思います。ここで私が達成したい結果である:プログラムでUITableViewCellの下にUITextViewとUIImageViewを追加するSwift 4
は、しかし、私は唯一の純粋なコードでの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
}
}
画像と説明を追加できない原因は何ですか?あなたはどんな問題を抱えていますか? – rmaddy
さらに多くのアイテムがある場合は、イメージもスクロールされますか?そうでない場合は、最も簡単な方法は 'self.view'にUIImageViewを追加することです。その起源は' self.tableView'の終わりです。そうでなければ、カスタム 'UITableViewCell'を作ることについてのあなたの問題ですか? 'cellForRowAtIndexPath:'のさまざまな種類の 'UITableViewCell'を管理していますか? – Larme
@rmaddy画像と説明を追加する方法がわかりません... – user174782