【发布时间】:2020-06-27 20:08:09
【问题描述】:
我正在构建一个包含 2 种单元格的用户配置文件表视图。 第一个单元格包含一个图像视图和一个标签(分别用于用户图像及其名称)
第二个标签包含 2 个并排的标签(一个用于职位名称,另一个用于职位描述)。
我希望达到的结果是:
- 每个单元格类型都在一个部分下
- 第一部分仅包含 1 个单元格(图像和标签)
- 第二部分包含 3 个单元格(第二类)。
我设法实现的是:
- 我有正确的部分名称/标题
- 我有正确数量的部分
- 第 1 节只有一种类型的单元格(正确的类型)
- 但是,在第 2 部分,我有第一种细胞类型和第二种细胞类型。
我想从第 2 部分中删除第一种类型的单元格。这是我的代码:
//MARK: - 2 CELLS CLASS
class ImageCell: UITableViewCell {
@IBOutlet weak var candidatePicture: UIImageView!
@IBOutlet weak var candidateNameLabel: UILabel!
}
//
class ItemCell: UITableViewCell {
@IBOutlet weak var itemNameLabel: UILabel!
@IBOutlet weak var itemDetailLabel: UILabel!
}
class CandidateUserProfileScreenTableViewController: UITableViewController {
//MARK: RELEVANT VARIABLES
let sectionNameArray: [String] = ["Candidate ID", "Jobs"]
let candidateId = ["Name"]
let candidateJobs = ["job 1", "job2", "job3"]
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return sectionNameArray.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
//return sectionNameArray[0]
return sectionNameArray[section]
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
//return 4
switch section {
case 0:
return candidateId.count
case 1:
return candidateJobs.count
default:
fatalError("there was a mistake in my code")
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// en fonction de index path
// index path == 0 on affiche la cellule avec image
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell", for: indexPath) as! ImageCell
//
cell.candidatePicture.image = UIImage(named: "welcome")
cell.candidateNameLabel.text = "John SMITH"
return cell
}
// sinon on affiche le second type de cellule
let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell", for: indexPath) as! ItemCell
cell.itemNameLabel.text = "Job 1"
cell.itemDetailLabel.text = "Job 1 detail"
return cell
}
【问题讨论】:
标签: ios swift xcode uitableview uitableviewsectionheader