gpt4 book ai didi

ios - 如何将数据放在 UITableViewCell 的右侧

转载 作者:行者123 更新时间:2023-11-30 13:25:28 24 4
gpt4 key购买 nike

我正在使用 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.Value1UITableViewCellStyle.Value2 作为您的 UITableViewCell 样式。

来自Table View Styles and Accessory Views

UITableViewCellStyle.Value1 puts the subtitle in blue text and right-aligns it on the right side of the row. Images are not permitted.

While UITableViewCellStyle.Value2 puts the main title in blue and right-aligns it at a point that’s indented from the left side of the row. The subtitle is left aligned at a short distance to the right of this point. This style does not allow images.

关于ios - 如何将数据放在 UITableViewCell 的右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37258038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com