使用Swift以编程方式自定义UITableViewCell

编程入门 行业动态 更新时间:2024-10-25 00:26:02
本文介绍了使用Swift以编程方式自定义UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嘿,我正在尝试创建自定义的 UITableViewCell ,但是在模拟器上看不到任何东西。

Hey all I am trying to create a custom UITableViewCell, but I see nothing on the simulator. Can you help me please.

只有当我 var labUserName = UILabel(frame:CGRectMake(0.0,0.0,130, 30));

,但它与单元格重叠。我不明白,自动版式应该知道每个单元格的首选大小/最小大小吗?

but it overlaps the cell. I don't understand, Auto Layout should know the preferred size/minimum size of each cell?

谢谢

import Foundation import UIKit class TableCellMessages: UITableViewCell { var imgUser = UIImageView(); var labUserName = UILabel(); var labMessage = UILabel(); var labTime = UILabel(); override init(style: UITableViewCellStyle, reuseIdentifier: String) { super.init(style: style, reuseIdentifier: reuseIdentifier) imgUser.layer.cornerRadius = imgUser.frame.size.width / 2; imgUser.clipsToBounds = true; contentView.addSubview(imgUser) contentView.addSubview(labUserName) contentView.addSubview(labMessage) contentView.addSubview(labTime) //Set layout var viewsDict = Dictionary <String, UIView>() viewsDict["image"] = imgUser; viewsDict["username"] = labUserName; viewsDict["message"] = labMessage; viewsDict["time"] = labTime; //Image //contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[image(100)]-'", options: nil, metrics: nil, views: viewsDict)); //contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[image(100)]-|", options: nil, metrics: nil, views: viewsDict)); contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[username]-[message]-|", options: nil, metrics: nil, views: viewsDict)); contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[username]-|", options: nil, metrics: nil, views: viewsDict)); contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[message]-|", options: nil, metrics: nil, views: viewsDict)); } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }

推荐答案

让我们一些假设:

您有一个包含 Storyboard 的iOS8项目,其中包含一个 UITableViewController 。其 tableView 具有一个独特的原型 UITableViewCell ,具有自定义样式和标识符: cell。

You have an iOS8 project with a Storyboard that contains a single UITableViewController. Its tableView has a unique prototype UITableViewCell with custom style and identifier: "cell".

UITableViewController 将链接到类 TableViewController ,即单元格将链接到类 CustomTableViewCell 。

The UITableViewController will be linked to Class TableViewController, the cell will be linked to Class CustomTableViewCell.

然后您可以设置以下代码(已为Swift 2更新):

You will then be able to set the following code (updated for Swift 2):

CustomTableViewCell.swift:

import UIKit class CustomTableViewCell: UITableViewCell { let imgUser = UIImageView() let labUserName = UILabel() let labMessage = UILabel() let labTime = UILabel() override func awakeFromNib() { super.awakeFromNib() imgUser.backgroundColor = UIColor.blueColor() imgUser.translatesAutoresizingMaskIntoConstraints = false labUserName.translatesAutoresizingMaskIntoConstraints = false labMessage.translatesAutoresizingMaskIntoConstraints = false labTime.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(imgUser) contentView.addSubview(labUserName) contentView.addSubview(labMessage) contentView.addSubview(labTime) let viewsDict = [ "image": imgUser, "username": labUserName, "message": labMessage, "labTime": labTime, ] contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[image(10)]", options: [], metrics: nil, views: viewsDict)) contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[labTime]-|", options: [], metrics: nil, views: viewsDict)) contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[username]-[message]-|", options: [], metrics: nil, views: viewsDict)) contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[username]-[image(10)]-|", options: [], metrics: nil, views: viewsDict)) contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[message]-[labTime]-|", options: [], metrics: nil, views: viewsDict)) } }

TableViewController.swift:

import UIKit class TableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() //Auto-set the UITableViewCells height (requires iOS8+) tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 44 } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 100 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell cell.labUserName.text = "Name" cell.labMessage.text = "Message \(indexPath.row)" cell.labTime.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .ShortStyle, timeStyle: .ShortStyle) return cell } }

您会期望这样的显示(iPhone横向):

You will expect a display like this (iPhone landscape):

更多推荐

使用Swift以编程方式自定义UITableViewCell

本文发布于:2023-11-28 07:33:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   方式   Swift   UITableViewCell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!