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

标签 ios swift uitableview uiviewcontroller

我目前正在开发一款应用,其功能一半是 Uber,一半是 Tinder。

我想创建一个用于编辑个人资料的 TableView,使其看起来像 Tinder 的个人资料编辑 View 。

我成功地制作了第一个用于获取个人资料图像的单元格和第二个用于 textLabel 的单元格。

我想制作第三个单元格,它有一个可编辑的 TextView(不是 textField,这样用户可以输入多行单词)但是代码没有按我的预期工作。

虽然我在我的自定义单元格类中创建了一个 textView 并将其设置为我的 tableViewController 类中的第三个 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
}

}

如果您需要知道比我在这里显示的更多的代码,请告诉我。

我并不沉迷于这种创建我的编辑个人资料 View 的方法,所以如果有人知道比这更好的其他方法,我也很想知道。

谢谢

最佳答案

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 个自定义表格 View 单元格,分别及其 subview :

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

}

关于ios - 如何在单个 tableView 中创建各种类型的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47855350/

相关文章:

ios - 不清楚为什么 UIView.animate 工作顺利,但代码在动画 block 之外

cocoa-touch - 当 UITableView 在 UIPopoverController 中时,为什么单元格边界和框架大小不正确?

ios - 允许 iOS 应用程序用户通过 Twilio 号码、Parse 后端相互调用电话和发短信

javascript - 为什么 jquery 似乎没有应用特定于 vendor 的 css 属性?

ios - pow() 行为与 Float 和 Double 不同

swift - 通过 UITabBarController 移动到选定索引后 UIView 为零

ios - 转到 NavigationViewController 中的第二个 View Controller

ios - danielgindi/iOSCharts chartValueSelected 未调用

ios - 自定义 TableViewCell 中的标签不能是多行或换行

ios - UITableView 重新加载数据有效,但单元格不会在屏幕上更新