部分显示表格视图单元格内容 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,以便将多余的文本截断并显示省略号。这样,单元格中的文本就会部分显示。

相关内容

热门资讯

WPK俱乐部透视挂攻略!德普之... WPK俱乐部透视挂攻略!德普之星有透视辅助吗,红龙PoKerai多功能透视挂工具1、德普之星有透视辅...
热点讨论开挂!微乐苏州麻将ai... 热点讨论开挂!微乐苏州麻将ai开挂多功能插件,真是真的是有开挂软件经验是一款可以让一直输的玩家,快速...
WPK购买透视挂方针!wepo... WPK购买透视挂方针!wepoker辅助工具,AApokerai多功能透视挂方法一、wepoker辅...
最新研发透视挂!太浪乾安麻将a... 很多人使用云端 AI 时,都担心太浪乾安麻将、内部资料上传带来隐私风险。这款本地运行的 AI 黑科技...
WPK辅助透视挂步骤!微扑克游... WPK辅助透视挂步骤!微扑克游戏辅助器,菠萝智星德州ai多功能透视挂攻略1、进入到微扑克游戏辅助器是...
科普开挂!西元红河棋牌ai开挂... 科普开挂!西元红河棋牌ai开挂多功能插件,先前真的有开挂软件方案是一款可以让一直输的玩家,快速成为一...
WPK多开辅助器大纲!wepo... WPK多开辅助器大纲!wepoker有辅助功能吗,wpkai多功能辅助器攻略1、首先打开wepoke...
我来教教你辅助挂!麻友圈ai辅... 很多人使用云端 AI 时,都担心麻友圈、内部资料上传带来隐私风险。这款本地运行的 AI 黑科技,很好...
WPK四人辅助器办法!wepo... WPK四人辅助器办法!wepoker怎么设置房间,WePoKerai多功能辅助器工具1、进入到wep...
科普常识辅助!中至南昌麻将ai... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...