ios - 根据单元格中的内容自动更改单元格内容 - Xcode 9.2 Swift 4

标签 ios swift uitableview

我在 View Controller 中有一个 TableView ,在单元格中,当文本太长时,我的文本会被截断。如何让单元格根据单元格中的内容自动更改或让文本换行以免文本被截断?这是一个 image我在说什么。

override func viewDidLoad() {
    super.viewDidLoad()

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

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return 50
}

func allowMultipleLines(tableViewCell: UITableViewCell) {
    tableViewCell.textLabel?.numberOfLines = 0
    tableViewCell.textLabel?.lineBreakMode = .byWordWrapping
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return courses.count
}


 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let course:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "course")!

    course.textLabel?.text = courses[indexPath.row]

    return course
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "Courses", sender: nil)
}

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let detailView: DetailCoursesViewController = segue.destination as! DetailCoursesViewController

    selectedRow = (table.indexPathForSelectedRow?.row)!

    detailView.setCourseTitle (t: courses[selectedRow])
    detailView.setCourseDescription (d: courseDescription[selectedRow])
    detailView.setCredits (c: credits[selectedRow])
    detailView.setPrerequisites (p: prereq[selectedRow])

}

这是 image代码的

最佳答案

首先,您需要为 TableViewcell 中的 contentView 设置标签的前导、尾随、底部和顶部约束

     override func viewDidLoad() {
        super.viewDidLoad()

        self.tabelView.estimatedRowHeight = 50
        self.tabelView.rowHeight = UITableViewAutomaticDimension

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       // #warning Incomplete implementation, return the number of rows
       return courses.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

       let course:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "course")!
       cell.textLabel.numberOfLines = 0
       course.textLabel?.text = courses[indexPath.row]

       return course
    }

关于ios - 根据单元格中的内容自动更改单元格内容 - Xcode 9.2 Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49291374/

相关文章:

objective-c - MailCore 并发支持

ios - 将 CIFilter 保存为 RAW 图像

swift - 如何在 NSApplicationDelegate 上加载 NSViewController

swift - 将表格 View 设置为编辑模式

ios - 批量删除剩下的所有行改为向左和向上

ios - 如何使包显示名称长度超过 12 个字符

ios - Swift - Core Data - codegen - 使扩展符合协议(protocol)?

swift - "try"关键字的位置差异

ios - Swift - 在不嵌入导航 Controller 的情况下从左到右的 Segue 过渡

ios - 在用户定义的函数中更改单元格的附件 View