Swift tableView单元格配件类型

编程入门 行业动态 更新时间:2024-10-28 15:35:21
本文介绍了Swift tableView单元格配件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet var tableView: UITableView var items: String[] = ["We", "Heart", "Swift"] override func viewDidLoad() { super.viewDidLoad() self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell") } func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { return self.items.count; } func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as UITableViewCell cell.textLabel.text = self.items[indexPath.row] cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton cell.selectionStyle = UITableViewCellSelectionStyle.Blue tableView.separatorStyle = UITableViewCellSeparatorStyle.None return cell } func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { println("You selected cell #\(indexPath.row)!") } }

我的问题是附件类型和selectionStyle不会更改。 tableView.separatorStyle以及cell.textlabel.text确实得到更改。 我该如何解决?

My problem is that the accessoryType and the selectionStyle don't get changed. The tableView.separatorStyle does get changed as well as the cell.textlabel.text. How can I fix that?

推荐答案

UITableViewCell.SelectionStyle.blue

单元格在选定时具有默认背景色。

The cell has a default background color when selected.

在iOS 7中,选择颜色不再是蓝色。使用 UITableViewCell.SelectionStyle.default代替。

In iOS 7, the selection color is no longer blue. Use UITableViewCell.SelectionStyle.default instead.

对于 accessoryType ,只要您以后不要在其他地方更改它,它就可以正常工作。确保表宽度正确,否则附件视图可能不在屏幕上。

As for the accessoryType, it should work fine as long as you don't change it later somewhere else. Make sure that the table width is correct, otherwise accessory views might be offscreen.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet var tableView: UITableView var items: String[] = ["We", "Heart", "Swift"] override func viewDidLoad() { super.viewDidLoad() self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell") } func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { return self.items.count; } func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as UITableViewCell cell.textLabel.text = self.items[indexPath.row] cell.selectionStyle = UITableView.CellSelectionStyle.blue /* enum UITableViewCellAccessoryType : Int { case none // don't show any accessory view case disclosureIndicator // regular chevron. doesn't track case detailDisclosureButton // info button w/ chevron. tracks case checkmark // checkmark. doesn't track case detailButton // info button. tracks } */ // Standard options cell.accessoryType = UITableViewCell.AccessoryType.none cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator cell.accessoryType = UITableViewCell.AccessoryType.detailDisclosureButton cell.accessoryType = UITableViewCell.AccessoryType.checkmark cell.accessoryType = UITableViewCell.AccessoryType.detailButton // Custom view options cell.accessoryType = UITableViewCell.AccessoryType.none cell.accessoryView = UIView(frame: CGRectMake(0, 0, 20, 20)) cell.accessoryView.backgroundColor = UIColor.blueColor() return cell } func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { println("You selected cell #\(indexPath.row)!") } }

请注意每次请求单元格时都设置表的 separatorStyle 并不是一个好的解决方案,而是在 tableView 已加载:位于 viewDidLoad 。

Note that it isn't a good solution to set separatorStyle of the table each time the cell is requested, instead do it once when the tableView is loaded: at viewDidLoad.

更多推荐

Swift tableView单元格配件类型

本文发布于:2023-11-26 20:24:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635068.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   类型   配件   Swift   tableView

发布评论

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

>www.elefans.com

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