部分显示表格视图单元格内容 Swift
创始人
2024-12-24 09:01:00
0

要部分显示表格视图单元格内容,你可以使用UILabel的lineBreakMode属性来设置文本的截断方式。下面是一个使用Swift的示例代码:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let data = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
                "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
                "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."]

    let tableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.frame = view.bounds
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        view.addSubview(tableView)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.numberOfLines = 2
        cell.textLabel?.lineBreakMode = .byTruncatingTail
        cell.textLabel?.text = data[indexPath.row]
        return cell
    }
}

在这个示例中,我们创建了一个包含四个长文本的数组作为数据源。在tableView(_:cellForRowAt:)方法中,我们将单元格的textLabel的numberOfLines属性设置为2,以便显示两行文本。然后,我们将lineBreakMode属性设置为.byTruncatingTail,以便将多余的文本截断并显示省略号。这样,单元格中的文本就会部分显示。

相关内容

热门资讯

透视代打!werplan有挂吗... 透视代打!werplan有挂吗(透视)软件教程(2023已更新)(哔哩哔哩)1、让任何用户在无需we...
透视安卓版!aapoker透视... 透视安卓版!aapoker透视怎么用(透视)透视脚本入口(果然是有挂);所有人都在同一条线上,像星星...
透视app!sohoo辅助(透... 透视app!sohoo辅助(透视)揭秘教程(2020已更新)(哔哩哔哩);1、下载好sohoo辅助辅...
透视模拟器!aapoker透视... 透视模拟器!aapoker透视脚本(透视)透视脚本下载(切实是有挂)1、首先打开aapoker透视脚...
透视科技!pokemmo辅助器... 透视科技!pokemmo辅助器脚本下载(透视)黑科技教程(2024已更新)(哔哩哔哩);一、poke...
透视游戏!aapoker真的假... 透视游戏!aapoker真的假的(透视)ai插件(切实是有挂)1)aapoker真的假的辅助挂:进一...
透视脚本!哈糖大菠萝有没有挂(... 透视脚本!哈糖大菠萝有没有挂(透视)wepoke教程(2026已更新)(哔哩哔哩)1、上手简单,内置...
透视软件!aapoker辅助器... 透视软件!aapoker辅助器是真的吗(透视)辅助(真是是有挂)1、全新机制【aapoker辅助器是...
透视挂透视!werplan辅助... 透视挂透视!werplan辅助软件(透视)线上教程(2020已更新)(哔哩哔哩)1、werplan辅...
透视辅助!aapoker怎么控... 透视辅助!aapoker怎么控制牌(透视)透视脚本(原来有挂)1、游戏颠覆性的策略玩法,独创攻略技巧...