我正在使用Swift 3.0中的一个项目,我有一个UITableView
有三个自定义单元格。在第一个我只有一个标签,在第二个我有一个图像加上一个标签以及可扩展和可折叠的标题。因此,我有三个不同的部分为第二个单元格。最后,第三个也只包含一个标签。我坚持使用委托方法,因为这三个单元格返回三个不同的数组,其中计数不同将返回。我已经部分完成了以下代码。截至目前,代码只是第二个单元格(正在运行)。但我认为我已正确完成cellForRow
方法。
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate {
@IBOutlet var tblSwift: UITableView!
var arrayForTableView = ["swift","objective-c","c#","java"]
var arrayForIcons = ["swift.jpeg","objectiveC.jpeg","csharp.jpeg","java.jpeg"]
let dictSection = [["swift 1.0","swift 2.0","swift 3.0"],["objectiveC 1.0","objectiveC 2.0"],["C# 1.0","C# 2.0"],["java 1.0"]]
let cellItentifir = "cellText"
var expanedSections = [true, true, true, true]
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
// The bellow code is for the expandable and collapsible cells
return arrayForTableView[section]
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let headerView : UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
headerView.contentView.backgroundColor = UIColor.yellow.withAlphaComponent(1.0)
}
func tapped(sender: UITapGestureRecognizer)
{ // The bellow code is for the expandable and collapsible cells
if let tag = sender.view?.tag{
expanedSections[tag] = !expanedSections[tag]
}
tblSwift.reloadData()
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UITableViewHeaderFooterView()
headerView.tag = section
let tapRecog = UITapGestureRecognizer(target: self, action: #selector(tapped))
tapRecog.numberOfTapsRequired = 1
tapRecog.numberOfTouchesRequired = 1
tapRecog.delegate = self
headerView.addGestureRecognizer(tapRecog)
return headerView
}
func numberOfSections(in tableView: UITableView) -> Int {
return arrayForTableView.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
let isExpanded = expanedSections[section]
if(!isExpanded) {
return 0
}
return dictSection[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "FirstCell")
cell?.textLabel?.text = "First Cell"
return cell!
}else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: self.cellItentifir)
cell?.textLabel?.text = dictSection[indexPath.section][indexPath.row]
cell?.imageView?.image = UIImage(named:arrayForIcons[indexPath.section])
return cell!
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: "ThirdCell")
cell?.textLabel?.text = "Third Cell"
return cell!
}
}
答案 0 :(得分:0)
您需要在condtion中考虑部分而不是indexPath。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if section == 1 {
let isExpanded = expanedSections[section]
if(!isExpanded) {
return 0
}
return dictSection[section].count
}
else {
return 1;
}
}
现在是CellForRowAtIndexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "FirstCell")
cell?.textLabel?.text = "First Cell"
return cell!
}else if section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: self.cellItentifir)
cell?.textLabel?.text = dictSection[indexPath.section][indexPath.row]
cell?.imageView?.image = UIImage(named:arrayForIcons[indexPath.section])
return cell!
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: "ThirdCell")
cell?.textLabel?.text = "Third Cell"
return cell!
}
}