私は現在、半Uber、半Tinderのような機能を持つアプリケーションを開発しています。1つのテーブルビューでさまざまな種類のセルを作成する方法は?
プロフィールを編集するためのTableViewを作成して、Tinderのプロファイル編集ビューのように見せたいと思います。
私は正常にプロファイル画像を取得するための最初のセルを作成し、textLabelのために2番目のセルを作成しました。
編集可能なTextView(テキストフィールドではなく、複数行の単語を入力できるようにする)を持つ3番目のセルを作成したいのですが、意図したとおりにコードが機能しません。
カスタムセルクラスでtextViewを作成し、それを私のtableViewControllerクラスの3番目のtableViewセルに設定しても、シミュレータには表示されないようです。
私はまだ、単一のtableViewのためにいくつかのタイプのセルを作成するための良い方法は何もわかりません。それを行う他のより良い方法があるなら、私は知りたいです。
この問題に関するマイコードは以下のとおりです。
のViewController
//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()
}
}
}
TableViewCell
//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
}
}
ここに示したコードよりも多くのコードを知りたい場合は、教えてください。
私は私の編集プロフィールを作成するためにこの方法に夢中になっていません。誰かこの他の方法を知っていれば、私も知りたいです。
おかげで、このような
すべてのセルがテーブルビューに埋め込まれていますか?すべてのセルにカスタムクラスがありますか? –
ブレークポイントを使用し、条件が満たされているかどうかコントロールがcellForRowを超えているかどうかを確認できますか?if cell = tableView.dequeueReusableCell(withIdentifier: "MyCustomCell"、for:indexPath) MyCellTableViewCell – MBN
そして、MyCellTableViewCellのストーリーボードまたはカスタムセルxibのtableviewプロトタイプセルを使用していますか? – MBN