【发布时间】:2015-06-21 17:13:33
【问题描述】:
我正在尝试使一行的内容(在 UITableView 中)具有多个对齐方式:左、中和右。我在 Stackoverflow 上搜索了一个解决方案,发现了这个:Different text alignment to a row in table view。唯一的问题是该解决方案是基于 ObjectiveC 的,我无法将其转换为基于 Swift 的解决方案。
具体来说,我有一个表示活动用户列表的 UITableView。将内容加载到表格视图中可以正常工作,但为了便于阅读,内容 - 分为三种类型 - 需要在视觉上分为三个不同的“部分”。我该怎么做呢?上面的解决方案在 Swift 中会是什么样子?提前致谢。
代码(适用于 Vive)
类声明
class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
viewDidLoad()
self.activeIDs.delegate = self
self.activeIDs.dataSource = self
self.activeIDs.rowHeight = 25
self.activeIDs.separatorStyle = UITableViewCellSeparatorStyle.None
self.activeIDs.allowsMultipleSelection = true
cellForRowAtIndexPath
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = activeIDs.dequeueReusableCellWithIdentifier("cellReuseId", forIndexPath: indexPath) as ActiveIDTableViewCell
cell.leftLabel.text = "I go left"
cell.middleLabel.text = "I go center"
cell.rightLabel.text = "I go right"
return cell
}
numberOfRowsInSection
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return 4 //I randomly chose 4, just for example
}
ActiveIDTableViewCell.swift - 几乎复制了
import UIKit
class ActiveIDTableViewCell: UITableViewCell {
// alloc & init labels
let leftLabel = UILabel()
let middleLabel = UILabel()
let rightLabel = UILabel()
// MARK: Memory Management
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
// never forget to call super in overriden UIKit methods (almost never ;))
super.init(style: .Default, reuseIdentifier: reuseIdentifier)
// now let's assign these values we've set previously in storyboards
leftLabel.textAlignment = NSTextAlignment.Left
// you created a label, but you need to add this label as a subview of cells view.
contentView.addSubview(leftLabel)
middleLabel.textAlignment = NSTextAlignment.Center
contentView.addSubview(middleLabel)
rightLabel.textAlignment = NSTextAlignment.Right
contentView.addSubview(rightLabel)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented") //Xcode points to this line when error is thrown
}
// MARK: Layout
override func layoutSubviews() {
super.layoutSubviews()
// here happens all the magic which skips autolayouts. You can ofc set autolayouts from code :)
let frame = self.contentView.bounds
// frame was the size of the cell, but we want to set our labels at least 10px from sides of cell - so it looks nicely
let insetFrame = CGRectInset(frame, 10.0, 10.0)
// width of each label is width of cell / 3 (you want 3 labels next to each other) minus margins
let labelWidth = (CGRectGetWidth(insetFrame) - 20.0) / 3.0
// lets calculate the rects and assign frames to calculated values
let leftLabelRect = CGRectMake(CGRectGetMinX(insetFrame), CGRectGetMinY(insetFrame), labelWidth, CGRectGetHeight(insetFrame))
leftLabel.frame = leftLabelRect
let middleLabelRect = CGRectMake(CGRectGetMaxX(leftLabelRect), CGRectGetMinY(insetFrame), labelWidth, CGRectGetHeight(insetFrame))
middleLabel.frame = middleLabelRect
let rightLabelRect = CGRectMake(CGRectGetMaxX(middleLabelRect), CGRectGetMinY(insetFrame), labelWidth, CGRectGetHeight(insetFrame))
rightLabel.frame = rightLabelRect
}}
我已经仔细检查了故事板中的单元格标识符和自定义类等内容是否正确设置。
【问题讨论】:
-
把这段代码翻译成 Swift 到底有什么麻烦……?有什么特别的行吗?
-
我可能会得到的objective-c,但主要是这样的:“在你的故事板中添加一个 UITableViewCell CustomTableViewCell 类型到你的 UITableView 并在... cellForRowAtIndexPath ...更改你的单元格之后连接你的标签” s identifier to your customtableviewcell" 所以,我在我的 tableview 中添加了一个单元格,并将其标识符设置为包含三个标签 (pastebin.com/7EgyNGp5) 的 swift 类的名称,包括将其自定义类设置为该类?然后我在 cellForRowAtIndexPath? 中简单地写这个(pastebin.com/CfS47p3q)?这会输出我使用 cell.leftLabel 的展开可选错误(nil)
-
一直在努力,我现在在我的 cellForRowAtIndexPath 中使用这个:pastebin.com/5vn1EkGS。唯一的问题是 tableview 中什么都没有出现 - 如何将这三个标签放在单元格的 textLabel 中?
-
您应该将代码的内容粘贴到问题中,而不是第 3 方页面中 - 对其他开发人员来说不那么麻烦。您应该使用代码或故事板。如果您决定使用情节提要,请在此处设置
textAligment。更重要的是,即使您想在代码中执行此操作(这是一个坏主意),那么您应该在单元类的init中设置textAligment,而不是在cellForRowAtIndexPath中一遍又一遍地设置它。单元格是可重用的,这些属性应该只设置一次,而不是每次您将单元格出列时。所有这些都是旁注,不是问题的解决方案。 -
明白了!感谢您的小费。我一定会编辑我现有的问题,以便将来为人们提供代码。 :) 在您下面的帖子中,我评论了我正在寻找哪种类型的解决方案(故事板或代码),并决定使用代码 - 所以更多的帮助肯定会受到赞赏!
标签: ios swift uitableview alignment