【发布时间】:2016-01-06 22:48:45
【问题描述】:
我正在尝试创建一个包含多个自定义 UITableViewCells 的表格视图。如果我只使用一个自定义单元格,则会显示正确的单元格。如果我添加第二个单元格,则显示第二个单元格会覆盖第一个单元格并禁止交互,第二个单元格会显示第一个自定义单元格的默认数据,直到它被选中,然后才会显示正确的单元格。
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let nib1 = UINib(nibName: "AccountCell1", bundle: nil)
tableView.registerNib(nib1, forCellReuseIdentifier: "SettingCell1")
let nib2 = UINib(nibName: "AccountCell2", bundle: nil)
tableView.registerNib(nib2, forCellReuseIdentifier: "SettingCell2")
//removing empty rows
tableView.tableFooterView = UIView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
/*let cell = tableView.dequeueReusableCellWithIdentifier( "AccountCell", forIndexPath: indexPath) as! UITableViewCell*/
let cell1 = tableView.dequeueReusableCellWithIdentifier( "SettingCell1", forIndexPath: indexPath) as! AccountCell1Controller
let cell2 = tableView.dequeueReusableCellWithIdentifier( "SettingCell2", forIndexPath: indexPath) as! AccountCell2Controller
// Configure the cell...
if(indexPath.row == 0)
{
let imageName = "Amanda.jpeg"
let image = UIImage(named: imageName)
cell1.UserImage.image = image
cell1.UserLabel.text = "@AmandaSmith"
cell1.setNeedsLayout()
return cell1
}
else
{
return cell2
}
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if(section == 0)
{
return "John Smith"
}
else if(section == 1)
{
return "Settings"
}
else if(section == 2)
{
return "About"
}
return ""
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(indexPath.row == 0)
{
return 204
}
else if(indexPath.row == 1)
{
return 61
}
else {
return 44
}
}
}
My Account 单元控制器,2 是两个按钮,对应的动作
class AccountCell1Controller: UITableViewCell {
@IBOutlet weak var UserImage: UIImageView!
@IBOutlet weak var UserLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
let user = UserData.self
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
【问题讨论】:
-
请提供您的
cellForRowAtIndexPath方法的代码示例,以及您与单元格交互的任何其他地方。如果不查看一些代码,我们将无法帮助您诊断问题。 -
@AlexPopov 我编辑了我的问题,很抱歉我在添加代码之前不小心点击了提交问题。
标签: ios swift tableview tableviewcell