swift UItableViewCell怎么自定义添加控件,设置样式

swift UItableViewCell怎么自定义添加控件,设置样式,第1张

你可以使用xib,推荐使用,创建cell,同时创建Xib即可,xib上面可以自定义布局

// 注册nib

tableView.registerNib(UINib(nibName: "nibName", bundle: mainBundle), forCellReuseIdentifier: "cellIdentifier")

// 注册类

tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "cellIdentifier")

// 数据源

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as! TableViewCell

return cell

}

使用注册类的时候需要手写代码

重写init(style: UITableViewCellStyle, reuseIdentifier: String?)方法,并在里面进行UI设置

ps: 网上搜自定义cell教程还是挺多的

在Xcode中,选择 File\New\Project… 创建一个新项目。选择 Single View Application 点 Next 。将项目命名为 CandySearch 确保 Language 设置为 Swift 并且 Devices 设置为 iPhone 。点击完成,选择你想储存的位置,然后点击 Create 。

首先将默认的文件清理掉,这样你就可以真正的从头开始。在 Project Navigator中,选择 ViewController.swift ,右键点击,选择删除,然后选择 Move to Trash 。然后打开 Main.storyboard ,选择唯一的一个view controller然后删掉他。现在你有一个空的storyboard,可以在你的应用中添加主屏幕了。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11595039.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存