2
我想創建一個單選按鈕;不過,我正在使用UITableView
。我有以下代碼:添加附件類型移動標籤在UITableViewCell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .Default, reuseIdentifier: "sortcell")
if indexPath.row == 0 {
cell.textLabel?.text = "Student"
} else {
cell.textLabel?.text = "Teacher"
}
cell.textLabel?.textAlignment = .Center
cell.textLabel?.font = getFont(17.5)
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
var selectedIndexPath = 10
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selected = true
if indexPath.row == 0 {
sort = "Student"
let cell = sortTable.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
let otherIndexPath = NSIndexPath(forRow: 1, inSection: 0)
let otherCell = sortTable.cellForRowAtIndexPath(otherIndexPath)
otherCell?.accessoryType = .None
} else {
sort = "Teacher"
let cell = sortTable.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
let otherIndexPath = NSIndexPath(forRow: 0, inSection: 0)
let otherCell = sortTable.cellForRowAtIndexPath(otherIndexPath)
otherCell?.accessoryType = .None
}
sortTable.deselectRowAtIndexPath(indexPath, animated: true)
}
這允許用戶在選擇他或她是學生還是教師之間切換。然而,當加入accessoryType
,文本標籤輕微移動,如圖所示:
我應該如何解決這個問題?我希望文字標籤留在同一位置,而不是向左移動。謝謝你的幫助。
不幸的是,這是唯一的解決方案。我不得不把桌子移動一下... – penatheboss