我正在使用Xcode 7和Swift创建一个小的任务列表应用程序。但是,我希望某些数据(例如日期标签)在UITableViewCell的右侧,除了左侧。我将使用什么代码将日期标签放在UITableViewCell的右侧?
这是我的ViewController.swift代码,其中包含TableView。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// All Outlets Connected to StoryBoard
@IBOutlet var BTN: UIButton!
@IBOutlet var BTN2: UIButton!
@IBOutlet var BTN3: UIButton!
@IBOutlet var BTN4: UIButton!
@IBOutlet var tbl: UITableView?
@IBOutlet var Button: UIBarButtonItem!
@IBOutlet var Bar: UINavigationItem!
//Other Variables
var varView = Int()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.\
if revealViewController() != nil {
Button.target = revealViewController()
Button.action = #selector(SWRevealViewController.revealToggle(_:))
}
self.view.backgroundColor = UIColor(red: 50 / 255.0, green: 132 / 255.0, blue: 255 / 255.0, alpha: 1.0)
BTN.alpha = 1.0
BTN4.alpha = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func BTNClicked(sender: AnyObject) {
UIView.animateWithDuration(1.0, animations: ({
self.BTN2.transform = CGAffineTransformMakeTranslation(0, -90)
self.BTN3.transform = CGAffineTransformMakeTranslation(0, -180)
self.BTN4.transform = CGAffineTransformMakeTranslation(0, 0)
self.BTN.alpha = 0
self.BTN4.alpha = 1.0
}))
}
@IBAction func BTNClickedAgain(sender: AnyObject) {
UIView.animateWithDuration(1.0, animations: ({
self.BTN2.transform = CGAffineTransformMakeTranslation(0, 0)
self.BTN3.transform = CGAffineTransformMakeTranslation(0, 0)
self.BTN4.transform = CGAffineTransformMakeTranslation(0, 0)
self.BTN.alpha = 1.0
self.BTN4.alpha = 0
}))
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if(editingStyle == UITableViewCellEditingStyle.Delete){
taskMgr.tasks.removeAtIndex(indexPath.row)
tbl?.reloadData();
}
}
override func viewWillAppear(animated: Bool) {
tbl?.reloadData();
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return taskMgr.tasks.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "test")
cell.textLabel?.text = taskMgr.tasks[indexPath.row].name
cell.detailTextLabel?.text = taskMgr.tasks[indexPath.row].desc
cell.detailTextLabel?.text = taskMgr.tasks[indexPath.row].date
return cell
}
另外,这是我的AddPlan.swift的外观,这是我在ViewControlelr.swift中将数据添加到UITableViewCells的地方:
class addPlan: UIViewController, UITextFieldDelegate {
var time: Int = 6
@IBOutlet var txt: UITextField!
@IBOutlet var txt2: UITextField! // This is the data text, I want this to be on the left side of the UITableViewCell.
@IBOutlet var txt1: UITextField!
@IBOutlet var Button02: UIBarButtonItem!
override func viewDidLoad() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(addPlan.dismissKeyboard))
view.addGestureRecognizer(tap)
self.txt.delegate = self;
self.txt1.delegate = self;
self.txt2.delegate = self;
if revealViewController() != nil {
Button02.target = revealViewController()
Button02.action = #selector(SWRevealViewController.revealToggle(_:))
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
@IBAction func ClickedforSelection(sender: UIButton) {
taskMgr.addTask(txt.text!, desc: txt1.text!, date: txt2.text!)
self.view.endEditing(true)
txt.text = ""
txt1.text = ""
txt2.text = "" // This is the data text
}
}
最佳答案
您可以将UITableViewCellStyle.Value1
或UITableViewCellStyle.Value2
用作UITableViewCell样式。
来自Table View Styles and Accessory Views
UITableViewCellStyle.Value1
将字幕显示为蓝色文本,然后
将其右对齐到行的右侧。图片不是
允许的。
而UITableViewCellStyle.Value2
将主标题设置为蓝色,
将其右对齐到从左侧左侧缩进的位置
行。字幕左对齐,距离字幕的右侧不远
这点。此样式不允许使用图像。
关于ios - 如何将数据放在UITableViewCell的右侧,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37258038/