如何在单个tableView中创建各种类型的单元格?

问题描述 投票:1回答:2

我目前正在开发一款应用程序,它具有半优步,半智能等功能。

我想创建一个用于编辑配置文件的TableView,使其看起来像是Tinder的配置文件编辑视图的视图。

我成功地制作了第一个用于拾取轮廓图像的单元格和用于textLabel的第二个单元格。

我想制作一个第三个单元格,它有一个可编辑的TextView(不是textField,所以用户可以输入几行单词)但代码不能正常工作。

看起来虽然我在自定义单元类中创建了一个textView并将其设置为我的tableViewController类中的第三个tableView单元格,但它并没有出现在我的模拟器上。

我仍然不太确定为单个tableView制作几种类型的单元格的好方法是什么,所以如果还有其他更好的方法,我很想知道。

我的相关问题代码如下。

视图控制器

//EditProfileViewController For The Profile Edit view
class EditProfileViewController: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

TableViewController

//MyTableViewController for the EditProfileViewController
class EditProfileTableViewController: UITableViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextViewDelegate {
override func viewDidLoad() {
    super.viewDidLoad()

}

    override func numberOfSections(in tableView: UITableView) -> Int {

    return 3
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return 5
    } else if section == 1 {
        return 2
    } else {
        return 4
    }
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //link my custom Cell here
    if let cell = tableView.dequeueReusableCell(withIdentifier: "MyCustomCell", for: indexPath) as?  MyCellTableViewCell {

        if indexPath.section == 0 {

            if indexPath.row  == 0 {
                //this row is for an Image Picker
                cell.textLabel?.text = "edit profile image"
            } else if indexPath.row == 1 {
                //this row is just like a title of third cell which is editable self description
                cell.textLabel?.text = "Describe yourself"
                cell.isUserInteractionEnabled = false
            } else if indexPath.row == 2 {
                //this row is an editable self description
                cell.addSubview(cell.selfDescritionTextView)
                cell.selfDescritionTextView.delegate = self
                cell.selfDescritionTextView.isEditable = true
                cell.selfDescritionTextView.text = "Enter some descriptions here"


            }

        }

        else if indexPath.section == 1 {
            cell.textLabel?.text = "section 1"


        }

        else if indexPath.section == 2 {
            cell.textLabel?.text = "section 2"

        }


        return cell
    } else {
        //Just in case
        return UITableViewCell()
    }
}

}

的UITableViewCell

//MyCellTableViewCell
class MyCellTableViewCell: UITableViewCell {

//create a textView for self description
var selfDescritionTextView = UITextView()

//init
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: .default, reuseIdentifier: "MyCustomCell")
    selfDescritionTextView = UITextView(frame: self.frame)
    self.addSubview(selfDescritionTextView)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

如果您需要知道的代码多于我在此处展示的代码,请告知我们。

我并不痴迷于这种方法来创建我的编辑个人资料视图,所以如果有人知道比这更好的其他方法,我也很乐意知道。

谢谢

ios swift uitableview uiviewcontroller
2个回答
1
投票
override func viewDidLoad() {
   super.viewDidLoad()

   // Do any additional setup after loading the view.
   tableView.register(UINib(nibName: TableViewCell1.NibName, bundle: nil), forCellReuseIdentifier: TableViewCell1.Identifier)
   tableView.register(UINib(nibName: TableViewCell2.NibName, bundle: nil), forCellReuseIdentifier: TableViewCell2.Identifier)
   tableView.register(UINib(nibName: TableViewCell3.NibName, bundle: nil), forCellReuseIdentifier: TableViewCell3.Identifier)
}

您将分别拥有3个自定义表视图单元及其子视图:

class MyCellTableViewCell1: UITableViewCell {
   static var NibName: String = String(describing: TableViewCell1.self)
   static var Identifier: String = "TableViewCell1"

   func configure() { ... }
}

然后:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell: UITableViewCell
   // link my custom Cell here. 

   if indexPath.row  == 0, let dequeuedCell = tableView.dequeueReusableCell(withIdentifier: TableViewCell1.Identifier, for: indexPath) as? TableViewCell1 {
      dequeuedCell.configure()
      cell = dequeuedCell
   }
   else if indexPath.row  == 1, let dequeuedCell = tableView.dequeueReusableCell(withIdentifier: TableViewCell2.Identifier, for: indexPath) as? TableViewCell2 {
      dequeuedCell.configure()
      cell = dequeuedCell
   }
   else if indexPath.row  == 2, let dequeuedCell = tableView.dequeueReusableCell(withIdentifier: TableViewCell3.Identifier, for: indexPath) as? TableViewCell3 {
      dequeuedCell.configure()
      cell = dequeuedCell
   }
   else {
      cell = UITableViewCell()
   }


   return cell

}

1
投票

像这样注册多个nib文件,但它在viewDidLoad中

  tableView.register(UINib(nibName: "TableViewCell1", bundle: nil), forCellReuseIdentifier: CellIdentifier1)

  tableView.register(UINib(nibName: "TableViewCell2", bundle: nil), forCellReuseIdentifier: CellIdentifier2)

  tableView.register(UINib(nibName: "TableViewCell3", bundle: nil), forCellReuseIdentifier: CellIdentifier3)
© www.soinside.com 2019 - 2024. All rights reserved.